【问题标题】:Why does a PDF become corrupt/unreadable after mailing it through SMTP with Email::MIME?为什么 PDF 在使用 Email::MIME 通过 SMTP 邮寄后会损坏/不可读?
【发布时间】:2011-07-29 20:07:46
【问题描述】:

我已按照Email::SenderEmail::MIME 中的示例进行操作,看起来不错,直到您尝试打开 PDF。然后很明显,它的大小比原来的小,而且不知何故损坏了。我的脚本或多或少是为测试目的而给出的示例的模板副本,但我担心 MIME 的东西在这里不起作用。

use strict;
use warnings;

use Data::Dumper;
use IO::All ;

use Email::Simple;
use Email::Simple::Creator;

use Email::MIME;

use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTP;

# assemble the parts
my @parts = (
    Email::MIME->create(
        attributes => {
            filename     => "report.pdf",
            content_type => "application/pdf",
            encoding     => "quoted-printable",
            name         => "report.pdf",
        },
        body => io("report.pdf")->all
    ),
    Email::MIME->create(
        attributes => {
            content_type => "text/plain",
            disposition  => "attachment",
            charset      => "US-ASCII",
        },
        body => "Hello there!",
    ),
);

# assemble parts into email
my $email = Email::MIME->create(
    header => [
        To      => 'me@you.com',
        From    => 'me@you..com',
        Subject => "Thanks for all the fish ...",
    ],
    parts => [@parts],
);

# standard modifications
$email->header_set( 'X-PoweredBy' => 'RT v3.0' );

# more advanced
# $_->encoding_set('base64') for $email->parts;

# send the email
my $transport = Email::Sender::Transport::SMTP->new({
    host => 'mail.whatever.com',
    # port => 2525,
    sasl_username => 'webuser',
    sasl_password => 's3cr3t',
    timeout       => 20,
});
sendmail( $email, { transport => $transport } );

我使用的是 Windows 和 Perl 5.12.1.0。它似乎不是IO::All 模块,但我认为问题出在此处。有没有人足够了解这些东西来帮助我修复它?

我尝试过二进制模式、不同的 SMTP 服务器、不同的 PDF 文件,但我根本无法让这该死的东西正常工作。

【问题讨论】:

  • 你为什么注释掉encoding_set
  • 因为有或没有它,它没有任何区别......
  • 你试过用print $email->as_string;转储邮件吗?

标签: perl email smtp mime multipart


【解决方案1】:

您需要在发送电子邮件之前对二进制附件进行编码。

$_->encoding_set( 'base64' ) for $email->parts;

我不知道电子邮件::MIME。我使用MIME::Lite 并没有遇到任何问题,因为编码是自动完成的。

### Start with a simple text message:
$msg = MIME::Lite->new(
     From    =>'me@myhost.com',
     To      =>'you@yourhost.com',
     Cc      =>'some@other.com, some@more.com',
     Subject =>'A message with 2 parts...',
     Type    =>'TEXT',
     Data    =>"Here's the GIF file you wanted"
);

### Attach a part... the make the message a multipart automatically:
$msg->attach(Type     =>'image/gif',
     Path     =>'aaa000123.gif',
     Filename =>'logo.gif'
);

MIME::Lite->send('smtp', "smtp.myisp.net", AuthUser=>"YourName", AuthPass=>"YourPass");
$msg->send;

【讨论】:

  • 谢谢你的回复 PacoRG - 我可以看到你要去哪里 - 但在你拨打电话并传递电子邮件部分数组之前,你不会得到 $email 对象 - 完全混淆了这一切...
  • btw - 我刚刚尝试了MIME::Lite 示例,它可以工作:-) :-( - 该项目要求我使用 Email::Simple - 显然是因为它需要使用 gmail,而我相信 MIME::Lite 无法处理它。
  • 我编辑了我的答案。我的意思是在发送电子邮件之前,在Email::Sender::Transport::SMTP->new 之前,很抱歉让您感到困惑。
  • 好的,谢谢 - 放回去,但仍然不起作用 - 我得到一个空的 PDF 文档 - 你提到的 MIME::Lite 版本有效,但不适用于 gmail...啊啊啊!只是在这里似乎无法得到任何快乐..
猜你喜欢
  • 2022-09-24
  • 1970-01-01
  • 2019-12-14
  • 2020-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-14
相关资源
最近更新 更多