2011-02-23 47 views
1

我试图用ActionMailer 2.3.5从rails的zip附件发送邮件。ActionMailer 2.3.5破解我的zip附件

服务器上的压缩文件是正常的(使用解压缩实用程序正确解压缩),但传入收件人的zip文件已损坏。此外,添加附件会导致电子邮件中省略邮件正文。

有没有什么了不起的方法:

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

有明显的东西约File.read行不通了。当我在这里传递一个字符串而不是文件内容时,附件正确地通过了。与二进制数据有关吗?

WTF?

+0

上有没有一个差异电子邮件附件和原始zip文件。看起来文件的末尾(317字节)被截断了。 – 2011-03-10 04:01:36

回答

0

尝试指定为附件的编码:

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

感谢您的建议。不幸的是,仍然有一个zip文件的问题。干杯:) – 2011-03-10 03:48:42

1

如果要包括附件,并保持你的身体(多部分邮件),你必须做这样的事情:

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 

“电子邮件”是你的身体模板。

+0

谢谢 - 这解决了缺少消息正文的问题。然而,Zip文件仍然通过损坏。 – 2011-03-10 03:46:04

0

可能你需要二进制读模式来打开你的压缩文件:

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

我在项目达到了同样的问题。我混合了“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 
3

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

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

或者在滑轨> 3:

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