我想使用GmailMessage 的 getAttachments提取内联图像,然后通过Class MailApp 的 sendEmail发送它们。
问题是前者返回一个 Blob 附件数组,而后者需要一个 JavaScript 对象,其中包含从图像键(字符串)到图像数据 Blob 的映射。
示例代码 - 棘手的部分是{..., inlineImages: attachments, ...}
:
function test() {
var firstThread = GmailApp.getTrashThreads(0,1)[0];
var message = firstThread.getMessages()[0];
var from = message.getHeader('From');
var attachments = message.getAttachments({includeAttachments: false});
var obj = {htmlBody: message.getBody(), inlineImages: attachments, noReply: false, replyTo: message.getFrom(), name: from};
MailApp.sendEmail("some@one.com", 'Testing', '', obj);
}
示例 EML 消息(注意 text/html 的用法<img src="cid:key"
):
MIME-Version: 1.0
Date: Thu, 22 Jul 2021 23:06:23 +0300
Message-ID: <1234567890@mail.gmail.com>
Subject: Testing
From: Me <just@me.com>
To: Someone <some@one.com>
Content-Type: multipart/related; boundary="0000000000004aee5205c7bbd26b"
--0000000000004aee5205c7bbd26b
Content-Type: multipart/alternative; boundary="0000000000004aee5005c7bbd26a"
--0000000000004aee5005c7bbd26a
Content-Type: text/plain; charset="UTF-8"
This is an inline image:
[image: image.png]
And here's another:
[image: image.png]
Thanks!
--0000000000004aee5005c7bbd26a
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">This is an inline=C2=A0image:<div><div dir=3D"ltr" class=
=3D"gmail_signature" data-smartmail=3D"gmail_signature"><div dir=3D"ltr"></=
div></div></div><div><img src=3D"cid:ii_krfchaks0" alt=3D"image.png" width=
=3D"117" height=3D"118"><br></div><div>And here's another:</div><div><i=
mg src=3D"cid:ii_krfchlll1" alt=3D"image.png" width=3D"41" height=3D"47"><b=
r></div><div>Thanks!</div></div>
--0000000000004aee5005c7bbd26a--
--0000000000004aee5205c7bbd26b
Content-Type: image/png; name="image.png"
Content-Disposition: attachment; filename="image.png"
Content-Transfer-Encoding: base64
X-Attachment-Id: ii_krfchaks0
Content-ID: <ii_krfchaks0>
--0000000000004aee5205c7bbd26b
Content-Type: image/png; name="image.png"
Content-Disposition: attachment; filename="image.png"
Content-Transfer-Encoding: base64
X-Attachment-Id: ii_krfchlll1
Content-ID: <ii_krfchlll1>
--0000000000004aee5205c7bbd26b--