【问题标题】:Sending email using Perl使用 Perl 发送电子邮件
【发布时间】:2017-03-22 04:28:01
【问题描述】:

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

【问题讨论】:

标签: perl email


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • @Grep Bacon 我试过这段代码,但错误是Can't call method "From" on an undefined value at mail.pl line 24. 在这段代码中。我能做什么?
    • @Hussain 不要完全按原样运行此代码 - 这只是一个 CPAN 片段,显示了您可以执行的所有方法。在第 24 行之前,它运行 first()、next()、last() 和 previous() 的代码。您不应该背靠背运行所有这些...可能是它试图访问没有 From() 的东西。此外,在它选择当前文件夹两次之前...不妨清理它以获取“收件箱”。
    • 您好,我遇到了一个问题,似乎$outlook = new Mail::Outlook() 返回 undef...这是为什么呢?我已经安装了模块并且我有 Outlook 2007...如何创建 $outlook 对象?
    【解决方案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),您甚至不需要配置它。

    【讨论】:

    • 我知道这个答案很旧,但 MIME::Lite 文档现在说:WAIT! MIME::Lite is not recommended by its current maintainer. There are a number of alternatives, like Email::MIME or MIME::Entity and Email::Sender, which you should probably use instead. MIME::Lite continues to accrue weird bug reports, and it is not receiving a large amount of refactoring due to the availability of better alternatives. Please consider using something else.
    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 2013-11-22
      • 2017-01-20
      • 2011-02-07
      • 2021-02-25
      • 2015-01-20
      • 2011-05-23
      • 1970-01-01
      • 2016-04-17
      • 2017-12-07
      相关资源
      最近更新 更多