#cat send_mail.pl
#!/usr/bin/perl -w
#Author:duhui
#Date :2010-11-29
#perl -MCPAN -e shell
#cpan>install Net::SMTP_auth
use Net::SMTP;
use Net::SMTP_auth;
use MIME::Base64;
$mail_server = 'smtp.163.com';
$mail_from = 'dh03@163.com';
$mail_to = 'duhui@goso.cn';
$uname='XXX';
$passwd='XXX';
#开启Debug模式
$smtp = Net::SMTP->new("$mail_server" , Debug => 1);
#普通发送模式
#$smtp = Net::SMTP->new("$mail_server" );
$smtp->auth("$uname","$passwd");
$smtp->mail("$mail_from");
$smtp->to("$mail_to");
$smtp->data();
$smtp->datasend("from:$mail_from");
$smtp->datasend("To:$mail_to");
$smtp->datasend("Reply-To:$mail_from");
$smtp->datasend("Return-Path:$mail_from");
$smtp->datasend('Subject:Your Subject');
$smtp->datasend('Content-Type:text/plain; ChartSet=gb2312');
$smtp->datasend('Your mail content!');
$smtp->datasend('/n');
$smtp->quit;
另外一种实现方式 可以发送附件的
#*****************************************************************************#
# Function Name:
# SendMail($$/@$$/@)
# Description:
# Use Net::SMTP and MIME::Lite to send Email with attachment.
# Argument:
# 1: Sending Email address
# 2: Password of sending Email.
# 3: The recipient Email array
# 4: The subject of the Email
# 5: The content of the Email
# 6: The attachments array of the Email
# Return:
# None
#*****************************************************************************#
sub SendMail($$/@$$/@)
{
use Net::SMTP;
use MIME::Lite;
my ($mailFrom, $password, $mailToRef, $subject, $content, $attachmentRef)=@_;
my ($userName,$mailHost) = split(//@/, $mailFrom);
my $helloPara = $mailHost;
# print "The Email user name is: $userName/n";
# print "The mailHost is: $mailHost/n";
print $mailHost;
if ('163.com.cn' ne $mailHost)
{
$mailHost = "smtp.".$mailHost;
# print "The mailHost is: $mailHost/n";
} #The smtp host
my @mailTo = @$mailToRef; #The recipient list
for(my $i=0; $i<=$#mailTo; $i++) {
print "$#mailTo/n";
print "$mailTo[$i]/n"; }
my @attachment = @$attachmentRef; #The attachments
my $smtp = Net::SMTP->new($mailHost, Hello => $helloPara, Timeout => 120, Debug => 1)
||die 'Cannot connect to server /'$mailHost/'';
# anth login, type your user name and password here
$smtp->auth($userName,$password)||print "Auth Error!/n";
foreach my $mailTo (@mailTo)
{
# Create a new multipart message:
my $msg = MIME::Lite->new(
From => $mailFrom,
To => $mailTo,
Subject => $subject,
Type =>'multipart/mixed',
)or print "Error creating MIME body: $!/n";
# Add parts:
$msg->attach(Type =>'TEXT',
Data => $content,
);
foreach my $attachment (@attachment)
{
$msg->attach(
Type => 'AUTO', # the attachment mime type
Path => $attachment, # local address of the attachment
)or print "Error attaching test file: $!/n";
}
my $str = $msg->as_string() or print "Convert the message as a string: $!/n";
# 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 message
$smtp->datasend("$str");
# Send the termination string
$smtp->dataend();
}
$smtp->quit;
return;
}
my $drvTestReport="/data/test";
our $mailFrom = 'dh03@163.com'; #Send email address
our $password = 'XXX';
my @mailTo = ('duhui@goso.cn'); #The recipient list
my $mailSubject = "Driver Test Result";
my $mailContent = "The attachment is the $mailSubject!/n
Please check it./nThanks!/n/n Chenny.chen/n";
my @mailAttachment = ($drvTestReport); #The attachments for the test result
SendMail($mailFrom, $password, @mailTo, $mailSubject, $mailContent, @mailAttachment);