perl发信模块MailSender

发布时间:2019-11-22编辑:脚本学堂
这个模块基本实现了日常运维中常用的发信功能,比如多人邮件,抄送,密送,smtp认证,发送带附件的邮件,html格式的邮件等。

这个模块基本实现了日常运维中常用的发信功能,比如多人邮件,抄送,密送,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;