【问题标题】:How to send pdf from URL using perl and sendmail [Mail::Sendmail]如何使用 perl 和 sendmail 从 URL 发送 pdf [Mail::Sendmail]
【发布时间】:2020-12-21 22:05:10
【问题描述】:

我有一个需要更改的旧版应用。它是用 perl 编写的,使用 send::mail 向用户发送邮件。以前我们在电子邮件正文中发送链接,但现在他们需要 pdf 附件。 PDF 是使用 php 在另一台服务器上生成的。

工作流程是

  1. 创建电子邮件正文
  2. 通过 URL 从另一个服务器获取 pdf
  3. 将 pdf 作为电子邮件的附件添加
  4. 发送。

我觉得可以用

use LWP::Simple;
unless (defined ($content = get $URL)) {
    die "could not get $URL\n";
}

获取 URL 的内容,但我不知道如何将该 var 用作 sendmail 中的附件。当前的发送邮件代码是:

my %maildata = (To  => $to,
        From    => 'OurSite - Billing <billing@ourSite.com>',
        Organization => 'OurSite, LLC      http://www.OurSite.com/',
        Bcc => 'sent-billing@ourSite.com',
        Subject => $subject{$message} || 'ourSite invoice',
        Message => $body
           );
print STDERR "notify1 now calling sendmail\n";
sendmail(%maildata) || print STDERR $Mail::Sendmail::error;

我遇到的另一个问题是我不知道如何确定我拥有的 sendmail 版本(旧的 freebsd 系统)是否甚至能够发送附件?

【问题讨论】:

  • 如果您的代码可以生成有效的 MIME 消息,比您自己更早的 Sendmail 版本将很乐意提供它。
  • 我 52 岁,你确定吗?哈哈
  • 那么你有一些余量 (-: 但无论如何,如果你的邮件服务器能够与其他邮件服务器互操作,它会很好地处理任何有效的 SMTP 消息,而不知道或关心什么它的内容是(排除大小限制;例如,某些邮件服务器被配置为不允许大于 50 MB 的邮件)。

标签: perl email sendmail mime


【解决方案1】:

好的,感谢那些给我一些方向/愿意尝试的海报。

最后,我构建了 mime 主体,执行以下操作

use LWP::Simple;
use MIME::Base64;

unless (defined ($content = get $URL)) {
    die "could not get $URL\n";
} 
my $pdfencoded = encode_base64($content);  

my %maildata = (To  => $to,
        From    => 'OurSite - Billing <billing@ourSite.com>',
        Organization => 'OurSite, LLC      http://www.OurSite.com/',
        Bcc => 'sent-billing@ourSite.com',
        Subject => $subject{$message} || 'ourSite invoice',

           );

my $boundary = "====" . time() . "====";

$maildata{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
$maildata{'Message'} = "--".$boundary."\n"."Content-Type: text/plain\n".$body.
"\n--".$boundary."\nContent-Transfer-Encoding: base64\nContent-Type: 
application/pdf; name=\"invoice.pdf\"\n".$pdfencoded."\n--".$boundary."--";

sendmail(%maildata) || print STDERR $Mail::Sendmail::error;

这给了我为正文内容手动构建的 MIME 格式。

感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-20
    • 2015-07-19
    • 2012-07-24
    • 2013-11-22
    • 1970-01-01
    • 2014-12-10
    • 2011-09-14
    • 1970-01-01
    相关资源
    最近更新 更多