Perl sends mail


Release date:2023-10-20 Update date:2023-10-24 Editor:admin View counts:335

Label:

Perl sends mail

If your program runs on Linux/Unix, you can use it in Perl sendmail tool to send mail.

The following is a simple script example for sending mail:

Example

#!/usr/bin/perl# Receiving email, I set it as my QQ
Email, you need to modify it to your own email $to='429240967@qq.com';
#Sender email$from='test@runoob.com';#title$subject='Novice Tutorial
Perl Send Email Test';$message='This is a letter using Perl
sent email.';open(MAIL,"\|/usr/sbin/sendmail -t");#
Email header printMAIL"To:$to\\n";printMAIL"From:$from\\n";printMAIL"Subject:$subject\\n\\n";#
Email information printMAIL$message;close(MAIL);print"Email sent successfully\\n";

Execute the above program, and the output is as follows:

Email sent successfully

Under normal circumstances, the above mail will be intercepted by QQ Mail, we can add it to the whitelist, the mode of operation can be clicked: https://kf.qq.com/faq/120322fu63YV130805rYRFzu.html

After joining the whitelist, you can receive email normally.

Send a message in HTML format

We can add it to the header Content-type: text/html\n to send an emailin HTML format, an example is as follows:

Example

#/ Usr/bin/perl # Receive email, here I set it as my QQ
Email, you need to modify it to your own email $to=' 429240967@qq.com '# Sender email
$from=' test@runoob.com '# Title $subject='Rookie Tutorial
Perl sending email test '$ Message='<h1>This is an email using Perl
Hello, I am from Cainiao Tutorial and my address is: http://www.runoob.com </ p> '; Open (MAIL, " |/usr/sbin/sendmail
-T ")#
Email header printMAIL "To: $to n"; PrintMAIL "From: $from n"; PrintMAIL "Subject: $subject n"; PrintMAIL "Content type:
Text/html n "#
Email information printMAIL $message; Close (MAIL); Print "Email sent successfully n";

After the execution is successful, you can view the contents of the message.

Use the MIME::Lite module

If you are using a window system, no sendmail tools. At this point, youcan use perl of MIME:Lite module to send mail as a mail client.

MIME:Lite module download address is: MIME-Lite-3.030.tar.gz .

We use it directly here. cpan to install (root permission is required) without downloading:

$ cpan -i MIME::Lite
……
  /usr/bin/make install  -- OK

After the installation is successful, let’s demonstrate an example:

Example

#/ Usr/bin/perluse MIME:: Lite# Receiving email, I set it as my QQ here
Email, you need to modify it to your own email $to=' 429240967@qq.com '#
CC recipients, multiple separated by commas # $cc=' test1@runoob.com ,
test2@runoob.com '# Sender email $from=' test@runoob.com '# Title $subject='Rookie Tutorial
Perl sending email test '$ Message='This is an email sent using Perl, using
MIME:: Lite
Module$ Msg=MIME:: Lite ->new (From=>$from, To=>$to, Cc=>$cc, Subject=>$subject,
Data=>$message)$ Msg ->send; Print "Email sent successfully n";

After the execution is successful, you can view the contents of the message.

Send a message in HTML format

We can add it to the header Content-type: text/html\n to send an emailin HTML format, an example is as follows:

Example

#/ Usr/bin/perluse MIME:: Lite# Receiving email, I set it as my QQ here
Email, you need to modify it to your own email $to=' 429240967@qq.com '#
CC recipients, multiple separated by commas # $cc=' test1@runoob.com ,
test2@runoob.com '# Sender email $from=' test@runoob.com '# Title $subject='Rookie Tutorial
Perl sending email test '$ Message='<h1>This is an email using Perl
The email sent<h1><p>used MIME:: Lite
Module</ p> From the rookie tutorial, the address is: http://www.runoob.com </ p> '$ Msg=MIME::
Lite ->new (From=>$from, To=>$to, Cc=>$cc, Subject=>$subject, Data=>$message)#
Add header information $msg ->attr ("content type"=>"text/html")
$ Msg ->send; Print "Email sent successfully n";

After the execution is successful, you can view the contents of the message.

Send a message with an attachment

An example of sending a message with an attachment is as follows:

Example

#/ Usr/bin/perluse MIME:: Lite# Receiving email, I set it as my QQ here
Email, you need to modify it to your own email $to=' 429240967@qq.com '#
CC recipients, multiple separated by commas # $cc=' test1@runoob.com ,
test2@runoob.com '# Sender email $from=' test@runoob.com '# Title $subject='Rookie Tutorial
Perl sending email test '$ Message='This is an email sent using Perl, using
MIME:: Lite
Module, including attachments$ Msg=MIME:: Lite ->new (From=>$from,
To=>$to, Cc=>$cc, Subject=>$subject, Type=>'multipart/mixed '#
Attachment marking)$ Msg ->attach (Type=>'TEXT ', Data=>$message)#
Specify attachment information $msg ->attach (Type=>'TEXT ', Path=>'./runoob. txt '#

Filename= > ‘runoob.txt’,Disposition= >’ attachment’) in the current directory; $msg- > send;print “email sent successfullyn”

After the execution is successful, you can view the contents of the message.

You can do this by using multiple $msg->attach to add multiple attachments.

Powered by TorCMS (https://github.com/bukun/TorCMS).