2006-7-5 16:58ChennyskyPerl 发送邮件(可带附件)的脚本!
最近看到大家讨论 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
#*****************************************************************************#
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";
if ('xxxxxx.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;
}
调用方法:
our $mailFrom = ''; #Send email address
our $password = 'xxxxxx';
my @mailTo = (''); #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);
我用公司的邮箱可以发送邮件,可是用外网的邮箱,不能成功。
还请大家继续讨论!
谢谢
2006-7-6 10:52莫愁可能是没有加认证的原因。
建议你研究一下 Net::SMTP 的 auth 函数,看能不能认证,如果不行的话,强烈推荐用 Mail::Sender,效果非常好。
2006-7-6 17:06Chennysky# anth login, type your user name and password here
$smtp->auth($userName,$password)||print "Auth Error!\n";
加过了认证的,用公司的邮箱可以认证成功,且发送邮件成功,可是用外网的邮箱就有问题了,呵呵
我其实可以回家用自己家里的计算机试试看的,等有结果了再讨论!
2006-7-12 10:06apile这就表示你们公司的mailserver关了mail relay功能....
他应该有限制..除了特殊IP可以外..其他都不行.....
详细mailserver设定...我不清楚...
但是我之前是有打电话跟管mailserver的人讲..才能送email出去..
2006-7-12 11:28Chennysky哦,这样啊
可是像 foxmail,outlook是什么原理呢?这些都可以发邮件的
我回家用自己家里的计算机试了,我的函数通过不了,报错
继续调试中
2006-7-14 14:33zhurx
$smtp->auth($userName,$password)||print "Auth Error!\n";
这里改为
$smtp->auth($userName,$password)||print "$!\n";
看看
我也推荐Mail::Sender,如果使用Mail::Sender,可以添加debug参数,这样无论成功与失败都能清楚的看到原因。
open my $DEBUG, ">> /path/to/debug/file.txt"
or die "Can't open the debug file: $!\n"
$sender = new Mail::Sender ({
...
debug => $DEBUG,
});
2009-6-2 21:32finixlei多谢10楼。晚上研究了一下Mail::Sender,还真能发附件。代码如下:
use strict;
use Mail::Sender;
my $sender = new Mail::Sender{smtp => 'smtp.163.com', from => 'myname_1@163.com'};
$sender->OpenMultipart({to => 'myname_2@gmail.com',
subject => 'Send Attachment!',
auth => 'LOGIN',
authid => 'myname_1',
authpwd => 'mypasswd_1',});
$sender->Attach(
{description => 'Perl module Mail::Sender.pm',
ctype => 'application/x-zip-encoded',
encoding => 'Base64',
disposition => 'attachment; filename="111.rar"; type="ZIP archive"',
file => '111.rar'
});
$sender->Close;
#### The following is to send just a text mail ####
# if ($sender->MailMsg({
# smtp => 'smtp.163.com',
# from => 'myname_1@163.com',
# to =>'myname_2@gmail.com',
# subject => 'Test Attachment',
# msg => "Hi, Attachment!",
# auth => 'LOGIN',
# authid => 'myname_1',
# authpwd => 'mypasswd_1',
# }) < 0) {
# die "$Mail::Sender::Error\n";
# }
print "Mail sent OK.";
1;
该篇文章发表在:百家学院 (http://www.9php.com),获取更多同类文章请访问以上网站.
最近看到大家讨论 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
#*****************************************************************************#
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";
if ('xxxxxx.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;
}
调用方法:
our $mailFrom = ''; #Send email address
our $password = 'xxxxxx';
my @mailTo = (''); #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);
我用公司的邮箱可以发送邮件,可是用外网的邮箱,不能成功。
还请大家继续讨论!
谢谢
2006-7-6 10:52莫愁可能是没有加认证的原因。
建议你研究一下 Net::SMTP 的 auth 函数,看能不能认证,如果不行的话,强烈推荐用 Mail::Sender,效果非常好。
2006-7-6 17:06Chennysky# anth login, type your user name and password here
$smtp->auth($userName,$password)||print "Auth Error!\n";
加过了认证的,用公司的邮箱可以认证成功,且发送邮件成功,可是用外网的邮箱就有问题了,呵呵
我其实可以回家用自己家里的计算机试试看的,等有结果了再讨论!
2006-7-12 10:06apile这就表示你们公司的mailserver关了mail relay功能....
他应该有限制..除了特殊IP可以外..其他都不行.....
详细mailserver设定...我不清楚...
但是我之前是有打电话跟管mailserver的人讲..才能送email出去..
2006-7-12 11:28Chennysky哦,这样啊
可是像 foxmail,outlook是什么原理呢?这些都可以发邮件的
我回家用自己家里的计算机试了,我的函数通过不了,报错
继续调试中
2006-7-14 14:33zhurx
原帖由 莫愁 于 2006-7-6 10:52 发表 可能是没有加认证的原因。 建议你研究一下 Net::SMTP 的 auth 函数,看能不能认证,如果不行的话,强烈推荐用 Mail::Sender,效果非常好。 [/quote] 写一个例子阿 NEt:SMTP的auth函数认证通不过。。。。。。 2006-7-27 17:52laputa73用SendMail包可以 my $sm = new SendMail($mailserver); $sm->setAuth($sm->AUTHLOGIN, $mail1, $mailpasswd); $sm->From($mailuser); $sm->Subject($headers); $sm->To(@mailaddr); $sm->setMailBody($mailcontent); $sm->Attach($file); $sm->sendMail(); 很简单啊 2009-6-2 09:16finixlei请问有谁知道这个SendMail的包的全名是什么啊?在CPan上搜了一下,好像有好多。但是这一个看起来使用非常简单。不好意思,把老帖子顶上来了。 2009-6-2 09:17finixlei8楼所说是指7楼。 8楼所说是指7楼。 2009-6-2 13:47xti9er[quote]原帖由 zhurx 于 2006-7-14 14:33 发表 ![]() 写一个例子阿 NEt:SMTP的auth函数认证通不过。。。。。。 |
$smtp->auth($userName,$password)||print "Auth Error!\n";
这里改为
$smtp->auth($userName,$password)||print "$!\n";
看看
我也推荐Mail::Sender,如果使用Mail::Sender,可以添加debug参数,这样无论成功与失败都能清楚的看到原因。
open my $DEBUG, ">> /path/to/debug/file.txt"
or die "Can't open the debug file: $!\n"
$sender = new Mail::Sender ({
...
debug => $DEBUG,
});
2009-6-2 21:32finixlei多谢10楼。晚上研究了一下Mail::Sender,还真能发附件。代码如下:
use strict;
use Mail::Sender;
my $sender = new Mail::Sender{smtp => 'smtp.163.com', from => 'myname_1@163.com'};
$sender->OpenMultipart({to => 'myname_2@gmail.com',
subject => 'Send Attachment!',
auth => 'LOGIN',
authid => 'myname_1',
authpwd => 'mypasswd_1',});
$sender->Attach(
{description => 'Perl module Mail::Sender.pm',
ctype => 'application/x-zip-encoded',
encoding => 'Base64',
disposition => 'attachment; filename="111.rar"; type="ZIP archive"',
file => '111.rar'
});
$sender->Close;
#### The following is to send just a text mail ####
# if ($sender->MailMsg({
# smtp => 'smtp.163.com',
# from => 'myname_1@163.com',
# to =>'myname_2@gmail.com',
# subject => 'Test Attachment',
# msg => "Hi, Attachment!",
# auth => 'LOGIN',
# authid => 'myname_1',
# authpwd => 'mypasswd_1',
# }) < 0) {
# die "$Mail::Sender::Error\n";
# }
print "Mail sent OK.";
1;
该篇文章发表在:百家学院 (http://www.9php.com),获取更多同类文章请访问以上网站.