2011-11-21 94 views
3

當我使用Ruby 1.92發送HTML電子郵件時,附件不會在某些電子郵件客戶端中顯示。附件應該是一個pdf文件。我可以在交換服務器上看到Outlook 2010中的附件,但不能在雅虎網絡郵件中看到。HTML電子郵件缺少附件

如果我只是禁用HTML正文一切正常。

下面是代碼:

require 'mail' 

body_text = File.read('body.html') 
subject_text = File.read("subject.txt") 

Mail.defaults do 
    delivery_method :smtp, { :address => "servername", :port => "25", :user_name => "name", :password => "password", :authentication => 'login'} 
end 

message = Mail.new do 
    from ARGV[0] 
    to ARGV[1] 
    subject subject_text 

    # if I comment out this section everything works fine 
    html_part do 
     content_type 'text/html; charset=UTF-8' 
     body body_text 
    end 

    add_file ARGV[2] 
end 

message.deliver! 

這裏是HTML來源:

<html> 
<body> 
<p style="font-family:trebuchet ms,helvetica,sans-serif;">Hello, <br /> 
<br /> 
This email was sent from Geiger and contains your A/R aged report for date. For any questions please call the Geiger helpdesk @ 207-755-2510<br /> 
Technical Details:<br /> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sent from: Server_Name<br /> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sent To: rep_id<br /> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Original Email: to_email<br /> 
<br /> 
Helpdesk Please forward all enquiries to so and so<br /> 
<br /> 
<img src="http://www.geiger.com/logo.gif" /></p> 
</body> 
</html> 

這裏是message.to_s的輸出:

Date: Tue, 22 Nov 2011 08:22:52 -0500 

From: [email protected] 

To: [email protected] 

Message-ID: <4ecba22c9ecb[email protected]> 

Subject: Your report for November, 2011 - consoto Gets it! 

Mime-Version: 1.0 

Content-Type: multipart/alternative; 

boundary="--==_mimepart_4ecba22c550e5_1cec974ab416812"; 

charset=UTF-8 

Content-Transfer-Encoding: 7bit 







----==_mimepart_4ecba22c550e5_1cec974ab416812 

Date: Tue, 22 Nov 2011 08:22:52 -0500 

Mime-Version: 1.0 

Content-Type: text/plain; 

charset=UTF-8; 

filename=test_attatchment.txt 

Content-Transfer-Encoding: 7bit 

Content-Disposition: attachment; 

filename=test_attatchment.txt 

Content-ID: <[email protected]> 



This is a test 



----==_mimepart_4ecba22c550e5_1cec974ab416812 

Date: Tue, 22 Nov 2011 08:22:52 -0500 

Mime-Version: 1.0 

Content-Type: text/html; 

charset=UTF-8 

Content-Transfer-Encoding: 7bit 

Content-ID: <[email protected]> 



<html> 

<body> 

<p style="font-family:trebuchet ms,helvetica,sans-serif;">Hello, <br /> 

<br /> 

This email was sent from consoto and contains your report for Nov 2011. For any questions please call the consoto helpdesk @ 207-755-2510<br /> 

Technical Details:<br /> 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sent from: GBFA100016<br /> 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sent To: BDB<br /> 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Original Email: [email protected]<br /> 

<br /> 

Helpdesk Please forward all enquiries to Wesley Staples or Shawn White<br /> 

<br /> 

<img src="http://www.consoto.com/logo.gif" /></p> 

</body> 

</html> 





----==_mimepart_4ecba22c550e5_1cec974ab416812-- 
+0

這可能是一些客戶端刪除它爲垃圾郵件?這是什麼郵件庫?也許問題應該針對該圖書館的聯繫信息? – DGM

+0

在確實看到消息正確的客戶端上,您能查看原始源代碼(包括頭文件)併發布完整的原始消息嗎? – DGM

+0

如果您使用的是https://github.com/mikel/mail,則發佈'message.to_s'的輸出(使用小的txt文件而不是pdf) – DGM

回答

0

問題在於您的

Content-Type: multipart/alternative; 

當這些郵件具有內容類型「multipart/alternative」和附件時,某些郵件程序不會正確解析郵件。在這種情況下,你必須用這種結構建立的消息:

- multipart/mixed 
    - multipart/alternative 
... 

你可以這樣說:

unless mail_attachments.blank? 
    mail.part :content_type => "multipart/alternative" do |p| 
    p.text_part = text_part 
    p.html_part = html_part 
    end 
    mail_attachments.each do |file| 
    mail.add_file file.local_path 
    end 
else 
    mail.text_part = text_part 
    mail.html_part = html_part 
end 
+0

感謝讓我走上正軌。感謝你,我在自述文件中找到了另一個示例:https://github.com/mikel/mail#making-multipartalternate-without-a-block – amenthes