Perl发送HTML格式的邮件

(0 comments)

先建立一个HTML文件,然后在发送邮件时,指定邮件格式为HTML,邮件内容读取HTML文件。

#! /usr/local/bin/perl -w
 
use Mail::Sender;
 
$htmlfile = "sign.htm";
open IN, $htmlfile or die "Cannot open $htmlfile : $!\n";
$sender = new Mail::Sender {smtp => 'smtp.liaojl.com'};
$sender->Open({ from => 'liaojl@liaojl.com', to => 'liaojl@liaojl.com', subject => 'HTML test',
       headers => "MIME-Version: 1.0\r\nContent-type: text/html\r\nContent-Transfer-Encoding: 7bit"
}) or die $Mail::Sender::Error,"\n";
 
while () { $sender->Send($_) };
close IN;
$sender->Close();
__END__

在HTML内容中包含图片:


#! /usr/local/bin/perl -w
 
use Mail::Sender;
 
my $recipients = 'liaojl@liaojl.com';
my $sender = new Mail::Sender {smtp => 'smtp.liaojl.com'};
if ($sender->OpenMultipart({from => 'liaojl@liaojl.com', to => $recipients,
                      subject => 'Embedded Image Test', subtype => 'related',
                      boundary => 'boundary-test-1',
                      type => 'multipart/related'}) > 0) {
 $sender->Attach(
        {description => 'html body',
        ctype => 'text/html; charset=us-ascii',
        encoding => '7bit',
        disposition => 'NONE',
        file => 'sign.htm'
  });
 $sender->Attach(
  {description => 'ed\'s jpeg',
   ctype => 'image/jpeg',
   encoding => 'base64',
   disposition => "inline; filename=\"redhat.jpg\";\r\nContent-ID: ",
   file => 'redhat.jpg'
  });
 $sender->Close() or die "Close failed! $Mail::Sender::Error\n";
} else {
 die "Cannot send mail: $Mail::Sender::Error\n";
}

在HTML文件中添加图片的引用,注意引用的名字并不是文件名,而是Content-ID。

<IMG src="cid:redhat-jpg">
Currently unrated

Comments

There are currently no comments

New Comment

required

required (not published)

optional

required