Latest web development tutorials

Perl Send Mail

If your program is running on a Linux / Unix system, you'll be able to send mail usingsendmail tool in Perl.

The following is a simple example script for sending mail:

#!/usr/bin/perl

# 接收邮箱,这里我设置为我的 QQ 邮箱,你需要修改它为你自己的邮箱
$to = '[email protected]';
#发送者邮箱
$from = '[email protected]';
#标题
$subject = '本教程 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 program, the output is:

邮件发送成功

Under normal circumstances, the above message will be QQ-mail interception, what can I add it to the white list, the operation mode can click on: https: //kf.qq.com/faq/120322fu63YV130805rYRFzu.html

After whitelist can receive mail.

Send messages in HTML format

We can add aContent-type header in the message: text / html \ nto send messages in HTML format, examples are as follows:

#!/usr/bin/perl
 
# 接收邮箱,这里我设置为我的 QQ 邮箱,你需要修改它为你自己的邮箱
$to = '[email protected]';
#发送者邮箱
$from = '[email protected]';
#标题
$subject = '本教程 Perl 发送邮件测试';
$message = '<h1>这是一封使用 Perl 发送的邮件<h1><p>你好,我来自本教程,地址是:http://www.w3big.com。</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";

After you do, you can check the contents of the message, as follows:


Use MIME :: Lite module

If you are using a window system, there is no sendmail tool. Then you can use the perl MIME: Lite module as a mail client to send mail.

MIME: Lite Download module as: the MIME-Lite-3.030.tar.gz .

Here we directly cpan to install (requires root privileges), do not download:

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

After a successful installation, we have to demonstrate an example:

#!/usr/bin/perl
use MIME::Lite;
 
# 接收邮箱,这里我设置为我的 QQ 邮箱,你需要修改它为你自己的邮箱
$to = '[email protected]';
# 抄送者,多个使用逗号隔开
# $cc = '[email protected], [email protected]';

#发送者邮箱
$from = '[email protected]';
#标题
$subject = '本教程 Perl 发送邮件测试';
$message = '这是一封使用 Perl 发送的邮件,使用了 MIME::Lite 模块。';

$msg = MIME::Lite->new(
                 From     => $from,
                 To       => $to,
                 Cc       => $cc,
                 Subject  => $subject,
                 Data     => $message
                 );
                 
$msg->send;
print "邮件发送成功\n";

After you do, you can check the contents of the message, as follows:

Send messages in HTML format

We can add aContent-type header in the message: text / html \ nto send messages in HTML format, examples are as follows:

#!/usr/bin/perl
use MIME::Lite;
 
# 接收邮箱,这里我设置为我的 QQ 邮箱,你需要修改它为你自己的邮箱
$to = '[email protected]';
# 抄送者,多个使用逗号隔开
# $cc = '[email protected], [email protected]';

#发送者邮箱
$from = '[email protected]';
#标题
$subject = '本教程 Perl 发送邮件测试';
$message = '<h1>这是一封使用 Perl 发送的邮件<h1><p>使用了 MIME::Lite 模块。</p><p>来自本教程,地址是:http://www.w3big.com。</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";

After you do, you can check the contents of the message, as follows:

Send mail with attachments

Send e-mail with attachments examples are as follows:

#!/usr/bin/perl
use MIME::Lite;
 
# 接收邮箱,这里我设置为我的 QQ 邮箱,你需要修改它为你自己的邮箱
$to = '[email protected]';
# 抄送者,多个使用逗号隔开
# $cc = '[email protected], [email protected]';

#发送者邮箱
$from = '[email protected]';
#标题
$subject = '本教程 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        => './w3big.txt',   # 当前目录下
             Filename    => 'w3big.txt',
             Disposition => 'attachment'
            );
$msg->send;
print "邮件发送成功\n";

After you do, you can check the contents of the message, as follows:

You can use more than one $ msg-> attach to add more attachments.