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

use*_*569 0 email perl 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]);
  }
Run Code Online (Sandbox Code Playgroud)

//// \在Yahoo或Gmail webmail中看不到的附件..

------------=_1371465849-18979-0
Content-Type: text/plain; name="Careways Final.txt"
Content-Disposition: attachment; filename="Careways Final.txt"
Content-Transfer-Encoding: quoted-printable
Run Code Online (Sandbox Code Playgroud)

bja*_*ski 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
Run Code Online (Sandbox Code Playgroud)

你可能需要一些东西:

multipart/mixed:
    multipart/alternative:
        text/plain
        text/html
    text/plain (first attachment)
Run Code Online (Sandbox Code Playgroud)

因此,整个消息是多部分/混合类型,它包含:1.多部分/替代,带有两个替代消息(可能是html和文本)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( ... );
}
Run Code Online (Sandbox Code Playgroud)