perl 如何在使用Email::Stuffer构建的电子邮件中引用附件图像?

piwo6bdm  于 2023-11-22  发布在  Perl
关注(0)|答案(1)|浏览(164)

我想用Email::Stuffer发送一封电子邮件,并在邮件的html部分引用一个附件图像。为此,我需要图像的MIME部分有一个ID,但我无法让Email::Stuffer添加一个。

#!perl
use strict;
use warnings;
use Email::Stuffer;

my $stuffer = Email::Stuffer
    ->from('[email protected]')
    ->to('[email protected]')
    ->subject('mcve')
    ->text_body('see attached image')
    ->attach_file('image.png');
print $stuffer->as_string(), "\n\n";

my @parts = $stuffer->email->subparts;
my $attachment = $parts[1];
my %headers = $attachment->header_str_pairs();
print "CID header: ", $headers{'Content-ID'}, "\n\n";

字符串
奇怪的是,它打印的东西看起来像一个内容id,即使它打印的mime结构没有Content-ID头:

Date: Sat, 28 Oct 2023 10:33:05 -0500
MIME-Version: 1.0
From: [email protected]
To: [email protected]
Subject: mcve
Content-Transfer-Encoding: 7bit
Content-Type: multipart/mixed; boundary=16985071850.4fFe46cEc.181699

--16985071850.4fFe46cEc.181699
Date: Sat, 28 Oct 2023 10:33:05 -0500
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

see attached image=

--16985071850.4fFe46cEc.181699
Date: Sat, 28 Oct 2023 10:33:05 -0500
MIME-Version: 1.0
Content-Type: image/png; name=image.png
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename=image.png

(some base64 data here)

--16985071850.4fFe46cEc.181699--

CID header: <16985071854.8a407.181699@perle>


如果我在查询内容id头之后打印,这不会有什么区别,所以请求一个并不会神奇地添加它。
当我使用attach_file('image.png', 'Content-ID' => 'my@id');而不是普通的attach时,它会向content-type头部添加一个content-id属性,如下所示:Content-Type: image/png; content-id=my@id; name=image.png
我尝试手动添加一个标题的图像部分

$headers{'Content-ID'} = $cid;
$attachment->header_str_set('Content-ID' => [$cid]);


但是当我将电子邮件打印为文本时,Content-ID标题仍然不显示。
如何让Email::StufferContent-ID标头添加到图像部分?

mklgxw1f

mklgxw1f1#

如何让Email::Stuffer将Content-ID头添加到图像部分?
我做了一些调试,下面的修改你的脚本似乎工作:

  • 使用$stuffer->parts而不是$stuffer->email->subparts
  • 使用$attachment->header_set('Content-ID', $cid);代替$attachment->header_str_set('Content-ID' => [$cid]);
    示例
# ... Previous part of script as before...
my @parts = $stuffer->parts;
my $attachment = $parts[1];
$attachment->header_set('Content-ID', "<image.png>");
print $stuffer->as_string(), "\n\n";

字符串

输出

[...]
Date: Sat, 28 Oct 2023 23:29:04 +0200
MIME-Version: 1.0
Content-Type: image/png; name=image.png
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename=image.png
Content-ID: <image.png>

[...]

相关问题