【问题标题】:ActionMailer cannot attach a HTML fileActionMailer 无法附加 HTML 文件
【发布时间】:2012-10-10 18:27:15
【问题描述】:

我不确定为什么我通过 ActionMailer 发送的带有附件的电子邮件没有显示附件,而是附件的内容只是显示/输出到电子邮件中。

class MyMailer < ActionMailer::Base
  default :from => "email@email.com"

  def test_email()
    attachments['testresult1.html'] = {
      :filename => 'testresult1.html',
      :body => File.read('C:\\testresult1.html')
    }

  mail(:to => 'email@email.com',
    :subject => 'Cool great message'
  )
  end
end

电子邮件的输出如下所示

Date: Tue, 17 Apr 2012 14:11:52 +1000
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8;
filename=testresult1.html
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename=testresult1.html
filename: testresult1.html
Content-ID: <4f8ced883c0d9_1b1c30b580813e@PC-049.mail>

然后是 HTML 文件/附件的全部内容。

我希望将 HTML 文件附加到电子邮件中,因为它会附加到普通电子邮件中,我应该能够双击附件并在浏览器中打开它。

Email Client = Microsoft Outlook
Connection = SMTP
Rails = 3.2.1
ActionMailer = 3.2.1
Mail = 2.4.4

【问题讨论】:

    标签: ruby-on-rails actionmailer


    【解决方案1】:

    您应该尝试将body 键添加到#mail。

    【讨论】:

    • 嗨,谢谢,这有效。你能解释一下它为什么起作用吗??
    • 根据我的经验,带有附件且没有正文的 ActionMailer 电子邮件会变成杂乱无章的标题和内容。我想这应该归咎于糟糕的测试覆盖率(他们的测试似乎假设总是有一个身体)。
    • 啊,我同意并且完全有道理。看起来像一个必须与 Ruby 社区一起提出的错误。感谢您的回复。
    【解决方案2】:

    非常感谢菲利普 :)

    我发送的附件没有任何正文属性 我面临以下两个问题:

    1. PDF 的名称总是以“noname”的形式出现
    2. 有时 PDF 会损坏

    但是在邮件中添加正文属性后它可以工作。

    所以,正确的语法是:

    attachments['mypdf.pdf'] = File.read('#{Rails.root.to_s}/public/test.pdf')
    mail(:to => guru****@gmail.com, :from => guru****@gmail.com, :subject => 'Attachment Test Mail', :body => 'Regards, Guru')
    

    【讨论】:

      【解决方案3】:

      我对“Noname”也有同样的问题并且已损坏。

      我正在使用以下代码:

      attachments['126539_statistics.pdf'] = File.read("app/assets/pdfs/126539_statistics.pdf")
      mail(:to => email, :subject => subject, :body => message)
      

      发送带有附件的邮件没有任何问题...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-04
        • 1970-01-01
        • 2020-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多