Rails Mailer发送邮件附件的问题

时间:2015-08-10 10:52:42

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

我正在将应用程序从Rails 2.3迁移到Rails 3.1,电子邮件无法正常工作,当我发送带附件的电子邮件时,我会在电子邮件中看到带有编码的pdf内容的普通/文本电子邮件,而不是作为附件。< / p>

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

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

这里是returns_form的定义,return_forms是UserMailer class (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

1 个答案:

答案 0 :(得分:1)

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