perl利用SMTP发送邮件脚本(带认证)

#!/usr/bin/perl -w
use strict;
use Net::SMTP;
use Authen::SASL;(在SMTP服务器上要求用户认证,必须的模块)
BEGIN {
push (@INC,'/usr/lib/perl5/5.8.8/Net/ ');

}

my $from = '[email protected]';
my $to = '[email protected]';
my $username = 'huashan125';
my $passwd = 'huashan125';
my $subject = 'Test Email';
my $smtp = Net::SMTP->new('192.168.21.82');

$smtp->auth($username,$passwd) or die "Could not authenticate $!";(这三句的顺序)
$smtp->mail($from);
$smtp->to($to);

$smtp->data();
$smtp->datasend("From:$from/n");
$smtp->datasend("To: $to/n");
#$smtp->datasend("Cc: $Cc/n");
$smtp->datasend("Subject: $subject/n");
$smtp->datasend("This is a test perl email/nIt is successfull");
$smtp->dataend();

$smtp->quit;

---------------------------------------------------以下是自己写的例子--------------------------------------

#!/usr/bin/perl -w
use Net::SMTP;
$smtp = Net::SMTP->new('10.1.28.22');    #邮件服务器地址
$smtp->mail('[email protected]');                 #发件人
$smtp->to('[email protected]');                   #收件人
$smtp->to('[email protected]');
$smtp->data();
$smtp->datasend("From:/"zhang/" /n");
$smtp->datasend("To: /"zhang/" ,/n /"zhang/" ,/n" );

$smtp->datasend("Subject: monitor_prov/n/n");   #主题

my $my_url = 'http://127.0.0.1:8081/div.html' . "/n";
#my $my_url = 'monitor_info
' . "/n";
$smtp->datasend($my_url);
$smtp->datasend("test mail/n");
$smtp->dataend();
$smtp->quit;

你可能感兴趣的:(Perl)