2012-04-10 65 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。正如您所看到的,只需添加一個電子郵件鏈接和一個圖像就需要比我們大多數人想要做的更多的工作。