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

发布于 2024-10-30 13:32:11 字数 2202 浏览 0 评论 0 原文

我已按照 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      => '[email protected]',
        From    => '[email protected]',
        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 文件,但我根本无法让这该死的东西工作。

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      => '[email protected]',
        From    => '[email protected]',
        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 } );

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万劫不复 2024-11-06 13:32:11

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

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

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

### Start with a simple text message:
$msg = MIME::Lite->new(
     From    =>'[email protected]',
     To      =>'[email protected]',
     Cc      =>'[email protected], [email protected]',
     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;

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    =>'[email protected]',
     To      =>'[email protected]',
     Cc      =>'[email protected], [email protected]',
     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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文