使用MIME::Lite模块
如果您在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模块。现在,您可以使用下面说明的简单脚本发送电子邮件。
发送简单的消息
现在下面是一个脚本,它将负责将电子邮件发送到给定的电子邮件ID-
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";
发送HTML消息
如果要使用sendmail发送HTML格式的电子邮件,则只需在电子邮件的标题部分添加Content-type:text/html\n。以下是脚本,它将负责发送HTML格式的电子邮件-
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";
发送附件
如果您想发送附件,则以下脚本可满足以下目的:
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()方法在电子邮件中附加任意数量的文件。