【问题标题】:sending multipart text/html alternative message with file attachments with perl,使用 perl 发送带有文件附件的多部分文本/html 替代消息,
【发布时间】:2013-06-13 07:23:58
【问题描述】:

我正忙着编写一个脚本来创建包含多部分替代部分和附件的电子邮件。现在似乎一切正常,除了普通附件,它们似乎无法在 Gmail 或 Yahoo 网络邮件中下载,我所有的内联图像都可用,但如果我附加任何东西,无论是简单的 .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

【问题讨论】:

    标签: perl email mime email-attachments


    【解决方案1】:

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

    如果您有 $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)
    

    所以整个消息是多部分/混合类型,它包含: 1. multipart/alternative 有两个可选消息(大概是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( ... );
    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-16
      • 1970-01-01
      • 2013-02-24
      • 1970-01-01
      • 2018-07-17
      • 1970-01-01
      • 2011-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多