Perl sends a message
If your program is running on a Linux/Unix system, you can use the sendmail tool to send messages in Perl.
Here's a simple example of a script for sending a message:
#!/usr/bin/perl # 接收邮箱,这里我设置为我的 QQ 邮箱,你需要修改它为你自己的邮箱 $to = '[email protected]'; #发送者邮箱 $from = '[email protected]'; #标题 $subject = 'W3Cschool教程 Perl 发送邮件测试'; $message = '这是一封使用 Perl 发送的邮件。'; open(MAIL, "|/usr/sbin/sendmail -t"); # 邮件头部 print MAIL "To: $to\n"; print MAIL "From: $from\n"; print MAIL "Subject: $subject\n\n"; # 邮箱信息 print MAIL $message; close(MAIL); print "邮件发送成功\n";
The above procedure is performed and the output is:
邮件发送成功
Under normal circumstances, the above message will be blocked by QQ mailbox, I can add it to the whitelist, the operation can click: https://kf.qq.com/faq/120322fu63YV130805rYRFzu.html
When you join the whitelist, you can receive mail normally.
Send HTML-formatted messages
We can add Content-type: text/html to the head of the message to send the HTML message, as follows:
#!/usr/bin/perl # 接收邮箱,这里我设置为我的 QQ 邮箱,你需要修改它为你自己的邮箱 $to = '[email protected]'; #发送者邮箱 $from = '[email protected]'; #标题 $subject = 'W3Cschool教程 Perl 发送邮件测试'; $message = '<h1>这是一封使用 Perl 发送的邮件<h1><p>你好,我来自W3Cschool教程,地址是:http://www.w3cschool.cn。</p>'; open(MAIL, "|/usr/sbin/sendmail -t"); # 邮件头部 print MAIL "To: $to\n"; print MAIL "From: $from\n"; print MAIL "Subject: $subject\n"; print MAIL "Content-type: text/html\n"; # 邮箱信息 print MAIL $message; close(MAIL); print "邮件发送成功\n";
Use the MIME:: Lite module
If you're using a window system, there's no sendmail tool. You can then use perl's MIME:Lite module as a mail client to send messages.
MIME:Lite module Download address: MIME-Lite-3.030.tar.gz.
Here we install directly with cpan (root permission required) without downloading:
$ cpan -i MIME::Lite …… /usr/bin/make install -- OK
After the installation is successful, let's demonstrate an example:
#!/usr/bin/perl use MIME::Lite; # 接收邮箱,这里我设置为我的 QQ 邮箱,你需要修改它为你自己的邮箱 $to = '[email protected]'; # 抄送者,多个使用逗号隔开 # $cc = '[email protected], [email protected]'; #发送者邮箱 $from = '[email protected]'; #标题 $subject = 'W3Cschool教程 Perl 发送邮件测试'; $message = '这是一封使用 Perl 发送的邮件,使用了 MIME::Lite 模块。'; $msg = MIME::Lite->new( From => $from, To => $to, Cc => $cc, Subject => $subject, Data => $message ); $msg->send; print "邮件发送成功\n";
Send HTML-formatted messages
We can add Content-type: text/html to the head of the message to send the HTML message, as follows:
#!/usr/bin/perl use MIME::Lite; # 接收邮箱,这里我设置为我的 QQ 邮箱,你需要修改它为你自己的邮箱 $to = '[email protected]'; # 抄送者,多个使用逗号隔开 # $cc = '[email protected], [email protected]'; #发送者邮箱 $from = '[email protected]'; #标题 $subject = 'W3Cschool教程 Perl 发送邮件测试'; $message = '<h1>这是一封使用 Perl 发送的邮件<h1><p>使用了 MIME::Lite 模块。</p><p>来自W3Cschool教程,地址是:http://www.w3cschool.cn。</p>'; $msg = MIME::Lite->new( From => $from, To => $to, Cc => $cc, Subject => $subject, Data => $message ); # 添加头部信息 $msg->attr("content-type" => "text/html"); $msg->send; print "邮件发送成功\n";
Send a message with attachments
Send an example of a message with an attachment as follows:
#!/usr/bin/perl use MIME::Lite; # 接收邮箱,这里我设置为我的 QQ 邮箱,你需要修改它为你自己的邮箱 $to = '[email protected]'; # 抄送者,多个使用逗号隔开 # $cc = '[email protected], [email protected]'; #发送者邮箱 $from = '[email protected]'; #标题 $subject = 'W3Cschool教程 Perl 发送邮件测试'; $message = '这是一封使用 Perl 发送的邮件,使用了 MIME::Lite 模块,包含了附件。'; $msg = MIME::Lite->new( From => $from, To => $to, Cc => $cc, Subject => $subject, Type => 'multipart/mixed' # 附件标记 ); $msg->attach ( Type => 'TEXT', Data => $message );# 指定附件信息 $msg->attach(Type => 'TEXT', Path => './w3cschool.txt', # 当前目录下 Filename => 'w3cschool.txt', Disposition => 'attachment' ); $msg->send; print "邮件发送成功\n";
You can add $msg attachments by using multiple attachments.