- 论坛徽章:
- 0
|
2楼
发表于 2011-09-27 13:19
这个是我另一个脚本,总体上都是一样的,只是需要在运行时手动输入发件人,密码,主题等信息。
如:sendmail.pl XXXX@XXX.com.cn 123456 "test test"
#!/usr/local/bin/perl
#*****************************************************************************#
# 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
#*****************************************************************************#
use Net::SMTP;
use MIME::Lite;
my ($mailFrom, $password, @mailTo, $mailSubject, $mailContent, @mailAttachment);
#die "Syntax: $0 <mailFrom> <password> <mailTo> <Subject> <Content> <Attachment>\n" if $#ARGV != 5;
#our $mailFrom = $ARGV[0]; #Send email address
#print "mailFrom is $mailFrom\n";
$mailFrom = $ARGV[0];
print "$mailFrom\n";
$password = $ARGV[1];
@mailTo = split(",",$ARGV[2]); #The recipient list
print "$ARGV[2]\n";
print "mailTo are @mailTo";
print "\n";
$mailSubject = $ARGV[3];
print "mailSubject is $mailSubject\n";
$mailContent = $ARGV[4];
print "mailContent is $mailContent\n";
@mailAttachment = split(";",$ARGV[5]); #The attachments for the test result
print "mailAttachment are @mailAttachment";
print "\n";
#SendMail($mailFrom, $password, @mailTo, $mailSubject, $mailContent, @mailAttachment);
#sub SendMail($$\@$$\@)
#{
#use Net::SMTP;
#use MIME::Lite;
#my ($mailFrom, $password, $mailToRef, $subject, $content, $attachmentRef)=@_;
my ($userName,$mailHost) = split(/\@/, $mailFrom);
#print "The Email user name is: $userName\n";
#print "The mailHost is: $mailHost\n";
$mailHost = "mail.".$mailHost;
my $helloPara = $mailHost;
print "The Email user name is: $userName\n";
print "The mailHost is: $mailHost\n";
#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 => $mailSubject,
Type =>'multipart/mixed',
)or print "Error creating MIME body: $!\n";
# Add parts:
$msg->attach(Type =>'TEXT',
Data => $mailContent,
);
foreach my $attachment (@mailAttachment)
{
$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;
#} |
|