Perl 简明教程
Perl - Sending Email
Using sendmail Utility
Sending a Plain Message
如果您在 Linux/Unix 计算机上工作,则可以在 Perl 程序中简单地使用 sendmail 实用程序来发送电子邮件。这里有一个示例脚本,用于向给定的电子邮件 ID 发送电子邮件。只需确保 sendmail 实用程序的给定路径正确。这可能因您的 Linux/Unix 计算机而异。
#!/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 发送电子邮件。
Sending an HTML Message
如果您想使用 sendmail 发送 HTML 格式的电子邮件,则只需在电子邮件的标题部分中添加 Content-type: text/html\n ,如下所示:
#!/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 计算机上。要安装它,请按照以下简单步骤操作:
$tar xvfz MIME-Lite-3.01.tar.gz
$cd MIME-Lite-3.01
$perl Makefile.PL
$make
$make install
这就完成了,并且您将在计算机上安装 MIME::Lite 模块。现在,您可以使用下面解释的简单脚本来发送电子邮件。
Sending a Plain Message
现在,以下是一个脚本,它将负责向给定的电子邮件 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 格式的电子邮件:
#!/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
如果您想发送附件,则以下脚本具有此功能:
#!/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() 方法在电子邮件中附加任意数量的文件。