Ruby on rails ActionMailer 2.3.5弄坏了我的拉链附件

Ruby on rails ActionMailer 2.3.5弄坏了我的拉链附件,ruby-on-rails,binary,zip,actionmailer,Ruby On Rails,Binary,Zip,Actionmailer,我正在尝试使用ActionMailer 2.3.5从rails发送带有zip附件的电子邮件 zip文件在服务器上运行正常(使用解压实用程序正确解压),但传递给收件人的zip文件已损坏。此外,添加附件会导致邮件正文从电子邮件中省略 这种方法没有什么特别之处: attachment :content_type => "application/zip", :body => File.read(zip.path), :filename => File.basen

我正在尝试使用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出现了一些问题。当我在这里传递一个字符串而不是文件内容时,附件将正确传递。与二进制数据有关吗


WTF?

尝试指定附件的编码:

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

请尝试指定附件的编码:

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

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

  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

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

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

  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

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

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

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

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

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

我在我的项目中遇到了同样的问题。我混合了“mu太短”和“fivaiez”的溶液。现在它起作用了。非常感谢你们的评论。下面是我的代码

def sent(sent_at = Time.now)
  subject    'test attachment mail'
  recipients 'mail-list@company.com'
  from       'please_no_reply@company.com'
  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

我在我的项目中遇到了同样的问题。我混合了“mu太短”和“fivaiez”的溶液。现在它起作用了。非常感谢你们的评论。下面是我的代码

def sent(sent_at = Time.now)
  subject    'test attachment mail'
  recipients 'mail-list@company.com'
  from       'please_no_reply@company.com'
  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

问题是
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')

问题是
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')

谢谢,这解决了邮件正文丢失的问题。但是,Zip文件仍处于损坏状态。谢谢-这解决了缺少邮件正文的问题。但是,Zip文件仍处于损坏状态。感谢您的建议。不幸的是,zip文件仍然存在问题。谢谢你的建议。不幸的是,zip文件仍然存在问题。干杯:)对电子邮件附件和原始zip文件进行了区分。文件结尾(317字节)似乎被截断。对电子邮件附件和原始zip文件进行了区分。文件结尾(317字节)似乎被截断。