防止 ActionMailer 去除纯消息中的重复空格

发布于 2025-01-07 14:46:01 字数 1585 浏览 4 评论 0原文

我试图在文本邮件中有一个表格,所以我写了一些助手:

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

I'm trying to have an table in text mail, so I write some helpers:

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

Then in view I write it like this:

<%= cell 'Test', 10 %>

But that what I get (according to letter_opener) is:

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

As can you see, the spaces that are repeating before Test. My question is how to prevent ActionMailer (or anything else what is destroying my beautiful table) from doing that.

Mailer code:

  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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光与爱终年不遇 2025-01-14 14:46:01

我发现了错误。这是由我用来在 HTML 部分内联 CSS 的 Premailer 引起的。

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

I've found bug. It was caused by Premailer what I use to inline CSS in HTML part.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文