【问题标题】:Rails - ActionMailer sometimes shows attachments before the email content?Rails - ActionMailer 有时会在电子邮件内容之前显示附件?
【发布时间】:2011-07-24 02:36:13
【问题描述】:

如何使 ActionMailer 始终在邮件底部显示附件: HTML、TXT、附件....

问题是这里的附件是一个文本文件:

----==_mimepart_4d8f976d6359a_4f0d15a519e35138763f4
Date: Sun, 27 Mar 2011 13:00:45 -0700
Mime-Version: 1.0
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename=x00_129999.olk14message
Content-ID: <4d8f976d49c72_4f0d15a519e351387611f@railgun64.53331.mail>

谢谢

【问题讨论】:

  • 您可以从邮件中发布代码吗?我怀疑我知道问题可能出在哪里。
  • @AnApprentice 我有同样的问题。你解决了吗?

标签: ruby-on-rails ruby-on-rails-3 actionmailer


【解决方案1】:

我知道已经有一个可接受的答案,但是切换attachments[]mail() 的顺序并没有解决我的问题。我的情况不同的是我试图附加一个文本文件附件(.txt)

对我有用的是为邮件设置 content_typeparts_order 默认值。

MyMailer < ActionMailer::Base

    default :from => "Awesome App <support@example.com>",
            :content_type => 'multipart/alternative',
            :parts_order => [ "text/html", "text/enriched", "text/plain", "application/pdf" ]

    def pdf_email(email, subject, pdfname, pdfpath)
      attachments[pdfname] = File.read(pdfpath)
      mail(:to => email, :subject => subject)
    end

    def txt_email(email, subject, filename, filebody)
      attachments[filename] = filebody
      mail(:to => email, :subject => subject)
    end
end

如果您尝试在 Rails 3 中使用纯文本文件 (.txt) 发送电子邮件,请尝试将 :content_typeparts_order 添加到您的默认值中,以便文本文件不会出现在您的电子邮件中的消息上方.

【讨论】:

  • parts_order 工作。虽然不一定放入default 部分。它可以是个人mail(...)的论据
【解决方案2】:

我遇到了同样的问题,就我而言,解决方案是交换附件和邮件行。先附加,再调用邮件。

轨道 3

错误

def pdf_email(email, subject, pdfname, pdfpath)
  mail(:to => email, :subject => subject)
  attachments[pdfname] = File.read(pdfpath)
end

def pdf_email(email, subject, pdfname, pdfpath)
  attachments[pdfname] = File.read(pdfpath)
  mail(:to => email, :subject => subject)
end

【讨论】:

    【解决方案3】:

    这是 rails 2.3 代码(在 rails3 中可能略有不同)

    在附件之前移动你的文本部分

    recipients  to@domain.com
    from      me@domain.com
    subject   "some subject"
    content_type  "multipart/mixed"
    
    part "text/plain" do |p|
      p.body = render_message 'my_message' #this is template file
    end
    
    attachment "application/octet-stream" do |a|
      a.body = File.read("some_file.jpg")
      a.filename = 'name.jpg'
    end
    

    【讨论】:

      猜你喜欢
      • 2011-09-02
      • 2014-05-29
      • 1970-01-01
      • 2018-07-20
      • 2015-08-29
      • 2011-07-01
      • 1970-01-01
      • 2015-02-06
      • 1970-01-01
      相关资源
      最近更新 更多