If your program runs on a Linux/Unix system, you can use the sendmail tool in Perl to send emails.
The following is a simple script example for sending emails:
Example
#!/usr/bin/perl # Receiving email, here I set it as my QQ email, you need to modify it to your own email $to = ' [email protected] ' ; #Sender's email $from = ' [email protected] ' ; #Title $subject = ' Coder tutorial Perl sending email test ' ; $message = ' This is an email sent using Perl. ' ; open ( MAIL , " |/usr/sbin/sendmail -t " ) ; # Mail header print MAIL " To: $to n " ; print MAIL " From: $from n " ; print MAIL " Subject: $subject n n " ; # Email information print MAIL $message ; close ( MAIL ) ; print " Email sent successfully n " ; Executing the above program, the output result is:
Email sent successfully
Under normal circumstances, the above email will be intercepted by QQ mailbox. We can add it to the whitelist. The operation method can be clicked: https://kf.qq.com/faq/120322fu63YV130805rYRFzu.html
After joining the whitelist, you can receive emails normally.
Send HTML format email
We can add Content-type: text/htmln in the email header to send emails in HTML format. The example is as follows:
Example
#!/usr/bin/perl # Receiving email, here I set it as my QQ email, you need to modify it to your own email $to = ' [email protected] ' ; #Sender's email $from = ' [email protected] ' ; #Title $subject = ' Coder Tutorial Perl sending email test ' ; $message = ' <h1>This is an email sent using Perl<h1><p>Hello, I am from the Coder Tutorial, the address is: http:/ /www.codercto.com. </p> ' ; open ( MAIL , " |/usr/sbin/sendmail -t " ) ; # Mail header print MAIL " To: $to n " ; print MAIL " From: $from n " ; print MAIL " Subject: $subject n " ; print MAIL " Content-type: text/html n " ; # Email information print MAIL $message ; close ( MAIL ) ; print " Email sent successfully n " ; After successful execution, check the email content, as shown below:
Using the MIME::Lite module
If you are using a window system, there is no sendmail tool. At this time you can use perl's MIME:Lite module as a mail client to send mail.
The MIME:Lite module download address is: MIME-Lite-3.030.tar.gz.
Here we use cpan to install directly (root permissions are required) without downloading:
$ cpan -i MIME::Lite…… /usr/bin/make install -- OK
After successful installation, let's demonstrate an example:
Example
#!/usr/bin/perl use MIME::Lite ; # Receive email, here I set it as my QQ email, you need to modify it to your own email $to = ' [email protected] ' ; # Cc's, separate multiple copies with commas # $cc = '[email protected], [email protected]'; #sender's email $from = ' [email protected] ' ; #Title $subject = ' Coder tutorial Perl sending email test ' ; $message = ' This is an email sent using Perl, using the 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 successful execution, check the email content, as shown below:
Send HTML format email
We can add Content-type: text/htmln in the email header to send emails in HTML format. The example is as follows:
Example
#!/usr/bin/perl use MIME::Lite ; # Receive email, here I set it as my QQ email, you need to modify it to your own email $to = ' [email protected] ' ; # Cc's, separate multiple ones with commas # $cc = '[email protected], [email protected]'; #sender's email $from = ' [email protected] ' ; #Title $subject = ' Coder tutorial Perl sending email test ' ; $message = ' <h1>This is an email sent using Perl<h1><p>Using the MIME::Lite module. </p><p>From the coder tutorial, the address is: http://www.codercto.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 successful execution, check the email content, as shown below:
Send email with attachments
An example of sending an email with attachments is as follows:
Example
#!/usr/bin/perl use MIME::Lite ; # Receive email, here I set it as my QQ email, you need to modify it to your own email $to = ' [email protected] ' ; # Cc's, separate multiple ones with commas # $cc = '[email protected], [email protected]'; #sender's email $from = ' [email protected] ' ; #Title $subject = ' Coder tutorial Perl sending email test ' ; $message = ' This is an email sent using Perl, using the MIME::Lite module and containing attachments. ' ; $msg = MIME::Lite -> new ( From => $from , To => $to , Cc => $cc , Subject => $subject , Type => ' multipart/mixed ' # Attachment tag ) ; $msg -> attach ( Type => ' TEXT ' , Data => $message ) ; #Specify attachment information $msg -> attach ( Type => ' TEXT ' , Path => ' ./codercto.txt ' , # in the current directory Filename => ' codercto.txt ' , Disposition => ' attachment ' ) ; $msg -> send ; print " Email sent successfully n " ; After successful execution, check the email content, as shown below:
You can add multiple attachments by using multiple $msg->attach.