[英]Perl MIME::Lite nested boundaries
我正在缺少許多Perl庫的服務器上工作,所以我只能使用MIME :: Lite。 是的,我應該使用其他方式,但這不在我的控制范圍內。
總體而言,電子郵件軟件包需要“多部分/混合”。 它將具有純文本版本,html版本和附件。 問題是,它在電子郵件正文中同時顯示純文本和html版本。
我仍想支持“ multipart / alternative”,以便顯示純文本或html。 但是,由於我需要附件,因此整個電子郵件應為“ multipart / mixed”,並在文本/ html版本中帶有“ multipart / alternative”的內部邊界部分。
我雖然可以正常使用,但是由於整個電子郵件程序包都是“ multipart / alternative”,因此它掉下了附件。 檢查我的收件箱中的一些電子郵件,我注意到您可以先進行“混合”,然后在其中進行“替代”。 就像div中的div :)
這是我當前的代碼片段(請查看定義邊界的注釋):
my $type = 'multipart/mixed';
sub sendMail {
my($from, $to, $subject, $messageHtml, $messageText, $type, %attachment) = @_;
my $msg = MIME::Lite->new(
From => $from,
To => $to,
Subject => $subject,
Type => $type, # multipart/mixed
);
# -- start new boundary and multipart/alternative
if ( $messageText ne '' ) {
$msg->attach(
Type => 'text/plain; charset=UTF-8',
Encoding => 'quoted-printable',
Data => $messageText,
);
}
if ( $messageHtml ne '' ) {
$msg->attach(
Type => 'text/html; charset=UTF-8',
Encoding => 'quoted-printable',
Data => $messageHtml,
);
}
# -- end new boundary
if ( %attachment ) {
$msg->attach(
Type => $attachment{"type"},
Encoding => 'base64',
Path => $attachment{"path"},
Filename => $attachment{"filename"},
Disposition => 'attachment'
);
}
if ($debugmode) {
my $result = '';
$msg->print($result);
print $result;
exit;
}
if ($msg->send) {
return 1;
}
return 0;
}
我將修改代碼以檢查是否同時傳遞了html和純文本。 我很清楚,如果僅使用其中之一,則不需要替代方法。
同樣,我需要純文本和html部分,將其作為“ multipart / alternative”置於其邊界內
您必須首先將消息創建為multipart/mixed
。 在此消息內,您需要將內部零件創建為multipart/alternative
。 在此內部部分上,您應該附加HTML和文本部分。 並且附件應添加到外部(混合)部分。 這可以通過如下代碼實現:
use strict;
use warnings;
use MIME::Lite;
# main message is multipart/mixed
my $msg = MIME::Lite->new(
From => 'me@example.com',
To => 'you@example.com',
Subject => 'some test message',
Type => 'multipart/mixed'
);
# inner part as multipart/alternative to include both HTML and text
my $alternative = $msg->attach(
Type => 'multipart/alternative',
);
$alternative->attach(
Type => 'text/plain',
Data => 'some TEXT here'
);
$alternative->attach(
Type => 'text/html',
Data => '<b>some HTML here</b>'
);
# attached image goes to main message
$msg->attach(
Type => 'image/gif',
Data => 'GIF89a....'
);
print $msg->as_string;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.