简体   繁体   English

如何使用Perl将多个文件附加到电子邮件?

[英]How do I attach multiple files to an email using Perl?

I find it hard to believe that this question doesn't exist on SO, but I couldn't find an instance or one similar for Perl.... 我很难相信这个问题在SO上不存在,但是我找不到Perl的实例或类似实例。

Anyway, what Perl module should I use to attach multiple files to an email? 无论如何,我应该使用哪个Perl模块将多个文件附加到电子邮件?

Currently, I'm using this code to send an email with a single attachment, but I couldn't figure out how to modify it to handle multiple attachments: 目前,我正在使用此代码发送带有单个附件的电子邮件,但是我不知道如何修改它以处理多个附件:

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

Can this code block be modified to handle multiple attachments? 可以修改此代码块以处理多个附件吗? Or do I have to use a special module to pull this off? 还是我必须使用特殊的模块才能完成此任务? If so, what is the module and how would I script it out? 如果是这样,什么是模块,我该如何编写脚本?

Thanks for any help! 谢谢你的帮助!

I ended up going with an example using MIME::Lite found here 我最后举了一个使用MIME::Lite的示例,在这里找到

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(  );

example usage: 用法示例:

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

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

Despite mixed ratings, I've found Mail::Sender (and it's pal Mail::Sender::Easy ) darn good and straightforward to use, and looks like it can handle multiple attachments. 尽管评分不一,但我发现Mail :: Sender (它是好朋友Mail :: Sender :: Easy )使用起来很简单好用,看起来可以处理多个附件。

I found the interface to be extremely annoying in Mail::Internet . 我发现Mail :: Internet中的界面非常烦人。

Anything should be better than what you have above, though. 不过,任何事情都应该比上面的要好。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM