繁体   English   中英

Rails - ActionMailer有时会在电子邮件内容之前显示附件?

[英]Rails - ActionMailer sometimes shows attachments before the email content?

我怎么能这样做,所以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>

谢谢

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

对我parts_order是为邮件程序设置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

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

我有同样的问题,在我的情况下解决方案是交换附件和邮件行。 首先附上,然后打电话给邮件。

Rails 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

这是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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM