Use Mime::lite to send message instances in Perl _perl

Source: Internet
Author: User
Tags mixed


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:





 code as follows:

#!/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:





code as follows:

#!/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:





 code as follows:

#!/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?


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.