如何使用 ActionMailer 防止电子邮件附件内联呈现

发布于 2024-08-14 12:27:33 字数 738 浏览 7 评论 0原文

我使用以下代码发送带有 pdf 附件的电子邮件:

class StudyMailer < ActionMailer::Base
  def notify_office(study, sent_at = Time.now)
    subject    "Email Subject Goes Here"
    recipients '[email protected]'
    from       "#{study.sender.full_name} <#{study.sender.email}>"
    sent_on    sent_at
    body       :study => study
    for document in study.documents   
      attachment :content_type => "application/pdf", :body => File.read(document.document.path) #absolute path to .pdf document
    end
  end
end

发送电子邮件时,附件似乎内联呈现为二进制代码而不是 .pdf 附件。

如何将 .pdf 呈现为典型附件,而不是内联附件?

I am using the following code to send an email with a pdf attachment:

class StudyMailer < ActionMailer::Base
  def notify_office(study, sent_at = Time.now)
    subject    "Email Subject Goes Here"
    recipients '[email protected]'
    from       "#{study.sender.full_name} <#{study.sender.email}>"
    sent_on    sent_at
    body       :study => study
    for document in study.documents   
      attachment :content_type => "application/pdf", :body => File.read(document.document.path) #absolute path to .pdf document
    end
  end
end

When the email is sent, the attachment seems to render inline as binary code rather than as a .pdf attachment.

How do I render the .pdf as a typical attachment, rather than inline?

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

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

发布评论

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

评论(3

汐鸠 2024-08-21 12:27:33
attachment :content_type => "application/pdf", 
    :content_disposition => "attachment", 
    :filename => File.basename(fattach), 
    :body => File.new(fattach,'rb').read() 

注意内容处置线。

attachment :content_type => "application/pdf", 
    :content_disposition => "attachment", 
    :filename => File.basename(fattach), 
    :body => File.new(fattach,'rb').read() 

Notice the content-disposition line.

凌乱心跳 2024-08-21 12:27:33

我相信您必须指明电子邮件的多部分性质,因此请在 from 行下添加此行:

content_type    "multipart/alternative"

I believe you have to indicate the multipart nature of the email, so add this line under the from line:

content_type    "multipart/alternative"
伴我老 2024-08-21 12:27:33

您的电子邮件有模板吗?如果电子邮件没有模板,即使其他所有设置均正确,附件也会内联显示。创建附件电子邮件模板。

views/notifier/attachment.html.erb

<p>   Please see attachment    </p>

然后在通知程序中指定使用此模板。

通知程序.rb

def my_email_method
    ...
    mail(:template_name => 'attachment', :from => from_address, ...)
end

Does your email have a template? If the email does not have a template, the attachment shows up inline even if everything else is set up correctly. Create an attachment email template.

views/notifier/attachment.html.erb

<p>   Please see attachment    </p>

Then in Notifier, specify to use this template.

notifier.rb

def my_email_method
    ...
    mail(:template_name => 'attachment', :from => from_address, ...)
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文