使用perl发送包含文件附件的multipart text / html替代消息,

时间:2013-06-17 10:57:36

标签: perl email mime email-attachments

我正忙着整理一个脚本,该脚本使用多部分替代部件和附件创建电子邮件。现在一切似乎都正常工作,除了普通附件,它们似乎无法在Gmail或雅虎网络邮件中下载,我的所有内联图像都可用,但如果我附加任何内容,无论是简单的.txt文件还是pdf它们都是无法下载,我已将处置设置为附件..我可以在邮件中看到它。以下是我如何去做。 还有其他我应该做的事吗?

 my    $ent = MIME::Entity->build(From      => $from,
                             To             => $to,
                             Type           => "multipart/alternative",
                             'X-Mailer'     => undef);
if ($plain_body)
{
    $ent->attach(
    Type            => 'text/plain; charset=UTF-8',
    Data            => $plain_body,
    Encoding        => $textEnc,
  );
}

 $ent->attach(Type => 'multipart/related');
 $ent->attach(
    Type            => 'text/html; charset=UTF-8',
    Data            => $content,
    Encoding        => $htmlEnc,
  );


 if ($count != 0) {
 for my $i (0..$count-1){
 $ent->attach(Data        => $attachment[$i],
             Type         => $content_type[$i],
             Filename     => $attname[$i],
             Id           => $cid[$i],
             Disposition  => $disposition[$i],
             Encoding     => $encoding[$i]);
  }

//// \在Yahoo或Gmail网络邮件中看不到的附件..

------------=_1371465849-18979-0
Content-Type: text/plain; name="Careways Final.txt"
Content-Disposition: attachment; filename="Careways Final.txt"
Content-Transfer-Encoding: quoted-printable

1 个答案:

答案 0 :(得分:8)

我的知识缺乏,您没有提供足够的代码来重现该电子邮件,但我认为您的问题可能是您将所有部件/附件附加到单个顶级多部件/替代部件。

如果您有$ plain_body和一个附件(所以$ count = 1),您的多部分/替代消息将包含4个部分,如下所示:

multipart/alternative:
    text/plain
    text/html
    mutlipart/related (it is empty, you did not attach anything to it!)
    text/plain (or other type, depends what attachment you had

您可能需要以下内容:

multipart/mixed:
    multipart/alternative:
        text/plain
        text/html
    text/plain (first attachment)

因此整个消息是multipart / mixed类型,它包含: 1. multipart / alternative有两个替代消息(大概是html和text) 2.任意数量的“附件”

在您当前的版本中,所有内容都在multipart / alternative下,因此邮件阅读器会将您的所有附件视为相同内容的不同版本,并且可能无法提供“下载到他们”

您可以通过调用 - >附加方法来创建嵌套零件结构,这些零件是其他附加的结果,例如:

my $top = MIME::Entity->build(
    ...
    Type           => "multipart/mixed",
);
my $alternative = $top->attach(
    ...
    Type    => "multipart/alternative",
);
# Now we add subparts IN the multipart/alternative part instead of using main message
$alternative->attach( ..., Type => "text/plain");
$alternative->attach( ..., Type => "text/html");

# Now we add attachments to whole message again:
for (@attachments) {
    $top->attach( ... );
}