2

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

4

4 回答 4

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;
于 2012-06-07T21:33:11.920 回答
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),因此您甚至不需要配置它。

于 2012-06-07T20:30:10.173 回答
3

值得一提的是,如果你的机器上碰巧有 Outlook 并 cpan 了 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不是真实的,因此上述地址将不会发送。无论如何,我在这里对模块中的内容做了一个体面的解释——这发送了一条信息!

于 2012-06-07T20:50:55.997 回答
1

没有 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";
于 2017-03-21T20:28:01.780 回答