【问题标题】:Email attachments with `MIME::Lite::TT::HTML`带有“MIME::Lite::TT::HTML”的电子邮件附件
【发布时间】:2011-08-22 07:31:00
【问题描述】:

我正在尝试使用MIME::Lite::TT::HTML 发送电子邮件。它很好用,但我不知道如何将它与附件一起使用。

我试过了:

$msg->attr("content-type"  => "multipart/mixed"); 
$msg->attach(Type => $mime,
             Path => $attachment,
             Filename => $name, );

但它破坏了信息。 HTML 和 TXT 部分不再被解释为同一个东西,因此内容会显示两次。

那么使用MIME::Lite::TT::HTML添加附件的正确方法是什么?

【问题讨论】:

  • 你是如何创建$msg的?我认为您最好不要在创建消息后直接使用 Content-Type 。只需创建一条新消息,附加您要附加的内容,然后让库负责提供合适的类型。
  • @tripleee 好吧,我使用的代码几乎相同,在文档中(减去编码内容)search.cpan.org/~chunzi/MIME-Lite-TT-HTML-0.03/lib/MIME/Lite/TT/…
  • 那么如果你省略$msg->attr() 调用,你会得到更好的结果吗?
  • @tripleee 没有。如果我不设置multipart/mixed,则附件根本不会发送。
  • 那你真的需要TT的东西吗?似乎你可能不得不解决它的限制,也许通过使用裸 MIME::Lite。

标签: perl email


【解决方案1】:

不幸的是,一封电子邮件似乎不能同时包含替代部分和附件。

我选择了MINE::Lite::TT,这似乎与代码配合得很好。暂时不会有任何 HTML 电子邮件:-/

【讨论】:

  • 你错了,通过正确嵌套 MIME 部分,MIME 消息可以同时具有 multipart/mixed 和 multipart/alternative。如果您正在寻找现代电子邮件创建模块:p3rl.org/Courriel::Builder p3rl.org/Email::MIME::Kit 可能还有 Email::MIME:: 命名空间中的其他模块。
【解决方案2】:

我在附加 PDF 文件和带有动态图像和字段(姓名、职业)的模板 HTML 时遇到了类似的问题,我还使用了 MIME::Lite::TT::HTML 和其他库。

问题是,如果你没有分离模板或者你没有包含库HTML::Template;,它会在电子邮件正文中发送一个文本和html文件并带有附件。该模板仅使用“text/html”。

这是工作代码的 sn-p: 只需更改变量以满足您的需要:

模板 html - test2.html

<html>
    <head>
        <title><TMPL_VAR NAME=profession></title>
    </head>
<body>
<table>
<tbody>
    <tr>
        <td>
            <img src=<TMPL_VAR NAME=photo> alt="my photo" width="700"/>
        </td>
    </tr>
    <tr >
        <td>
            <TMPL_VAR NAME=applicantname>
        </td>
    </tr>
</tbody>
</TABLE>
</body>
</html>

以下是:email.pl

use strict;
use warnings;
use MIME::Lite::TT::HTML;
#use MIME::Base64 qw( encode_base64 );
use Authen::SASL;
use Net::SSL;
use HTML::Template;

my $from_address = 'no-reply@abc.com';
my $to_address = 'to@address.com';
my $mail_host = '';
my $mail_user = '';
my $mail_password = '';
my $mail_port = 25;

my $template = HTML::Template->new(filename => 'test2.html');
$template->param(applicantname => "Applicant name");
$template->param(profession => "Designer");
$template->param(photo => "photo location or url path");

my $msg = MIME::Lite->new(
   From =>     $from_address,
   To =>       $to_address,
   Subject =>  'Applicant Resume',
   Type =>     'text/html',
   Data =>     $template->output()
);
$msg->attach (
   Type => 'application/pdf',
   Path => 'C:\Users\downloadfiles/filetest.pdf',
   Filename => 'filetest.pdf',
   Disposition => 'attachment'
) or die "Error adding : $!\n";

$msg->send('smtp',$mail_host,HELLO=>$mail_host,PORT=>$mail_port,AuthUser=>$mail_user,AuthPass=>$mail_password,Timeout=>60);

【讨论】:

    【解决方案3】:

    我认为 mime-type 应该是 multipart/alternative

    【讨论】:

    • 如果我设置multipart/alternative,那么附件将被完全跳过。
    猜你喜欢
    • 2018-05-12
    • 2014-08-21
    • 2011-04-14
    • 2018-01-16
    • 2011-12-11
    • 2011-12-30
    • 2010-10-30
    • 2013-11-26
    • 2021-11-05
    相关资源
    最近更新 更多