2012-04-10 67 views
0

如何使用sendmail将图像和超链接添加到perl中的电子邮件?带有超链接和嵌入图像的perl sendmail

这是我想在$消息体(可变):

文件:filename.jpg
(添加的形象在这里在线)

一直proccess你可以找到它: add link here`

这里是我的代码:
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);

回答

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::Lite模块MIME Primer。正如您所看到的,只需添加一个电子邮件链接和一个图像就需要比我们大多数人想要做的更多的工作。