【问题标题】:HTML Emails missing attachmentsHTML 电子邮件缺少附件
【发布时间】:2012-01-03 06:29:54
【问题描述】:

当我使用 Ruby 1.92 发送 HTML 电子邮件时,某些电子邮件客户端中没有显示附件。附件应该是pdf文件。我可以在 Exchange 服务器上的 Outlook 2010 中看到附件,但在 yahoo Web 邮件中看不到。

如果我只是禁用 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: wstaples@consoto.com

To: WSTAPLES@consoto.COM

Message-ID: <4ecba22c9ecb3_1cec974ab4171df@GBFA100016.mail>

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: <4ecba22c75492_1cec974ab416991@GBFA100016.mail>



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: <4ecba22c92576_1cec974ab41707b@GBFA100016.mail>



<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: WSTAPLES@consoto.COM<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--

【问题讨论】:

  • 可能是某些客户端将其作为垃圾邮件删除?这是什么邮件库?也许问题应该针对该图书馆的联系信息?
  • 在正确看到消息的客户端上,您可以查看原始源(包括标题)并发布完整的原始消息吗?
  • 如果您使用github.com/mikel/mail,则发布message.to_s 的输出(使用小txt 文件而不是pdf)
  • 嗨@DGM,是的,这是 github.com/mikel/mail gem。这是 message.to_s 的输出
  • FWIW,附件有很多错误。 github.com/mikel/mail/issues/search?q=attachment

标签: html ruby email


【解决方案1】:

问题出在你的

Content-Type: 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

【讨论】:

猜你喜欢
  • 2012-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-30
  • 2018-09-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多