使用 Perl 发送电子邮件
Perl 允许其用户使用 Perl 代码脚本发送邮件。有多种使用 Perl 发送电子邮件的方法。这些电子邮件可以是简单的电子邮件、带有附件的电子邮件、HTML 格式的电子邮件、具有多个接收者的电子邮件等。Perl 也提供了一些模块来做同样的事情。在本文中,我们将介绍一些使用 Perl 脚本发送电子邮件的方法。
发送邮件实用程序
首先,我们将讨论 Sendmail 实用程序。您不能期望通过使用它获得出色的交货率。有时电子邮件会跳过收件箱。对于 Linux/Unix,Sendmail 的设置既快速又简单。对于 Windows 和其他设备,您需要寻找替代方法。
发送简单邮件
perl
#!/usr/bin/perl
# Details for email
$to = 'user1@mail.abc';
$from = 'user2@mail.abc';
$subject = 'Sending mail using perl';
$message = 'Well that was easy!!';
open(MAIL, "|/usr/sbin/sendmail -t");
# Email Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
# Email Body
print MAIL $message;
$result = close(MAIL);
if($result)
{
print "Email Sent, Bro!\n";
}
else
{
print "There was a problem, Bro!\n";
}
perl
use MIME::QuotedPrint;
use MIME::Base64;
use Mail::Sendmail;
%mail = ( from => 'user1@mail.abc',
to => 'user2@mail.abc',
subject => 'Sending mail using perl');
$boundary = "====" . time() . "====";
$mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
$message = encode_qp( "Well that was easy!!" );
# This is the perl executable
$file = $^X;
open (F, $file) or die "Cannot read $file: $!";
binmode F; undef $/;
$mail{body} = encode_base64();
close F;
$boundary = '--'.$boundary;
$mail{body} = <
perl
#!/usr/bin/perl
use MIME::Lite;
$to = 'receiver_mail@anything.com';
$cc = 'anyone@anything.com';
$from = 'sender_mail@anything.com';
$subject = 'Sending mail using perl';
$message = 'Well that was easy!!';
$msg = MIME::Lite->new(
From => $from,
To => $to,
Cc => $cc,
Subject => $subject,
Data => $message
);
$msg->send;
print "Email Sent Successfully!\n";
perl
# Add text message.
$msg->attach(Type => 'text',
Filename => 'filename.extension',
Data => $message);
# Specify your file as attachment.
$msg->attach(Type => 'image/gif',
Path => 'Provide_Full_Path',
Filename => 'Filename.extension',
Disposition => 'attachment');
perl
#!/usr/bin/perl
use MIME::Lite;
$to = 'receiver1_mail@anything.com, receiver2_mail@anything.com, receiver3_mail@anything.com';
$cc = 'anyone@anything.com';
$from = 'sender_mail@anything.com';
$subject = 'Sending mail using perl';
$message = 'Well that was easy!!';
$msg = MIME::Lite->new(From => $from,
To => $to,
Cc => $cc,
Subject => $subject,
Data => $message);
# Add text message.
$msg->attach(Type => 'text',
Filename => 'filename.extension',
Data => $message);
# Specify your file as attachment.
$msg->attach(Type => 'image/gif',
Path => 'Provide_Full_Path',
Filename => 'Filename.extension',
Disposition => 'attachment');
$msg->send;
print "Email Sent Successfully!\n";
perl
#!/usr/bin/perl
use strict;
use warnings;
use MIME::Lite::TT::HTML;
my %params;
$params{first_name} = 'Piotr';
$params{client_name} = 'Jane';
my %options;
$options{INCLUDE_PATH} = '/path/to/templates';
my $msg = MIME::Lite::TT::HTML->new(
From => 'user1@mail.abc',
To => 'user2@mail.abc',
Subject => 'Sending mail',
Template => {text => 'notice.txt.tt',
html => 'notice.html.tt',},
TmplOptions => \%options,
TmplParams => \%params,
);
$msg->send;
perl
$msg->attr("content-type" => "multipart/mixed");
$msg->attach(Type => 'application/pdf',
Path => '/path/to/prison_map.pdf',
Filename => 'prison_map.pdf',
Disposition => 'attachment');
perl
#!/usr/bin/perl
use strict;
use warnings;
use MIME::Lite::TT::HTML;
my %params;
$params{first_name} = 'Piotr';
$params{client_name} = 'Jane';
my %options;
$options{INCLUDE_PATH} = '/path/to/templates';
my $msg = MIME::Lite::TT::HTML->new(
From => 'user1@mail.abc',
To => 'user2@mail.abc',
Subject => 'Sending mail',
Template => {text => 'notice.txt.tt',
html => 'notice.html.tt',},
TmplOptions => \%options,
TmplParams => \%params,
);
$msg->attr("content-type" => "multipart/mixed");
$msg->attach(Type => 'application/pdf',
Path => '/path/to/prison_map.pdf',
Filename => 'prison_map.pdf',
Disposition => 'attachment');
$msg->send;
只需添加您和发件人的电子邮件 ID,邮件就会成功发送,您将很快看到结果。如果不这样做,请确保在您的计算机上正确配置了 sendmail,并且收件人的电子邮件服务不会阻止来自您 IP 地址的电子邮件。
HTML 格式的电子邮件
如果用户想向接收者发送 html 格式的文本怎么办?在这种情况下,只需在 perl 脚本中添加以下代码作为 $message 变量:
$message = 'H1 is the main heading
';
上面的代码将按照 html 语法格式化您的邮件。您可以以所需的方式使用 html 的所有标签。
添加 CC 和 BCC 字段
您还可以在邮件标题中添加 CC 和 BCC 字段。在您的 Perl 脚本中添加以下变量:
$cc = 'user1@mail.abc, user2@mail.abc, user3@mail.abc';
$bcc = 'user4@mail.abc';
添加附件
可以在邮件中添加附件,但要复杂一些。使用这个模块并添加大文件可能不是一个好主意,但你最好使用小文件。
Note: Before running the code below, make sure the Mail::Sendmail module is already installed.
perl
use MIME::QuotedPrint;
use MIME::Base64;
use Mail::Sendmail;
%mail = ( from => 'user1@mail.abc',
to => 'user2@mail.abc',
subject => 'Sending mail using perl');
$boundary = "====" . time() . "====";
$mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
$message = encode_qp( "Well that was easy!!" );
# This is the perl executable
$file = $^X;
open (F, $file) or die "Cannot read $file: $!";
binmode F; undef $/;
$mail{body} = encode_base64();
close F;
$boundary = '--'.$boundary;
$mail{body} = <
Note: Don’t forget to configure %mail section and filename section of $message variable.
MIME::精简版
该模块提供了更大的灵活性,也可用于非 Linux/Unix 机器。通过这个模块,您可以轻松地将相同的邮件发送给不同的收件人,使用 HTML 标签格式化您的邮件,还可以发送多个附件。 MIME::Lite旨在作为一个简单、独立的模块,专门用于生成 MIME 消息,它允许您输出带有文本或二进制附件的简单、体面的单部分或多部分消息。它不要求您安装 Mail:: 或 MIME:: 模块,但如果安装了它们将与它们一起使用。为了安装只需写:
cpan -i MIME::Lite
或者你可以从这里下载。
简单邮件
以下是您需要运行才能发送邮件的文件。以下代码需要指定邮件的“收件人邮件地址”、“发件人邮件地址”、“主题”和“正文”。
perl
#!/usr/bin/perl
use MIME::Lite;
$to = 'receiver_mail@anything.com';
$cc = 'anyone@anything.com';
$from = 'sender_mail@anything.com';
$subject = 'Sending mail using perl';
$message = 'Well that was easy!!';
$msg = MIME::Lite->new(
From => $from,
To => $to,
Cc => $cc,
Subject => $subject,
Data => $message
);
$msg->send;
print "Email Sent Successfully!\n";
现在验证是否只需检查收件人的邮件,您将看到如下图:
HTML 格式的邮件
就像 SendMail 实用程序一样, MIME::Lite模块也允许用户使用 perl 脚本发送 HTML 格式的邮件。
$message = 'H1 is the main heading
';
上面的代码将按照 HTML 语法格式化您的邮件。您可以以所需的方式使用 HTML 的所有标签。
将相同的电子邮件发送给不同的收件人
现在的问题是,如果您有多个接收器怎么办。在这种情况下,您无需执行任何操作,只需将 $to 变量配置为:
$to = 'receiver1_mail@anything.com, receiver2_mail@anything.com, receiver3_mail@anything.com';
Note: Make sure to separate each mail address using comma(,)
发送附件
附件可以是文档、pdf 或图像之类的任何内容。您可以在附件中添加短信。该模块的优点之一是它可以轻松发送附件。您可以使用 attach() 方法在一封电子邮件中包含多个文件。为了发送附件只需写:
perl
# Add text message.
$msg->attach(Type => 'text',
Filename => 'filename.extension',
Data => $message);
# Specify your file as attachment.
$msg->attach(Type => 'image/gif',
Path => 'Provide_Full_Path',
Filename => 'Filename.extension',
Disposition => 'attachment');
最后一步
因此,经过上述配置(如添加多个接收者、使用 HTML 格式化邮件和添加附件)后的最终代码将如下所示:
perl
#!/usr/bin/perl
use MIME::Lite;
$to = 'receiver1_mail@anything.com, receiver2_mail@anything.com, receiver3_mail@anything.com';
$cc = 'anyone@anything.com';
$from = 'sender_mail@anything.com';
$subject = 'Sending mail using perl';
$message = 'Well that was easy!!';
$msg = MIME::Lite->new(From => $from,
To => $to,
Cc => $cc,
Subject => $subject,
Data => $message);
# Add text message.
$msg->attach(Type => 'text',
Filename => 'filename.extension',
Data => $message);
# Specify your file as attachment.
$msg->attach(Type => 'image/gif',
Path => 'Provide_Full_Path',
Filename => 'Filename.extension',
Disposition => 'attachment');
$msg->send;
print "Email Sent Successfully!\n";
MIME::精简版:TT::HTML
这是 MIME::Lite 的变体,它增加了它的流行度。它类似于 MIME::Lite,但不同之处在于使用模板而不是硬编码消息内容的能力。
为了安装这个模块:
cpan -i MIME::Lite::TT::HTML
现在首先您必须创建一个用于发送邮件的模板。
文件名:notice.txt.tt
This is from notice.txt
文件名:notice.html.tt
This is from
notice.html
现在使用模板配置的源代码将如下所示。
perl
#!/usr/bin/perl
use strict;
use warnings;
use MIME::Lite::TT::HTML;
my %params;
$params{first_name} = 'Piotr';
$params{client_name} = 'Jane';
my %options;
$options{INCLUDE_PATH} = '/path/to/templates';
my $msg = MIME::Lite::TT::HTML->new(
From => 'user1@mail.abc',
To => 'user2@mail.abc',
Subject => 'Sending mail',
Template => {text => 'notice.txt.tt',
html => 'notice.html.tt',},
TmplOptions => \%options,
TmplParams => \%params,
);
$msg->send;
发送附件
要发送附件,只需在 $msg 变量后添加以下代码。
perl
$msg->attr("content-type" => "multipart/mixed");
$msg->attach(Type => 'application/pdf',
Path => '/path/to/prison_map.pdf',
Filename => 'prison_map.pdf',
Disposition => 'attachment');
Note: Give complete path and full extension to add the file.
现在完整的源代码将如下所示:
perl
#!/usr/bin/perl
use strict;
use warnings;
use MIME::Lite::TT::HTML;
my %params;
$params{first_name} = 'Piotr';
$params{client_name} = 'Jane';
my %options;
$options{INCLUDE_PATH} = '/path/to/templates';
my $msg = MIME::Lite::TT::HTML->new(
From => 'user1@mail.abc',
To => 'user2@mail.abc',
Subject => 'Sending mail',
Template => {text => 'notice.txt.tt',
html => 'notice.html.tt',},
TmplOptions => \%options,
TmplParams => \%params,
);
$msg->attr("content-type" => "multipart/mixed");
$msg->attach(Type => 'application/pdf',
Path => '/path/to/prison_map.pdf',
Filename => 'prison_map.pdf',
Disposition => 'attachment');
$msg->send;
使用 SMTP
默认情况下,上述方法使用 localhost 发送电子邮件,本地设置服务器不保证会发送。一旦您选择了您的提供商或决定自行设置 SMTP 服务器,请调整 MIME 模块中的代码并在 send() 方法中提供 SMTP 凭据:
$msg->send('smtp', "smtp.example.com", AuthUser=>"your_id", AuthPass=>"your_password" );