使用Perl发送电子邮件

时间:2012-06-07 20:23:20

标签: perl email

我正在尝试使用Perl发送电子邮件。基本上我有一个Perl脚本以一种很好的格式打印出一个报告。我希望通过电子邮件发送该报告。我怎么能这样做?

4 个答案:

答案 0 :(得分:4)

如果机器没有配置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;

答案 1 :(得分:3)

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),因此您甚至无需对其进行配置。

答案 2 :(得分:3)

值得一提的是,如果您的计算机上安装了Outlook并且在Outlook模块上安装了Outlook:

 # 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不是真实的,因此上述地址无法提供。在任何情况下,我都在这里对模块中的内容做了一个不错的解释 - 这会发送一条消息!

答案 3 :(得分:0)

没有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";