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

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

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

  1. #!perl
  2. use strict;
  3. use warnings;
  4. use Email::Stuffer;
  5. my $stuffer = Email::Stuffer
  6. ->from('[email protected]')
  7. ->to('[email protected]')
  8. ->subject('mcve')
  9. ->text_body('see attached image')
  10. ->attach_file('image.png');
  11. print $stuffer->as_string(), "\n\n";
  12. my @parts = $stuffer->email->subparts;
  13. my $attachment = $parts[1];
  14. my %headers = $attachment->header_str_pairs();
  15. print "CID header: ", $headers{'Content-ID'}, "\n\n";

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

  1. Date: Sat, 28 Oct 2023 10:33:05 -0500
  2. MIME-Version: 1.0
  3. From: [email protected]
  4. To: [email protected]
  5. Subject: mcve
  6. Content-Transfer-Encoding: 7bit
  7. Content-Type: multipart/mixed; boundary=16985071850.4fFe46cEc.181699
  8. --16985071850.4fFe46cEc.181699
  9. Date: Sat, 28 Oct 2023 10:33:05 -0500
  10. MIME-Version: 1.0
  11. Content-Type: text/plain; charset=utf-8
  12. Content-Transfer-Encoding: quoted-printable
  13. see attached image=
  14. --16985071850.4fFe46cEc.181699
  15. Date: Sat, 28 Oct 2023 10:33:05 -0500
  16. MIME-Version: 1.0
  17. Content-Type: image/png; name=image.png
  18. Content-Transfer-Encoding: base64
  19. Content-Disposition: inline; filename=image.png
  20. (some base64 data here)
  21. --16985071850.4fFe46cEc.181699--
  22. 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
我尝试手动添加一个标题的图像部分

  1. $headers{'Content-ID'} = $cid;
  2. $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]);
    示例
  1. # ... Previous part of script as before...
  2. my @parts = $stuffer->parts;
  3. my $attachment = $parts[1];
  4. $attachment->header_set('Content-ID', "<image.png>");
  5. print $stuffer->as_string(), "\n\n";

字符串

输出

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

展开查看全部

相关问题