ActionMailer 2.3.5 破坏了我的 zip 附件

发布于 2024-10-19 01:51:55 字数 405 浏览 4 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

空城旧梦 2024-10-26 01:51:55

问题是 File.read 将您的文件视为文本文件。 (我猜你正在 Windows 上尝试这个)你必须指定模式来强制它以二进制模式打开你的文件:

attachment :content_type => "application/zip",
  :body => File.read(zip.path, mode: 'rb'),
  :filename => File.basename(zip.path)

或者在 Rails > 中3:

attachment[File.basename(zip.path)] = File.read(zip.path, mode: 'rb')

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:

attachment :content_type => "application/zip",
  :body => File.read(zip.path, mode: 'rb'),
  :filename => File.basename(zip.path)

Or in Rails > 3:

attachment[File.basename(zip.path)] = File.read(zip.path, mode: 'rb')
怪异←思 2024-10-26 01:51:55

如果您想包含附件并保留正文(多部分邮件),则必须执行以下操作:

  def email(message)
    setup_mail(message)

    part       :content_type => "text/html", 
               :body => render_message("email", @body)

    attachment :content_type => 'application/zip', 
               :body => File.read(message[:file].path), 
               :filename => File.basename(zip.path)
  end

其中“电子邮件”是您的正文模板。

If you want to include an attachment and keep your body (a multipart mail), you must do something like this:

  def email(message)
    setup_mail(message)

    part       :content_type => "text/html", 
               :body => render_message("email", @body)

    attachment :content_type => 'application/zip', 
               :body => File.read(message[:file].path), 
               :filename => File.basename(zip.path)
  end

Where "email" is your body template.

流心雨 2024-10-26 01:51:55

尝试指定附件的编码:

attachment :content_type => "application/zip",
      :body => File.read(zip.path),
      :filename => File.basename(zip.path),
      :transfer_encoding => 'base64'

Try specifying the encoding for the attachment:

attachment :content_type => "application/zip",
      :body => File.read(zip.path),
      :filename => File.basename(zip.path),
      :transfer_encoding => 'base64'
尐籹人 2024-10-26 01:51:55

可能您需要以二进制读取模式打开 zip 文件:

:body => File.open(zip.path, 'rb') {|f| f.read}

Likely you need to open your zipfile in binary read mode:

:body => File.open(zip.path, 'rb') {|f| f.read}
记忆之渊 2024-10-26 01:51:55

我在我的项目中遇到了同样的问题。我混合了“mu 太短”和“fivaiez”的解决方案。现在可以了。非常感谢大家的评论。以下是我的代码。

def sent(sent_at = Time.now)
  subject    'test attachment mail'
  recipients '[email protected]'
  from       '[email protected]'
  sent_on    sent_at
  content_type "text/html"
  attachment :content_type => 'application/zip',
             :body => File.read("data/sample.zip"),
             :filename => 'sample.zip',
             :transfer_encoding => "base64"    
end

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.

def sent(sent_at = Time.now)
  subject    'test attachment mail'
  recipients '[email protected]'
  from       '[email protected]'
  sent_on    sent_at
  content_type "text/html"
  attachment :content_type => 'application/zip',
             :body => File.read("data/sample.zip"),
             :filename => 'sample.zip',
             :transfer_encoding => "base64"    
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文