为什么 PDF 在使用 Email::MIME 通过 SMTP 邮寄后会损坏/无法读取?

Mik*_*ill 3 email perl mime smtp 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 } );
Run Code Online (Sandbox Code Playgroud)

我正在使用 Windows 和 Perl 5.12.1.0。它似乎不是IO::All模块,但我认为问题出在这里。有谁对这个东西足够了解来帮助我解决它吗?

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

Fra*_*o R 5

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

$_->encoding_set( 'base64' ) for $email->parts;
Run Code Online (Sandbox Code Playgroud)

我不知道电子邮件::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;
Run Code Online (Sandbox Code Playgroud)