簡體   English   中英

Rails ActionMailer - 附加文本文件會導致電子郵件正文作為附件發送

[英]Rails ActionMailer - Attaching text file causes email body to be sent as attachment

當我嘗試將文本文件(擴展名為.txt )附加到電子郵件時,電子郵件將在沒有正文和附加附件的情況下發送: noname.html 此文件包含我希望在電子郵件正文中獲得的內容。 當我將文件的擴展名更改為.txt以外的其他內容並附加它時,問題不會發生。

調查電子郵件的mimeparts顯示文本附件mimepart出現在html mimepart之前,我想成為電子郵件的正文。 但是,我不確定mimeparts的順序是否會導致問題。

我創建電子郵件的代碼如下所示:

render_to_string # Using a template
attachments[name] = File.read...
mail(options)

將此添加到options參數沒有幫助:

content_type: 'multipart/alternative',
parts_order: [ 'text/html', 'text/enriched', 'text/plain' ] 

這個問題的原因是什么? 如何強制html部分成為電子郵件的正文?

我通過附加mime type text/x-diff文本文件修復了這個問題:

mime_type = ... # Get mime type, e.g.: Paperclip::ContentTypeDetector.new(attachment_file_name).detect
mime_type = mime_type == 'text/plain' ? 'text/x-diff' : mime_type
attachments[attachment_file_name] = { content: File.read..., mime_type: mime_type }

通過使用text/x-diff交換mime類型的text/plain ,文本文件附件不會被混淆為body。 它已正確附加到電子郵件,並且電子郵件正文作為正文正確發送。

對於Rails 4/5只需使用

render_to_string mail.attachments[name] = File.read... mail(options)

添加郵件。 附件解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM