YouTip LogoYouTip

Perl Sending Email

If your program runs on 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 $to = '429240967@qq.com'; $from = 'test@'; $subject = ' Perl Send Email Test'; $message = 'This is an email sent using Perl.'; open(MAIL, "|/usr/sbin/sendmail -t"); print MAIL "To: $ton"; print MAIL "From: $fromn"; print MAIL "Subject: $subjectnn"; print MAIL $message; close(MAIL); print "Mail sent successfullyn"; Execute the above program, the output is: Mail sent successfully > Under normal circumstances, the above email will be blocked by QQ mailbox, we can add it to the whitelist, the operation method can be clicked: [https://kf.qq.com/faq/120322fu63YV130805rYRFzu.html](https://kf.qq.com/faq/120322fu63YV130805rYRFzu.html) After adding to the whitelist, you can receive emails normally. !(#) ### Send HTML Formatted Email We can send HTML formatted emails by adding **Content-type: text/htmln** in the email header. The example is as follows: ## Example #!/usr/bin/perl $to = '429240967@qq.com'; $from = 'test@'; $subject = ' Perl Send Email Test'; $message = '

This is an email sent using Perl

Hello, I am from , the address is:

'; open(MAIL, "|/usr/sbin/sendmail -t"); print MAIL "To: $ton"; print MAIL "From: $fromn"; print MAIL "Subject: $subjectn"; print MAIL "Content-type: text/htmln"; print MAIL $message; close(MAIL); print "Mail sent successfullyn"; After successful execution, view the email content, as shown below: !(#) * * * ## Using MIME::Lite Module If you are using Windows system and don't have sendmail tool. Then you can use Perl's MIME::Lite module as an email client to send emails. MIME::Lite module download address is: [MIME-Lite-3.030.tar.gz](http://search.cpan.org/CPAN/authors/id/R/RJ/RJBS/MIME-Lite-3.030.tar.gz). Here we directly use cpan to install (need root permission), no need to download: $ cpan -i MIME::Lite ... /usr/bin/make install -- OK After successful installation, let's demonstrate an example: ## Example #!/usr/bin/perl use MIME::Lite; $to = '429240967@qq.com'; $from = 'test@'; $subject = ' Perl Send 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 "Mail sent successfullyn"; After successful execution, view the email content, as shown below: !(#) ### Send HTML Formatted Email We can send HTML formatted emails by adding **Content-type: text/htmln** in the email header. The example is as follows: ## Example #!/usr/bin/perl use MIME::Lite; $to = '429240967@qq.com'; $from = 'test@'; $subject = ' Perl Send Email Test'; $message = '

This is an email sent using Perl

Used MIME::Lite module.

From , address is:

'; $msg = MIME::Lite->new( From =>$from, To =>$to, Cc =>$cc, Subject =>$subject, Data =>$message ); $msg->attr("content-type" => "text/html"); $msg->send; print "Mail sent successfullyn"; After successful execution, view the email content, as shown below: !(#) ### Send Email with Attachment The example for sending email with attachment is as follows: ## Example #!/usr/bin/perl use MIME::Lite; $to = '429240967@qq.com'; $from = 'test@'; $subject = ' Perl Send Email Test'; $message = 'This is an email sent using Perl, using MIME::Lite module, including attachment.'; $msg = MIME::Lite->new( From =>$from, To =>$to, Cc =>$cc, Subject =>$subject, Type =>'multipart/mixed'

← Perl Packages ModulesPerl Directories β†’