如何将内联图像从 Class GmailMessage 的 getAttachments 传递到 Class MailApp 的 sendEmail?
How to pass inline images from Class GmailMessage's getAttachments to Class MailApp's sendEmail?
我想使用 GmailMessage's getAttachments and then send them via Class MailApp's sendEmail 提取内联图像。
问题是前者 returns 一个 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 消息(注意 <img src="cid:key"
的 text/html 用法):
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--
根据 Cooper 的评论,您需要遍历数组。然后生成一个字符串,其中包含带有唯一键的标签。请参阅下面的代码。
代码:
function test() {
var firstThread = GmailApp.getTrashThreads(0,1)[0];
var message = firstThread.getMessages()[0];
var from = message.getHeader('From');
var attachments = message.getAttachments();
// contains key - blob pair
var images = {};
// contains the string built up containing usage of inlineImages
var imageString = '';
// used for assigning unique name per attachment
var index = 1;
for (attachment of attachments) {
// create unique name per attachment
var key = 'image' + index;
// assign blob of each attachment to the unique key
images[key] = attachment.copyBlob();
// generate string containing the inline image tag
imageString += key + " attachment <img src='cid:" + key + "'><br>";
index++;
}
MailApp.sendEmail({
to: "user@domain.com",
subject: "Testing",
// appended the string generated to the original body
htmlBody: message.getBody() + imageString,
// images obj assigned to inlineImages
inlineImages: images,
noReply: false,
replyTo: message.getFrom(),
name: from
});
}
已删除的邮件:
已发邮件:
参考:
原来 Google Apps Script 支持 matchAll,这意味着它可以像这样被拉掉:
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}), inline_images = {};
var regex = new RegExp('<img src="cid:(.*?)"', 'ig');
var i=-1, matches = message.getBody().matchAll(regex)
for (const match of matches) {
i++;
inline_images[match[1]] = attachments[i].copyBlob();
}
var obj = {htmlBody: message.getBody(), inlineImages: attachments, noReply: false, replyTo: message.getFrom(), name: from};
MailApp.sendEmail("some@one.com", 'Testing', '', obj);
}
我想使用 GmailMessage's getAttachments and then send them via Class MailApp's sendEmail 提取内联图像。
问题是前者 returns 一个 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 消息(注意 <img src="cid:key"
的 text/html 用法):
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--
根据 Cooper 的评论,您需要遍历数组。然后生成一个字符串,其中包含带有唯一键的标签。请参阅下面的代码。
代码:
function test() {
var firstThread = GmailApp.getTrashThreads(0,1)[0];
var message = firstThread.getMessages()[0];
var from = message.getHeader('From');
var attachments = message.getAttachments();
// contains key - blob pair
var images = {};
// contains the string built up containing usage of inlineImages
var imageString = '';
// used for assigning unique name per attachment
var index = 1;
for (attachment of attachments) {
// create unique name per attachment
var key = 'image' + index;
// assign blob of each attachment to the unique key
images[key] = attachment.copyBlob();
// generate string containing the inline image tag
imageString += key + " attachment <img src='cid:" + key + "'><br>";
index++;
}
MailApp.sendEmail({
to: "user@domain.com",
subject: "Testing",
// appended the string generated to the original body
htmlBody: message.getBody() + imageString,
// images obj assigned to inlineImages
inlineImages: images,
noReply: false,
replyTo: message.getFrom(),
name: from
});
}
已删除的邮件:
已发邮件:
参考:
原来 Google Apps Script 支持 matchAll,这意味着它可以像这样被拉掉:
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}), inline_images = {};
var regex = new RegExp('<img src="cid:(.*?)"', 'ig');
var i=-1, matches = message.getBody().matchAll(regex)
for (const match of matches) {
i++;
inline_images[match[1]] = attachments[i].copyBlob();
}
var obj = {htmlBody: message.getBody(), inlineImages: attachments, noReply: false, replyTo: message.getFrom(), name: from};
MailApp.sendEmail("some@one.com", 'Testing', '', obj);
}