2012-04-12 96 views
1

我想編寫一個小的Perl腳本,它發送純文本/ HTML的郵件帶附件:電子郵件與純文本部分,HTML部分和附件

 use Encode(); 
     use MIME::Entity(); 
     my $msg = MIME::Entity->build(
     From  => '"' . Encode::encode('MIME-Header', 'Any User') . '" <[email protected]>', 
     To  => '"' . Encode::encode('MIME-Header', 'Some User') . '" <[email protected]>', 
     Subject  => Encode::encode('MIME-Header', 'Multipart alternative with attachments'), 
     Type  => 'multipart/alternative', 
    ); 
     my $plain = $msg->attach(
     Type   => 'text/plain; charset=UTF-8', 
     Data   => [ "*Multipart alternative with attachments*\n\nHere comes the plain text." ], 
    ); 
     my $fancy = $msg->attach(Type => 'multipart/related'); 
     $fancy->attach(
     Type   => 'text/html; charset=UTF-8', 
     Data   => [ "<html>\n\t<body>\n\t\t<h1>Multipart alternative with attachments</h1>\n\t\t<p>Here comes the fancy text.</p>\n\t</body>\n</html>" ], 
    ); 
     $fancy->attach(
     Type   => 'text/plain; charset=UTF-8', 
     Disposition  => 'attachment', 
     Filename  => 'test.txt', 
     Data   => [ "*Multipart alternative with attachments*\n\nHere comes the attachment." ], 
    ); 

我正在以下MIME源代碼:

 Content-Type: multipart/alternative; boundary="----------=_1334231022-8218-0" 
     Content-Transfer-Encoding: binary 
     MIME-Version: 1.0 
     X-Mailer: MIME-tools 5.502 (Entity 5.502) 
     From: "Any User" <[email protected]> 
     To: "Some User" <[email protected]> 
     Subject: Multipart alternative with attachments 

     This is a multi-part message in MIME format... 

     ------------=_1334231022-8218-0 
     Content-Type: text/plain; charset=UTF-8 
     Content-Disposition: inline 
     Content-Transfer-Encoding: binary 

     *Multipart alternative with attachments* 

     Here comes the plain text. 
     ------------=_1334231022-8218-0 
     Content-Type: multipart/related; boundary="----------=_1334231022-8218-1" 
     Content-Transfer-Encoding: binary 

     This is a multi-part message in MIME format... 

     ------------=_1334231022-8218-1 
     Content-Type: text/html; charset=UTF-8 
     Content-Disposition: inline 
     Content-Transfer-Encoding: binary 

     <html> 
       <body> 
         <h1>Multipart alternative with attachments</h1> 
         <p>Here comes the fancy text.</p> 
       </body> 
     </html> 
     ------------=_1334231022-8218-1 
     Content-Type: text/plain; charset=UTF-8; name="test.txt" 
     Content-Disposition: attachment; filename="test.txt" 
     Content-Transfer-Encoding: binary 

     *Multipart alternative with attachments* 

     Here comes the attachment. 
     ------------=_1334231022-8218-1-- 

     ------------=_1334231022-8218-0-- 

如果我中The Bat查看電子郵件,普通版本和HTML版本正確顯示。在Mozilla Thunderbird 11.0.1中,純文本不會顯示,而是縮小的html版本。

我的邏輯有錯誤還是Thunderbird失敗?

回答

3

你不需要$fancy,把所有東西都加到$msg

Content-Transfer-Encoding標題更好base64quoted-printable。這是更加萬無一失。

Content-Type: text/plain; charset=UTF-8 
Content-Transfer-Encoding: base64 

Content-Type: text/html; charset=UTF-8 
Content-Transfer-Encoding: quoted-printable 
+0

[RFC 1521](http://tools.ietf.org/html/rfc1521)已過時。將來,請使用IETF網站查找RFC文本;靠近頂部顯示相關的RFC。 – daxim 2012-04-12 20:01:45

相關問題