0

如何使用 sendmail 在 perl 中添加图像和超链接到电子邮件?

这是我在 $message (变量)中想要的正文:

您的文件:filename.jpg
(在此处内联添加图像)

已处理,您可以在以下位置找到它:在此处添加链接 `

这是我的代码:
sub sendEmail
{
my ($to, $from, $subject, $message) = @_;
my $sendmail = '/usr/lib/sendmail';
open(MAIL, "|$sendmail -oi -t");
print MAIL "From: $from\n";
print MAIL "To: $to\n";
print MAIL "Subject: $subject\n\n";
print MAIL "$message\n";
close(MAIL);
}

sendEmail($receiver, 'admin, 'your file has been Synchronized', $message);

4

2 回答 2

2

您可能需要使用允许您创建 MIME 附件的 Perl 模块。CPAN 上应该有一堆,例如 MIME::Lite。请参阅以下讨论:

http://www.revsys.com/writings/perl/sending-email-with-perl.html

于 2012-04-10T13:22:13.867 回答
2

如果您只想在短信中提供一个简单的链接,您可能会认为不值得为此付出痛苦和努力。

但是,这里有一个简短的概要:

use MIME::Lite;
[...]

# First Create your message...
my $message = MIME::Lite->new(
    From    => $from_email,
    To      => $to_email,
    Cc      => join(", " => @cc_addresses),
    Subject => $subject,
    Type    => 'multipart/related',
);

# Now, we have to attach the message in HTML. First the HTML
my $html_message = <<"EOM";
<body>
    <p> Your File: <img src='cid:my_image.gif'/> has been processed
        and can be found <a href="$file_url">here</a>.</p>
</body>
EOM;

# Now define the attachment
$message->attach (
    Type => 'text/html',
    Data => $html_message,
);

# Let's not forget to attach the image too!
$message->attach (
    Type => 'image/gif',
    Id   => 'my_image.gif',
    Path => $file_name,
);

$message->send
    or die qq(Message wasn't sent: $!\n);

看看模块中包含的MIME Primer 。MIME::Lite如您所见,简单地添加一个电子邮件链接和一个图像需要比我们大多数人想做的更多的工作。

于 2012-04-10T18:02:03.353 回答