【问题标题】:How do I attach multiple files to an email using Perl?如何使用 Perl 将多个文件附加到电子邮件中?
【发布时间】:2011-01-31 15:57:13
【问题描述】:

我很难相信这个问题在 SO 上不存在,但我找不到 Perl 的实例或类似的实例......

无论如何,我应该使用哪个 Perl 模块将多个文件附加到电子邮件中?

目前,我正在使用此代码发送带有单个附件的电子邮件,但我不知道如何修改它以处理多个附件:

my $mail_fh = \*MAIL;
open $mail_fh, "|uuencode $attachment $attachment |mailx -m -s \"$subject\" -r $sender $recipient";
print $mail_fh $message;
close($mail_fh);

可以修改此代码块以处理多个附件吗?还是我必须使用一个特殊的模块来完成这个?如果是这样,模块是什么,我将如何编写脚本?

感谢您的帮助!

【问题讨论】:

  • CPAN 上有无数的邮件模块 - 选择一个并使用它。并且不要使用uuencode;它被 Base-64 编码取代有很多很好的理由。

标签: perl email perl-module attachment mailx


【解决方案1】:

我最终使用MIME::Lite 找到了here 的示例

use MIME::Lite;
use Getopt::Std;

my $SMTP_SERVER = 'smtp.server.com';             #change
my $DEFAULT_SENDER = 'default@sender.com';       #change
my $DEFAULT_RECIPIENT = 'default@recipient.com'; #change

MIME::Lite->send('smtp', $SMTP_SERVER, Timeout=>60);

my (%o, $msg);

# process options

getopts('hf:t:s:', \%o);

$o{f} ||= $DEFAULT_SENDER;
$o{t} ||= $DEFAULT_RECIPIENT;
$o{s} ||= 'Files';

if ($o{h} or !@ARGV) {
    die "usage:\n\t$0 [-h] [-f from] [-t to] [-s subject] files ...\n";
}

# construct and send email

$msg = new MIME::Lite(
    From => $o{f},
    To   => $o{t},
    Subject => $o{s},
    Data => "Data",
    Type => "multipart/mixed",
);

while (@ARGV) {
  $msg->attach('Type' => 'application/octet-stream',
               'Encoding' => 'base64',
               'Path' => shift @ARGV);
}

$msg->send(  );

示例用法:

./notify_mime.pl -f cheese -t queso -s subject /home/id/cheeseconqueso/some_dir/example1.xls /home/id/cheeseconqueso/some_other_dir/*.xls

【讨论】:

    【解决方案2】:

    如果您需要更多控制权,请参阅 Email::Stuff 中的 attach_fileEmail::MIME

    【讨论】:

      【解决方案3】:

      尽管评分参差不齐,但我发现Mail::Sender(它是朋友Mail::Sender::Easy)非常好用且简单易用,而且看起来可以处理多个附件。

      我发现Mail::Internet的界面非常烦人。

      不过,任何东西都应该比你上面的更好。 :-)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-22
        • 2011-04-29
        • 2011-05-07
        • 2020-01-14
        相关资源
        最近更新 更多