Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5359242
  • 博文数量: 1144
  • 博客积分: 11974
  • 博客等级: 上将
  • 技术积分: 12312
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-13 20:06
文章存档

2017年(2)

2016年(14)

2015年(10)

2014年(28)

2013年(23)

2012年(29)

2011年(53)

2010年(86)

2009年(83)

2008年(43)

2007年(153)

2006年(575)

2005年(45)

分类: PERL

2015-10-22 16:58:02

perl发信模块MailSender这个模块基本实现了日常运维中常用的发信功能,比如多人邮件,抄送,密送,smtp认证,发送带附件的邮件,html格式的邮件等。

首先,使用MailMsg方法发信
 
代码如下:
#!/usr/bin/perl
 #
 # file  wo.pl
 #
 use strict;
 use warnings;

use MailSender;

my $sender = new MailSender{
              smtp = 'smtp.163.com',
              from = 'test@163.com',
              on_errors = 'die',
 }
   or die Can't create the MailSender object $MailSenderErrorn;

# first by MailMsg
 if($sender-MailMsg({
   to = 'test@sina.com',
   subject = 'this is a test',
   msg = Hi toplover. n How are you,
   auth = 'LOGIN',
   authid = 'test',
   authpwd = 'sinapwd',
 })0){
   die $MailSenderError n;
 }
 print Mail sent OK. n;

第二部分,使用SendLineEnc输入多行内容

代码如下:
# second SendLineEnc
 $sender-Open({
    to = 'test@163.com',
    cc = 'test@sina.com',
    Bcc = 'test@sina.com',
    subject = 'Sorry, I'll come later.',
 })
   or die Can't open the message $sender-{'error_msg'}n;

$sender-SendLineEnc(I'm sorry, but thanks to the luseers,
         I'll come at 10pm at best.);
 $sender-SendLineEnc(nHi, Toplover@);
 #$sender-Close()
 #  or die Failed to send message $sender-{'error_msg'}n;

第三部分,使用GetHandle输入多行内容

代码如下:
# three GetHandle
 $sender-Open({to ='test@163.com',subject ='Hello dear friend.'})
   or die Error $MailSenderError n;
 my $FH = $sender-GetHandle();
 print $FH How are you  nn;
 print $FH 'END';
 I've found these jokes.

   Doctor, I feel like a pack of cards.
    Sit down and I'll deal with you later.

   Doctor, I keep thinking I'm a dustbin.
    Don't talk rubbish.

Hope you like'em. Jenda
 END
 # $sender-Close
 # or die Failed to send message $sender-{'error_msg'}n;

第四部分,发送带有附件的邮件
 
代码如下:
# four - Attachment
 $sender-OpenMultipart({to='test@163.com',
                         subject='MailSender.pm - new module'});
 $sender-Body;
 $sender-SendEnc('END');
 Here is a new module MailSender.
 It provides an object based interface to sending SMTP mails.
 It uses a direct socket connection, so it doesn't need any addtional program.

Enjoy, Jenda
 END

$sender-Attach(
  {description = 'Perl module MailSender.pm',
   ctype = 'applicationx-zip-encoded',
   encoding = 'Base64',
   disposition = 'attachment; filename=Sender.zip;type=ZIP archive',
   file = 'Sender.zip'
  });
 $sender-Close

第五部分,发送html格式的邮件
 
代码如下:
my $htmlfile=demo.html;
  open IN, $htmlfile or die Cannot open $htmlfile  $!n;
  $sender-Open({ from = 'your@address.com', to = 'other@address.com',
         subject = 'HTML test',
         ctype = texthtml,
         encoding = 7bit
  }) or die $MailSenderError,n;

 while (IN) { $sender-SendEx($_) };
  close IN;
  $sender-Close();

---Sending HTML messages with inline images
         if (ref $sender-OpenMultipart({
                 from = 'someone@somewhere.net', to = $recipients,
                 subject = 'Embedded Image Test',
                 boundary = 'boundary-test-1',
                 multipart = 'related'})) {
                 $sender-Attach(
                          {description = 'html body',
                          ctype = 'texthtml; charset=us-ascii',
                          encoding = '7bit',
                          disposition = 'NONE',
                          file = 'test.html'
                 });
                 $sender-Attach({
                         description = 'ed's gif',
                         ctype = 'imagegif',
                         encoding = 'base64',
                         disposition = inline; filename=apache_pb.gif;rnContent-ID img1,
                         file = 'apache_pb.gif'
                 });
                 $sender-Close() or die Close failed! $MailSenderErrorn;
         } else {
                 die Cannot send mail $MailSenderErrorn;
         }

-----------
 # or using the eval{ $obj-Method()-Method()-...-Close()} trick ...
         use MailSender;
         eval {
         (new MailSender)
                 -OpenMultipart({
                         to = 'someone@somewhere.com',
                         subject = 'Embedded Image Test',
                         boundary = 'boundary-test-1',
                         type = 'multipartrelated'
                 })
                 -Attach({
                         description = 'html body',
                         ctype = 'texthtml; charset=us-ascii',
                         encoding = '7bit',
                         disposition = 'NONE',
                         file = 'ctempzkHTMLTest.htm'
                 })
                 -Attach({
                         description = 'Test gif',
                         ctype = 'imagegif',
                         encoding = 'base64',
                         disposition = inline; filename=test.gif;rnContent-ID img1,
                         file = 'test.gif'
                 })
                 -Close()
         }
         or die Cannot send mail $MailSenderErrorn;

阅读(1778) | 评论(0) | 转发(0) |
0

上一篇:关键词

下一篇:John The Ripper Hash Formats

给主人留下些什么吧!~~