2011-02-23 48 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')