Rails邮件附件在电子邮件正文中

时间:2018-05-30 11:34:47

标签: ruby-on-rails ruby email sendmail ruby-on-rails-4.2

我需要发送带附件的简单邮件。现在它发送了一封电子邮件,但附件在这样的电子邮件正文中。 enter image description here

--
Content-Type: text/csv;
 charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename=errors_in_address_table.csv
Content-ID: <5b0e7d6b4abff_10c12ac224c8b0d4994d@development.mail>

发送电子邮件的当前代码

saved_file = Rails.root.join('db', 'fix_db', 'errors_in_address_table.csv')
mailer = ActionMailer::Base.mail(from: 'no-replay@test.com', to: 'test@test.com', subject: 'Errors in database', body: '', content_type: 'multipart/mixed')
mailer.attachments['errors_in_address_table.csv'] = { mime_type: 'text/csv', content: File.read(saved_file) }
mailer.deliver

我试着让它正常工作几个小时。也许一些更有经验的编码员可以帮助我。

3 个答案:

答案 0 :(得分:1)

对于任何对此感到困惑的人,这应该在Rails控制台中起作用(至少在Rails 6+中):

mailer = ActionMailer::Base.new
mailer.attachments['errors_in_address_table.csv'] = File.read(saved_file)
mailer.mail(from: 'from@test.com', to: 'to@test.com', subject: 'Errors', body: '').deliver

答案 1 :(得分:0)

我的应用程序中有一些不同的附件代码,我不确定这对你有用,但你可以尝试一下。

尝试替换你的块

saved_file = Rails.root.join('db', 'fix_db', 'errors_in_address_table.csv')
mailer = ActionMailer::Base.mail(from: 'no-replay@test.com', to: 'test@test.com', subject: 'Errors in database', body: '', content_type: 'multipart/mixed')
mailer.attachments['errors_in_address_table.csv'] = { mime_type: 'text/csv', content: File.read(saved_file) }
mailer.deliver

以下更简单:

attachments['errors_in_address_table.csv'] = open(Rails.root.join('db', 'fix_db', 'errors_in_address_table.csv')).read
mail(from: 'no-replay@test.com', to: 'test@test.com', subject: 'Errors in database', body: '')

这实际上是一种反复试验的方法。如果它不起作用,我会删除。

你还有邮件的视图吗?

答案 2 :(得分:-1)

根据共享的描述,您似乎需要修改附件代码

saved_file = Rails.root.join('db', 'fix_db', 'errors_in_address_table.csv')
mailer = ActionMailer::Base.mail(from: 'no-replay@test.com', to:    'test@test.com', subject: 'Errors in database', body: '', content_type: 'multipart/mixed')
mailer.attachments['errors_in_address_table.csv'] = saved_file
mailer.deliver