阻止ActionMailer删除明文消息中的重复空格

时间:2012-02-18 16:42:55

标签: ruby-on-rails whitespace actionmailer ascii-art

我正在尝试用文本邮件创建一个表,所以我写了一些助手:

module MailerHelper
  def field_width(text, width)
    ' ' * (width - text.length) + text
  end

  def cell(text, width)
    output = '| ' + field_width(text, width-2) + " |\n"
    output << '+-' + '-'*(width-2) + '-+'
  end
end

然后在视图中我这样写:

<%= cell 'Test', 10 %>

但我得到的(根据letter_opener)是:

| Test |
+----------+

如您所见,Test之前重复的空格。我的问题是如何阻止ActionMailer(或其他任何破坏我漂亮的桌子的东西)这样做。

邮件代码:

  def remind(client, invoices)
    @client = client
    @company = @client.company
    @invoices  = invoices.to_a

    days_left = @invoices.first.pay_date - Date.today
    message = @client.group.messages.find_by_period days_left.to_i

    raise 'No messages for this invoices.' if message.nil?

    @template = message.template || if days_left < 0
      t 'message.before'
    elsif days_left > 0
      t 'message.after'
    else
      t 'message.today'
    end

    @text = liquid_parse @template
    @html = markdown_parse @text

    mail(:to => @client.email, :subject => t('message.title'))
  end

  private
    def markdown_parse(text)
      markdown = Redcarpet::Markdown.new Redcarpet::Render::HTML,
        :autolink => true, :space_after_headers => true
      markdown.render text
    end

    def liquid_parse(text)
      renderer = Liquid::Template.parse text
      renderer.render 'company' => @company, 'invoice' => @invoice, 'client' => @client
    end

1 个答案:

答案 0 :(得分:1)

我发现了错误。它是由Premailer引起的,用于在HTML部分中内联CSS。

class InlineCSSInterceptor
  def self.delivering_email(message)
    #message.text_part.body = Premailer.new(message.text_part.body.to_s, with_html_string: true).to_plain_text # this is line causing the problem.
    message.html_part.body = Premailer.new(message.html_part.body.to_s, with_html_string: true).to_inline_css
  end
end

Mailer.register_interceptor InlineCSSInterceptor