Rails 通过邮件发送附件的邮件程序问题

Rails Mailer issue with sending attchemnt over mail

我正在将应用程序从 Rails 2.3 迁移到 Rails 3.1,电子邮件无法正常工作,当我发送带有附件的电子邮件时,我看到一封带有编码的 plain/text 电子邮件电子邮件中的 pdf 内容而不是附件。

这是我用来发送电子邮件的命令

ret = UserMailer.return_forms(@customer[:email], @store, id, @customer[:document]).deliver

这里是returns_form的定义,return_forms是UserMailerclass(class UserMailer < ActionMailer::Base)

中的一个方法
  def return_forms(email, store, order, pdf_document_path)
    load_smtp_settings("noreply")
    @recipients = email
    @subject    = "#{business_name}: Return forms"
    @body       = "Please follow the instructions within the forms to return your merchandise. Thank You."
    attachments['free_book.pdf'] = {mime_type: 'application/pdf',content:  File.read(pdf_document_path) }
    @from       = email
    @date    = Time.zone.now
    @headers    = {}
  end

我看到的内容是

--
Date: Mon, 10 Aug 2015 16:16:26 +0530
Mime-Version: 1.0
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-ID: <55c881028af96_48d43fe94782e9a0658aa@amol-desktop.mail>

Please follow the instructions within the forms to return your merchandise. Thank You.

--
Date: Mon, 10 Aug 2015 16:16:26 +0530
Mime-Version: 1.0
Content-Type: application/pdf;
 charset=UTF-8
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename=free_book.pdf
Content-ID: <55c88102872ba_48d43fe94782e9a0657a0@amol-desktop.mail>

JVBERi0xLjQKJcfsj6IKNyAwIG9iago8PC9MZW5ndGggOCAwIFIvRmlsdGVy
IC9GbGF0ZURlY29kZT4+CnN0cmVhbQp4nH1W227bOBB991cM0AWqABZLUvc8
bTZNivTiuLUX2GKzD6xE22pk0aWktOnX75CSbUW+xEhkDufMGc4cjvIDKGFA
zad7puvRmy8JLKuRtYJejnyfghd5MQRBSIGjTY4W1srDkB+xeh4HHocQ92yM
clxzr7NFLD6w7fx6WOtn4gWhD2ewjEfeaZIeuHW0GUYxg3Nwn/rhGaI+vnVt
8wzhHN4PqX+GqQ+3njbTxIez8DhOzhD14dZzUM+j8MD3zxH14K3nIOZReEjp

return_forms 调用应该在末尾调用 mail() 方法。类似于:

mail(:from => your_from, :to => your_to, :subject => your_subject, :body => your_body)

在您的 development.rb 中启用邮件错误以查看是否出现任何错误也是一个好主意:

config.action_mailer.raise_delivery_errors = true