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

发布于 2024-10-15 06:29:47 字数 425 浏览 3 评论 0原文

我很难相信这个问题在 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);

可以修改此代码块以处理多个附件吗?或者我是否必须使用特殊的模块来实现这一点?如果是这样,该模块是什么?我将如何编写它的脚本?

感谢您的帮助!

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....

Anyway, what Perl module should I use to attach multiple files to an email?

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千仐 2024-10-22 06:29:47

我最终使用 MIME::Lite 找到了一个示例 这里

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

my $SMTP_SERVER = 'smtp.server.com';             #change
my $DEFAULT_SENDER = '[email protected]';       #change
my $DEFAULT_RECIPIENT = '[email protected]'; #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

I ended up going with an example using MIME::Lite found here

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

my $SMTP_SERVER = 'smtp.server.com';             #change
my $DEFAULT_SENDER = '[email protected]';       #change
my $DEFAULT_RECIPIENT = '[email protected]'; #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
北陌 2024-10-22 06:29:47

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

See attach_file in Email::Stuff, or Email::MIME if you need more control.

哆啦不做梦 2024-10-22 06:29:47

尽管评价参差不齐,但我发现 Mail::Sender (而且它是朋友 < a href="http://search.cpan.org/~dmuey/Mail-Sender-Easy/" rel="nofollow">Mail::Sender::Easy)非常好用且简单易用,并且看起来它可以处理多个附件。

我发现 Mail::Internet< 中的界面非常烦人/a>.

不过,任何事情都应该比上面的更好。 :-)

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.

I found the interface to be extremely annoying in Mail::Internet.

Anything should be better than what you have above, though. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文