ActionMailer 2.3.5 破坏了我的 zip 附件
我正在尝试使用 ActionMailer 2.3.5 从 Rails 发送带有 zip 附件的电子邮件。
zip 文件在服务器上完好无损(使用解压缩实用程序可以正确解压缩),但到达收件人的 zip 文件已损坏。此外,添加附件会导致电子邮件中省略邮件正文。
该方法没有什么值得注意的地方:
attachment :content_type => "application/zip",
:body => File.read(zip.path),
:filename => File.basename(zip.path)
File.read 显然出了问题。当我在此处传递字符串而不是文件内容时,附件会正确通过。与二进制数据有关吗?
搞什么?
I'm attempting to send email with a zip attachment from rails with ActionMailer 2.3.5.
The zip file is sound on the server (unzips correctly with the unzip utility), but the zip file that comes through to the recipient is corrupted. Also, adding the attachment causes the message body to be omitted from the email.
There's nothing remarkable about the method:
attachment :content_type => "application/zip",
:body => File.read(zip.path),
:filename => File.basename(zip.path)
There's apparently something going awry around File.read. When I pass a string here instead of the file contents, the attachment comes through correctly. Something to do with binary data?
WTF?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
问题是
File.read
将您的文件视为文本文件。 (我猜你正在 Windows 上尝试这个)你必须指定模式来强制它以二进制模式打开你的文件:或者在 Rails > 中3:
The problem is that
File.read
treats your file as a text file. (I guess you are trying this on Windows) You have to specify the mode to force it to open your file in binary mode:Or in Rails > 3:
如果您想包含附件并保留正文(多部分邮件),则必须执行以下操作:
其中“电子邮件”是您的正文模板。
If you want to include an attachment and keep your body (a multipart mail), you must do something like this:
Where "email" is your body template.
尝试指定附件的编码:
Try specifying the encoding for the attachment:
可能您需要以二进制读取模式打开 zip 文件:
Likely you need to open your zipfile in binary read mode:
我在我的项目中遇到了同样的问题。我混合了“mu 太短”和“fivaiez”的解决方案。现在可以了。非常感谢大家的评论。以下是我的代码。
I met the same issue in my project. I mixed solutions from "mu is too short" and "fivaiez". Now it works. Thanks a lot for all you guys comments. The following is my code.