Ruby on rails 防止ActionMailer删除普通邮件中的重复空格

Ruby on rails 防止ActionMailer删除普通邮件中的重复空格,ruby-on-rails,whitespace,actionmailer,ascii-art,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) + '-+

我试图在文本邮件中创建一个表,因此我编写了一些帮助:

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
如您所见,在
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
def提醒(客户、发票)
@客户端=客户端
@company=@client.company
@发票=发票
剩余天数=@invoices.first.pay\u date-date.today
message=@client.group.messages.find_by_period d_left.to_i
引发“此发票无消息”。如果为message.nil?
@template=message.template | |如果剩余天数<0
信息,在之前
剩余天数>0
留言,在
其他的
今天的消息
结束
@text=liquid_parse@template
@html=标记\解析@文本
邮件(:to=>@client.email,:subject=>t('message.title'))
结束
私有的
def标记解析(文本)
markdown=red地毯::markdown.new red地毯::Render::HTML,
:autolink=>true,:头后面的空格=>true
markdown.render文本
结束
def液体解析(文本)
renderer=Liquid::Template.parse text
renderer.render'company'=>@公司,'发票'=>@发票,'客户'=>@客户机
结束

我发现了一个bug。这是由我用来在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

是时候打开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
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