Perl 简明教程

Perl - Sending Email

Using sendmail Utility

Sending a Plain Message

如果您在 Linux/Unix 计算机上工作,则可以在 Perl 程序中简单地使用 sendmail 实用程序来发送电子邮件。这里有一个示例脚本,用于向给定的电子邮件 ID 发送电子邮件。只需确保 sendmail 实用程序的给定路径正确。这可能因您的 Linux/Unix 计算机而异。

If you are working on Linux/Unix machine then you can simply use sendmail utility inside your Perl program to send email. Here is a sample script that can send an email to a given email ID. Just make sure the given path for sendmail utility is correct. This may be different for your Linux/Unix machine.

#!/usr/bin/perl

$to = 'abcd@gmail.com';
$from = 'webmaster@yourdomain.com';
$subject = 'Test Email';
$message = 'This is test email sent by Perl Script';

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;

close(MAIL);
print "Email Sent Successfully\n";

实际上,上述脚本是一个客户端电子邮件脚本,它将起草电子邮件并提交到在您的 Linux/Unix 计算机上本地运行的服务器。此脚本不负责将电子邮件发送到实际目的地。因此,您必须确保电子邮件服务器已正确配置并在计算机上运行,才能向给定的电子邮件 ID 发送电子邮件。

Actually, the above script is a client email script, which will draft email and submit to the server running locally on your Linux/Unix machine. This script will not be responsible for sending email to actual destination. So you have to make sure email server is properly configured and running on your machine to send email to the given email ID.

Sending an HTML Message

如果您想使用 sendmail 发送 HTML 格式的电子邮件,则只需在电子邮件的标题部分中添加 Content-type: text/html\n ,如下所示:

If you want to send HTML formatted email using sendmail, then you simply need to add Content-type: text/html\n in the header part of the email as follows −

#!/usr/bin/perl

$to = 'abcd@gmail.com';
$from = 'webmaster@yourdomain.com';
$subject = 'Test Email';
$message = '<h1>This is test email sent by Perl Script</h1>';

open(MAIL, "|/usr/sbin/sendmail -t");

# Email Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
print MAIL "Content-type: text/html\n";
# Email Body
print MAIL $message;

close(MAIL);
print "Email Sent Successfully\n";

Using MIME::Lite Module

如果您在 Windows 计算机上工作,则您无法访问 sendmail 实用程序。但是,您可以选择使用 MIME:Lite perl 模块来编写自己的电子邮件客户端。您可以从 MIME-Lite-3.01.tar.gz 下载此模块,并将其安装在您的 Windows 或 Linux/Unix 计算机上。要安装它,请按照以下简单步骤操作:

If you are working on windows machine, then you will not have access on sendmail utility. But you have alternate to write your own email client using MIME:Lite perl module. You can download this module from MIME-Lite-3.01.tar.gz and install it on your either machine Windows or Linux/Unix. To install it follow the simple steps −

$tar xvfz MIME-Lite-3.01.tar.gz
$cd MIME-Lite-3.01
$perl Makefile.PL
$make
$make install

这就完成了,并且您将在计算机上安装 MIME::Lite 模块。现在,您可以使用下面解释的简单脚本来发送电子邮件。

That’s it and you will have MIME::Lite module installed on your machine. Now you are ready to send your email with simple scripts explained below.

Sending a Plain Message

现在,以下是一个脚本,它将负责向给定的电子邮件 ID 发送电子邮件:

Now following is a script which will take care of sending email to the given email ID −

#!/usr/bin/perl
use MIME::Lite;

$to = 'abcd@gmail.com';
$cc = 'efgh@mail.com';
$from = 'webmaster@yourdomain.com';
$subject = 'Test Email';
$message = 'This is test email sent by Perl Script';

$msg = MIME::Lite->new(
                 From     => $from,
                 To       => $to,
                 Cc       => $cc,
                 Subject  => $subject,
                 Data     => $message
                 );

$msg->send;
print "Email Sent Successfully\n";

Sending an HTML Message

如果您想使用 sendmail 发送 HTML 格式的电子邮件,则只需在电子邮件的标题部分中添加 Content-type: text/html\n 。以下脚本将负责发送 HTML 格式的电子邮件:

If you want to send HTML formatted email using sendmail, then you simply need to add Content-type: text/html\n in the header part of the email. Following is the script, which will take care of sending HTML formatted email −

#!/usr/bin/perl
use MIME::Lite;

$to = 'abcd@gmail.com';
$cc = 'efgh@mail.com';
$from = 'webmaster@yourdomain.com';
$subject = 'Test Email';
$message = '<h1>This is test email sent by Perl Script</h1>';

$msg = MIME::Lite->new(
                 From     => $from,
                 To       => $to,
                 Cc       => $cc,
                 Subject  => $subject,
                 Data     => $message
                 );

$msg->attr("content-type" => "text/html");
$msg->send;
print "Email Sent Successfully\n";

Sending an Attachment

如果您想发送附件,则以下脚本具有此功能:

If you want to send an attachment, then following script serves the purpose −

#!/usr/bin/perl
use MIME::Lite;

$to = 'abcd@gmail.com';
$cc = 'efgh@mail.com';
$from = 'webmaster@yourdomain.com';
$subject = 'Test Email';
$message = 'This is test email sent by Perl Script';

$msg = MIME::Lite->new(
                 From     => $from,
                 To       => $to,
                 Cc       => $cc,
                 Subject  => $subject,
                 Type     => 'multipart/mixed'
                 );

# Add your text message.
$msg->attach(Type         => 'text',
             Data         => $message
             );

# Specify your file as attachement.
$msg->attach(Type         => 'image/gif',
             Path         => '/tmp/logo.gif',
             Filename     => 'logo.gif',
             Disposition  => 'attachment'
            );
$msg->send;
print "Email Sent Successfully\n";

您可以使用 attach() 方法在电子邮件中附加任意数量的文件。

You can attach as many files as you like in your email using attach() method.

Using SMTP Server

如果您的计算机没有运行电子邮件服务器,则可以使用远程位置上提供的任何其他电子邮件服务器。但是,要使用任何其他电子邮件服务器,您将需要一个 ID、其密码、URL 等。一旦您拥有所有必备的信息,您只需要在 send() 方法中提供这些信息,如下所示:

If your machine is not running an email server then you can use any other email server available at the remote location. But to use any other email server you will need to have an id, its password, URL, etc. Once you have all the required information, you simple need to provide that information in send() method as follows −

$msg->send('smtp', "smtp.myisp.net", AuthUser=>"id", AuthPass=>"password" );

您可以联系您的电子邮件服务器管理员以获取上述信息,并且如果尚未提供用户 ID 和密码,则您的管理员可以在几分钟内创建它。

You can contact your email server administrator to have the above used information and if a user id and password is not already available then your administrator can create it in minutes.