使用Email :: MIME通过SMTP邮寄后,为什么PDF会损坏/不可读?

时间:2011-04-05 09:59:33

标签: perl email smtp mime multipart

我已按照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文件,而且我根本无法使用该死的东西。

1 个答案:

答案 0 :(得分:4)

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

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

我不知道Email :: 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;