繁体   English   中英

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

[英]Rails mail attachment is in e-mail body

我需要发送带附件的简单邮件。 现在它会发送一封电子邮件,但附件在电子邮件正文中是这样的。 在此处输入图片说明

--
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

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

对于其他遇到此问题的人,这应该可以从 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

我的应用程序中有一些不同的附件代码,我不确定这是否适合您,但您可以尝试一下。

尝试更换块

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: '')

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

你也对你的邮件有看法吗?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM