perl 发邮件

1, 在不需要认证的smtp服务器上发送邮件

#!/usr/bin/perl -w
use Net::SMTP;

$smtp = Net::SMTP->new($mail_server);    #邮件服务器地址
$smtp->mail($send_user);                 #发件人
$smtp->to($recv_user);                   #收件人

$smtp->data();
$smtp->datasend("Subject: 测试主题\n");   #主题
$smtp->datasend("\n");
$smtp->datasend("A simple test message 测试中英文混合\n");    #消息内容
$smtp->dataend();

$smtp->quit;


2, 在需要认证的smtp服务器上发送邮件

#!/usr/bin/perl -w

use strict;
use Net::SMTP_auth;

my $mailhost = 'mail.abc.cn';
my $mailfrom = '[email protected]';
my @mailto = ('[email protected]',
              '[email protected]');

my $user = '[email protected]';
my $passwd = '123456';

my $subject = 'hello';

#my $text

my $f_list = '/home/sysadmin/scp.log';
open(FILE, $f_list) or die "Can not open list file\n";
undef $/;
my $text = <FILE>;


my $smtp = Net::SMTP_auth->new($mailhost, Timeout=>120, Debug => 1) or die "Error.\n";
$smtp->auth('LOGIN', $user, $passwd);

foreach my $mailto(@mailto) {
$smtp->mail($mailfrom);
$smtp->to($mailto);
$smtp->data();
$smtp->datasend("To: $mailto\n");
$smtp->datasend("From:$mailfrom\n");
$smtp->datasend("Subject: $subject\n");
$smtp->datasend("\n");
$smtp->datasend("$text\n\n");
$smtp->dataend();
}

$smtp->quit;

转载于:http://blog.chinaunix.net/uid-10449864-id-2956766.html

你可能感兴趣的:(perl,smtp)