1

我想使用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&#39;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--
4

2 回答 2

1

原来 Google Apps 脚本支持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);
}
于 2021-07-22T21:24:49.153 回答
0

根据 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
  });
}

垃圾邮件:

垃圾

发送邮件:

发送

参考:

于 2021-07-22T17:54:05.043 回答