Send email with Perl


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:

#!/usr/bin/perl

# 接收邮箱,这里我设置为我的 QQ 邮箱,你需要修改它为你自己的邮箱
$to = '429240967@qq.com';
#发送者邮箱
$from = 'test@php.cn';
#标题
$subject = 'php中文网 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";

Execute the above program, the output result is:

邮件发送成功

Under normal circumstances, the above email will If it is 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 adding it to the whitelist, it will be normal. Received mail.

Send HTML format email

We can add Content-type: text/html\n to the email header to send HTML The email format is as follows:

#!/usr/bin/perl
 
# 接收邮箱,这里我设置为我的 QQ 邮箱,你需要修改它为你自己的邮箱
$to = '429240967@qq.com';
#发送者邮箱
$from = 'test@php.cn';
#标题
$subject = 'php中文网 Perl 发送邮件测试';
$message = '<h1>这是一封使用 Perl 发送的邮件<h1><p>你好,我来自php中文网,地址是:http://www.php.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";

After successful execution, view the email content as follows:


Use 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 an email client to send emails.

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, we will demonstrate an example:

#!/usr/bin/perl
use MIME::Lite;
 
# 接收邮箱,这里我设置为我的 QQ 邮箱,你需要修改它为你自己的邮箱
$to = '429240967@qq.com';
# 抄送者,多个使用逗号隔开
# $cc = 'test1@php.cn, test2@php.cn';

#发送者邮箱
$from = 'test@php.cn';
#标题
$subject = 'php中文网 Perl 发送邮件测试';
$message = '这是一封使用 Perl 发送的邮件,使用了 MIME::Lite 模块。';

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

After successful execution, View the email content as follows:

Send HTML format email

We can add Content-type: text/html in the email header \n To send emails in HTML format, the example is as follows:

#!/usr/bin/perl
use MIME::Lite;
 
# 接收邮箱,这里我设置为我的 QQ 邮箱,你需要修改它为你自己的邮箱
$to = '429240967@qq.com';
# 抄送者,多个使用逗号隔开
# $cc = 'test1@php.cn, test2@php.cn';

#发送者邮箱
$from = 'test@php.cn';
#标题
$subject = 'php中文网 Perl 发送邮件测试';
$message = '<h1>这是一封使用 Perl 发送的邮件<h1><p>使用了 MIME::Lite 模块。</p><p>来自php中文网,地址是:http://www.php.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";

After successful execution, check the email content, as shown below:

Send Email with attachment

The example of sending an email with attachment is as follows:

#!/usr/bin/perl
use MIME::Lite;
 
# 接收邮箱,这里我设置为我的 QQ 邮箱,你需要修改它为你自己的邮箱
$to = '429240967@qq.com';
# 抄送者,多个使用逗号隔开
# $cc = 'test1@php.cn, test2@php.cn';

#发送者邮箱
$from = 'test@php.cn';
#标题
$subject = 'php中文网 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        => './php.txt',   # 当前目录下
             Filename    => 'php.txt',
             Disposition => 'attachment'
            );
$msg->send;
print "邮件发送成功\n";

After successful execution, view the email content as follows:

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