2

我已按照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 文件,但我根本无法让这该死的东西正常工作。

4

1 回答 1

4

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

$_->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;
于 2011-04-05T10:46:44.487 回答