[英]Sending email using Perl
我正在嘗試使用Perl發送電子郵件。 基本上我有一個Perl腳本以一種很好的格式打印出一個報告。 我希望通過電子郵件發送該報告。 我怎樣才能做到這一點?
如果機器沒有配置sendmail,我通常使用Mail :: Sendmail
use Mail::Sendmail;
%mail = (smtp => 'my.isp.com:25',
to => 'foo@example.com',
from => 'bar@example.com',
subject => 'Automatic greetings',
message => 'Hello there');
sendmail(%mail) or die;
MIME :: Lite是許多人使用的強大模塊。 它易於使用,包括您是否要附加文檔。
use MIME::Lite;
my $msg = MIME::Lite->new(
From => $from,
To => $to,
Subject => $subject,
Type => 'text/plain',
Data => $message,
);
$msg->send;
由於它默認使用sendmail
(而不是SMTP),因此您甚至不需要配置它。
值得一提的是,如果您的機器上安裝了Outlook並且在Outlook模塊上安裝了cpan:
# create the object
use Mail::Outlook;
my $outlook = new Mail::Outlook();
# start with a folder
my $outlook = new Mail::Outlook('Inbox');
# use the Win32::OLE::Const definitions
use Mail::Outlook;
use Win32::OLE::Const 'Microsoft Outlook';
my $outlook = new Mail::Outlook(olInbox);
# get/set the current folder
my $folder = $outlook->folder();
my $folder = $outlook->folder('Inbox');
# get the first/last/next/previous message
my $message = $folder->first();
$message = $folder->next();
$message = $folder->last();
$message = $folder->previous();
# read the attributes of the current message
my $text = $message->From();
$text = $message->To();
$text = $message->Cc();
$text = $message->Bcc();
$text = $message->Subject();
$text = $message->Body();
my @list = $message->Attach();
# use Outlook to display the current message
$message->display;
# Or use a hash
my %hash = (
To => 'suanna@live.com.invalid',
Subject => 'Blah Blah Blah',
Body => 'Yadda Yadda Yadda',
);
my $message = $outlook->create(%hash);
$message->display(%hash);
$message->send(%hash);
請注意, .invalid TLD不是真實的,因此上述地址無法提供。 在任何情況下,我都在這里對模塊中的內容做了一個不錯的解釋 - 這會發送一條消息!
沒有CPAN庫的最簡單方法:
#!/usr/bin/perl
$to = 'toAddress@xx.com'; # to address
$from = 'fromAddress@xx.com'; # from address
$subject = 'subject'; # email subject
$body = 'Email message content';# message
open(MAIL, "|/usr/sbin/sendmail -t");
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
print MAIL $body;
close(MAIL);
print "Email Sent Successfully to $to\n";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.