免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3830 | 回复: 8
打印 上一主题 下一主题

[perl] 请教 发邮件 的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-07-06 09:53 |只看该作者 |倒序浏览
在写一个perl脚本,发生错误时需要发送邮件,请问大家常用的perl发邮件的模块是哪个啊?
谢谢指教。

论坛徽章:
0
2 [报告]
发表于 2009-07-06 10:04 |只看该作者

论坛徽章:
0
3 [报告]
发表于 2009-07-06 10:07 |只看该作者
Mail::Sender

论坛徽章:
0
4 [报告]
发表于 2009-07-06 10:22 |只看该作者
Mail::Sender

论坛徽章:
0
5 [报告]
发表于 2009-07-06 12:22 |只看该作者
几天前刚写的一个。

#!/usr/bin/perl
use strict;
use warnings;
use Net::SMTP;

if($#ARGV < 2) {
    print "$#ARGV\n";
    print "Usage: $0 <Subject> <MailFrom> <MailTo> [MailContentFile]>\n";
    print "--------------------Sample---------------------------------\n";
    print "$0 \"Results\" \"xxx\@mail.xxx.com\" \"xxx\@mail.xxx.com,abc\@mail.xxx.com\" \"status.out\"\n";
    print "cat status.out | grep -v OK | $0 \"Results\" \"xxx\@mail.xxx.com\" \"xxx\@mail.xxx.com,abc\@mail.xxx.com\"\n";
    print "\n";
    exit 0;
}

my($subject, $mailfrom, $mailto_str, $content_file) = @ARGV;
print("subject=",$subject,"\n";
print("mailfrom=",$mailfrom,"\n";
print("subject=",$mailto_str,"\n";
shift(@ARGV); shift(@ARGV); shift(@ARGV);

my $text = "";
my $line = "";
while ($line = <> {
  $text=$text.$line;
}
print($text,"\n",);

my $mailhost='smtp.mail.xxx.com';
my $mailcc='xxx/@mail.xxx.com';

my @mailto = split (/,/, $mailto_str);

my $smtp=Net::SMTP->new($mailhost, Timeout=>120, Debug=>1) or die "Error.\n";
$smtp->mail($mailfrom);
$smtp->recipient(@mailto);
#$smtp->to($mailto);
$smtp->cc($mailcc);
$smtp->data();
$smtp->datasend("To: $mailto_str\n";
$smtp->datasend("Cc: $mailcc\n";
$smtp->datasend("Frommailfrom\n";
$smtp->datasend("Subject: $subject\n";
$smtp->datasend("\n";
$smtp->datasend("$text\n\n";
$smtp->dataend();

$smtp->quit;

刚学习perl不久,代码比较简陋,呵呵。

论坛徽章:
0
6 [报告]
发表于 2009-07-06 12:49 |只看该作者
How do I send mail?

Use the sendmail program directly:

  1.     open(SENDMAIL, "|/usr/lib/sendmail -oi -t -odq")
  2.                         or die "Can't fork for sendmail: $!\n";
  3.     print SENDMAIL <<"EOF";
  4.     From: User Originating Mail <me\@host>
  5.     To: Final Destination <you\@otherhost>
  6.     Subject: A relevant subject line

  7.     Body of the message goes here after the blank line
  8.     in as many lines as you like.
  9.     EOF
  10.     close(SENDMAIL)     or warn "sendmail didn't close nicely";
复制代码


The -oi option prevents sendmail from interpreting a line consisting of a single dot as "end of message". The -t option says to use the headers to decide who to send the message to, and -odq says to put the message into the queue. This last option means your message won't be immediately delivered, so leave it out if you want immediate delivery.

Alternate, less convenient approaches include calling mail (sometimes called mailx) directly or simply opening up port 25 have having an intimate conversation between just you and the remote SMTP daemon, probably sendmail.

Or you might be able use the CPAN module Mail::Mailer:

  1.     use Mail::Mailer;

  2.     $mailer = Mail::Mailer->new();
  3.     $mailer->open({ From    => $from_address,
  4.                     To      => $to_address,
  5.                     Subject => $subject,
  6.                   })
  7.         or die "Can't open: $!\n";
  8.     print $mailer $body;
  9.     $mailer->close();
复制代码


The Mail::Internet module uses Net::SMTP which is less Unix-centric than Mail::Mailer, but less reliable. Avoid raw SMTP commands. There are many reasons to use a mail transport agent like sendmail. These include queuing, MX records, and security.

[ 本帖最后由 Perl_Er 于 2009-7-6 13:16 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2009-07-06 13:58 |只看该作者
<code>
#!/usr/bin/perl
use strict;
use warnings;
use Net::SMTP;
use Authen::SASL;
my $smtp = Net::SMTP->new('smtp.163.com',
                           Hello => 'smtp.163.com',
                           Timeout => 30,
                           Debug   => 1,
                           );
#authentication
$smtp->auth("username_in_plain_text","password_in_plain_text");
#mail from
$smtp->mail("username\@163.com");
#rcpt to
$smtp->to("recipient\@sina.com");
#mail data
$smtp->data();
$smtp->datasend("To: recipient\n");
$smtp->datasend("\n");
$smtp->datasend("Line1\nLine2\n");
$smtp->dataend();
#close socket;disconnect from smtp server
$smtp->quit;
</code>
Net::SMTP提交用户名和密码时,需要使用Authen::SASL模块。在
http://cpansearch.perl.org/src/GBARR/libnet-1.22/Net/SMTP.pm
代码中可以看到。
很简洁。

论坛徽章:
1
未羊
日期:2014-09-08 22:47:27
8 [报告]
发表于 2009-07-07 20:49 |只看该作者
也可以使用MIME::Lite模块。

论坛徽章:
0
9 [报告]
发表于 2009-07-07 23:54 |只看该作者
Net SMTP好用
我一般用这个
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP