Monday, June 18, 2007

Perl下如何安装一个模块和使用smtp发送邮件

Active Perl和Cpan的Perl有些不一样,安装的时候使用ppm,具体的操作即在命令行模式下运行ppm

启动ppm后使用命令search xxx 模块名搜索模块,如果网络连接正常,过会就会返回找到的模块名称,我前面找的是sasl。
找到后运行 install authen-sasl,active perl会自动下载需要的文件并完成安装,看到Successfully installed authen-sasl version 2.10 in ActivePerl 5.8.3.809.
即成功完成安装。
下面是发送smtp邮件的方法
use Net::SMTP;
my $MailHost = "mailhost";
my $MailFrom = "xxxx\@mailhost.com";
my $MailTo = "xxxx\@mailhost.com";
my $subject = "Subject Line here";
my $MailBody = "This is the mail body";
my $smtp = Net::SMTP->new($MailHost) or die "Can't create smtp!";
$smtp->auth('user','pass');
# Send the From and Recipient for the mail servers that require it$smtp->mail($MailFrom); $smtp->to($MailTo);
# Start the mail$smtp->data();
# Send the header. $smtp->datasend("To: $MailTo\n"); $smtp->datasend("From: $MailFrom\n"); $smtp->datasend("Subject: $subject\n"); $smtp->datasend("\n");
# Send the message $smtp->datasend("$MailBody\n\n");
# Send the termination string $smtp->dataend();
$smtp->quit;

No comments: