天天看點

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伺服器上發送郵件

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->datasend("to: $mailto\n");

$smtp->datasend("from:$mailfrom\n");

$smtp->datasend("subject: $subject\n");

$smtp->datasend("$text\n\n");

}

轉載于:http://blog.chinaunix.net/uid-10449864-id-2956766.html