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

问题描述 投票:0回答:1

我想发送一封带有

Email::Stuffer
的电子邮件,并参考电子邮件 html 部分中的附件图像。为此,我需要带有图像的 MIME 部分才能拥有 ID,但我无法
Email::Stuffer
添加 ID。

#!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-id
标头添加
content-type
属性,如下所示:
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::Stuffer
Content-ID
标题添加到图像部分?

perl email mime
1个回答
0
投票

如何让 Email::Stuffer 将 Content-ID 标头添加到图像部分?

我做了一些调试,对脚本的以下修改似乎有效:

  • 不要使用
    $stuffer->email->subparts
    ,而使用
    $stuffer->parts
  • 不要使用
    $attachment->header_str_set('Content-ID' => [$cid]);
    ,而使用
    $attachment->header_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>

[...]
© www.soinside.com 2019 - 2024. All rights reserved.