Why does a PDF become corrupt/unreadable after mailing it through SMTP with Email::MIME?
I have followed the examples in Email::Sender and Email::MIME and it looks good, until you try to open the PDF. Then it is clear that it is smaller in size than the original and somehow corrupt. My script is more or less a template copy of the examples given for testing purposes, but I fear that the MIME stuff does not really work here.
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 => 'webus开发者_运维知识库er',
sasl_password => 's3cr3t',
timeout => 20,
});
sendmail( $email, { transport => $transport } );
I am using Windows and Perl 5.12.1.0. It does not seem to be the IO::All module, but I think the issue is somewhere here. Does anyone know enough about this stuff to help me fix it?
I have tried binary mode, different SMTP servers, different PDF files, and I cannot get the damn thing to work at all.
You need to encode your binary attachments before sending the email.
$_->encoding_set( 'base64' ) for $email->parts;
I don't know Email::MIME. I use MIME::Lite and never had any problem because encoding is done automatically.
### 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;
精彩评论