Sometimes when we deploy a script, we want to know how our program is performing, and we want to get the results of execution, so we can rest assured that a lot of it is good to be able to send me a message when the program succeeds or fails.
In fact, there are many ways to use Perl to send mail, including your search for mail keyword on the cpan is a lot of, after practice, Mime::lite used to send e-mail is very appropriate, the most incredible is that it can help you easily send a message with attachments oh.
Here we take the mime::lite email as an example:
There is a detailed usage of it on the CPAN (HTTP://SEARCH.CPAN.ORG/~RJBS/MIME-LITE-3.028/LIB/MIME/LITE.PM)
There are two ways to send mail, the first is the simplest use of the system's own mail program, such as SendMail to do, run SendMail of course, may have to have root permissions
The other is through the way of SMTP, we will take NetEase's 163 mailbox as an example to explain.
Let's use the default send mode (SendMail) as an example to illustrate:
#!/usr/bin/perl-w
Use Mime::lite;
My $msg = Mime::lite->new (
From => ' chenqing663@163.com ',
To => ' chenqing663@foxmail.com ',
Cc => ' some@other.com, some@more.com ',
Subject => ' hello,my-A-mail from chenqing.org ',
Type => ' multipart/mixed ',
Data => ' other data '
);
$msg->attach (
Type => ' Image/png ',
Disposition => ' attachment ',
Filename => ' Other.png ',
Path => '/home/king/perl/logo.png '
);
$msg->send;
One more HTML format:
#!/usr/bin/perl-w
Use Mime::lite;
My $msg = Mime::lite->new (
From => ' chenqing663@163.com ',
To => ' chenqing663@foxmail.com ',
Cc => ' some@other.com, some@more.com ',
Subject => ' hello,my-A-mail from chenqing.org ',
Type => ' multipart/mixed ',
Data => ' other data '
);
$msg->attach (
Type => ' text/html ',
Data => qq{
<body>
This is my <b>good</b> image:
</body>
},
);
$msg->attach (
Type => ' Image/png ',
Disposition => ' attachment ',
Filename => ' Other.png ',
Id => ' Logo.png ',
Path => '/home/king/perl/logo.png '
);
$msg->send;
Let's see how to send it in SMTP:
#!/usr/bin/perl-w
Use Mime::lite;
Use mime::base64;
Use AUTHEN::SASL;
My $host = ' smtp.163.com ';
My $pass = ' yourpass ';
My $user = ' xxx@163.com ';
My $msg = Mime::lite->new (
From => ' xxx@163.com ',
To => ' chenqing663@foxmail.com ',
Cc => ' some@other.com, some@more.com ',
Subject => ' hello,my-A-mail from chenqing.org ',
Type => ' multipart/mixed ',
Data => ' other data '
);
$msg->attach (
Type => ' text/html ',
Data => qq{
<body>
This is my <b>good</b> image:
</body>
},
);
$msg->attach (
Type => ' Image/png ',
Disposition => ' attachment ',
Filename => ' Other.png ',
Id => ' Logo.png ',
Path => '/home/king/perl/logo.png '
);
Mime::lite->send (' SMTP ', $host, timeout=>60, authuser=> $user, authpass=> $pass);
$msg->send;
Is it simple?