【问题标题】:Send multiple files using Email :: MIME in perl?在 perl 中使用电子邮件 :: MIME 发送多个文件?
【发布时间】:2020-04-03 03:52:25
【问题描述】:

我有一个脚本可以发送带有嵌入图像和 html 正文的电子邮件,但是当我尝试发送其他文件时,它们会被覆盖,这是我的 perl 代码

push @parts,
    Email::MIME->create(
         attributes => {
             content_type => $CtypeB,
             encoding     => $EncodingB,
             charset      => $CharsetB,
             },
         body => $BodyB,
        ),
    ;

  push @parts,
  Email::MIME->create(
        header_str=> [
            'Content-ID' => $Content_ID,
            ],
        attributes => {
            content_type => $CtypeF,
            encoding     => $EncodingF,
            'Content-Disposition' => $ContentD,
            filename=> $FilenameF,
            file => $FileF,
            },
        body => io($BodyF)->binary->all,
      );

    Email::MIME->create(
        header_str => [
            To => $Para,
            From => $De,
            Subject => $Asunto,
            Cc => $Cc,
            Bcc => $Bcc
            ],
        attributes => {
             content_type =>$CtypeH
        },
        parts => [@parts]
    ), 

我假设作为在安排中创建的 Email :: MIME 的元素,这些将使用推送来适应,但是当我通过添加多个文件发送邮件时,它会被覆盖,也就是说,它发送最后一个附件,任何关于我做错了什么的想法或建议?,谢谢。

【问题讨论】:

  • 提示:使用\@parts 而不是[@parts] 可以避免不必要的复制
  • 我不明白你对最终创建的返回做了什么;这可能是问题吗?除此之外,它看起来还可以。你能想出一个简短的可运行脚本来演示你的问题吗?
  • 使用Email::Stuffer 会更简单。

标签: arrays perl email email-attachments mime


【解决方案1】:

您可以像这样附加多个文件:

use strict;
use warnings;
use Email::MIME;
my @parts;
push @parts, Email::MIME->create(
    attributes => {
        content_type => 'application/octet-stream',
        disposition => 'attachment',
        encoding => 'base64',
        name => 'foobar.bin',
    },
    body => "\1\2\3\4\5\6\7",
);
push @parts, Email::MIME->create(
    attributes => {
        content_type => 'text/html',
        disposition => 'attachment',
        encoding => 'quoted-printable',
        charset => 'UTF-8',
        name => 'foobar.html',
    },
    body_str => "<!DOCTYPE html><title>foo bar</title><p>foo</p><p>bar</p>",
);
my $message = Email::MIME->create(
    header_str => [
        To => 'you@example.com',
        From => 'me@example.com',
        Subject => 'foo bar',
    ],
    parts => \@parts,
);
print $message->as_string;

这会产生以下消息:

To: you@example.com
From: me@example.com
Subject: foo bar
Date: Mon, 9 Dec 2019 21:16:43 -0800
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Type: multipart/mixed; boundary="15759550030.CE8DFF428.4203"


--15759550030.CE8DFF428.4203
Date: Mon, 9 Dec 2019 21:16:43 -0800
MIME-Version: 1.0
Content-Type: application/octet-stream; name="foobar.bin"
Content-Transfer-Encoding: base64
Content-Disposition: attachment

AQIDBAUGBw==

--15759550030.CE8DFF428.4203
Date: Mon, 9 Dec 2019 21:16:43 -0800
MIME-Version: 1.0
Content-Type: text/html; charset="UTF-8"; name="foobar.html"
Content-Disposition: attachment
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE html><title>foo bar</title><p>foo</p><p>bar</p>=

--15759550030.CE8DFF428.4203--

【讨论】:

    猜你喜欢
    • 2011-05-23
    • 2020-06-30
    • 1970-01-01
    • 2013-02-10
    • 2014-09-29
    • 2017-01-20
    • 2013-11-22
    • 2020-11-17
    • 2017-08-07
    相关资源
    最近更新 更多