简体   繁体   English

从现有草稿 [GmailApp] [Apps 脚本] 的 htmlBody 嵌入内嵌图像时会中断

[英]Inline images break when embedded from the htmlBody of an existing draft [GmailApp] [Apps Script]

I am working on a mail merge script that takes a draft message with GmailApp , gets its htmlBody and attachments, and uses these to send a new message.我正在开发一个邮件合并脚本,该脚本使用GmailApp获取草稿消息,获取其htmlBody和附件,并使用这些来发送新消息。

It works fine with attachments, and even with inline messages that have been inserted from an outside url (including images in Gmail signature);它适用于附件,甚至适用于从外部 url 插入的内联消息(包括 Gmail 签名中的图像); however, it can't deal with inline images directly inserted into the draft message using the Insert image panel in Gmail: these images break.但是,它无法处理使用 Gmail 中的插入图像面板直接插入到草稿消息中的内联图像:这些图像中断。

Using the includeInlineImages option of getAttachments() only changes whether the inline image in question is attached to the email or not, but regardless, it is broken in the body.使用getAttachments()includeInlineImages选项只会更改相关内联图像是否附加到 email,但无论如何,它在正文中已损坏。

Code excerpt:代码摘录:

var allAttachments = draft.getMessage().getAttachments()
var htmlBody = draft.getMessage().getBody()
GmailApp.sendEmail(recipient, subject, '', {
              name:senderName,
              from:senderEmail,
              htmlBody:htmlBody,
              cc:allCc,
              bcc:bcc,
              attachments: allAttachments

Any suggestions are appreciated.任何建议表示赞赏。

Broken image occurs when the source or mapping of image is not defined correctly.当图像的来源或映射未正确定义时,会发生图像损坏。

If you print the htmlBody , each inline image has cid and cid is used to map the Image.如果您打印htmlBody ,则每个内联图像都有cid并且cid用于 map 图像。

Example: <div dir="ltr"><img data-surl="cid:xxsome_idxx" src="cid:xxsome_idxx" alt="dog.jpg" width="490" height="246"><br></div>示例: <div dir="ltr"><img data-surl="cid:xxsome_idxx" src="cid:xxsome_idxx" alt="dog.jpg" width="490" height="246"><br></div>

To fix the issue, you have to set the values of inlineImages in GmailApp.sendEmail and the value of cid in your html should match the key of image in inlineImages.要解决此问题,您必须在 GmailApp.sendEmail 中设置 inlineImages 的值,并且 html 中的 cid 值应与 inlineImages 中的图像键匹配。 See example: link见示例: 链接

I created a demo on how to set mapping of image from draft to a new email.我创建了一个演示,介绍如何将图像从草稿映射到新的 email。

Example Draft:示例草稿:

消息草稿

Code:代码:

function getInlineImagefromDraft() {
  var draft = GmailApp.getDrafts()[0];
  var allAttachments = draft.getMessage().getAttachments();
  var htmlBody = draft.getMessage().getBody();
  
  var searchstring = "img data-surl=\"cid:";
  //search string position
  var index = htmlBody.search(searchstring);
  if (index >= 0) {
    ////the goal of this section is to get the value of cid
    var pos = index + searchstring.length
    var id = htmlBody.substring(pos, pos + 15);
    //remove double quotes
    id = id.replace(/"/,"");
    //remove characters after space
    id = id.replace(/\s.*/g, "");
    
    //send email
    var recipient = 'someemail';
    var subject = 'testing only gmail';
    var senderName = 'testing name';
    var senderEmail = 'someemail';
    GmailApp.sendEmail(recipient, subject, '', {
                name:senderName,
                from:senderEmail,
                htmlBody:htmlBody,
                inlineImages: {[id]: allAttachments[0]}
  });
  }  
}

Output: Output: 输出

Reference:参考:

sendEmail Advance Parameters sendEmail 高级参数

And just to add to the above solution by Nikko J., here's a code to get all inline images:只是为了添加 Nikko J. 的上述解决方案,这里有一个获取所有内联图像的代码:

//Get all attachments for inline images
var allInlineImages = draft.getMessage().getAttachments({includeInlineImages: true,includeAttachments:false})
var justAttachments = draft.getMessage().getAttachments({includeInlineImages: false})

//Initiate the allInlineImages object
var inlineImagesObj = {}
//Regexp to search for all string positions 
var regexp = RegExp('img data-surl=\"cid:', 'g');
var indices = htmlBody.matchAll(regexp)

//Iterate through all matches
var i = 0;
for (const match of indices){
  //Get the start position of the CID
  var thisPos = match.index + 19
  //Get the CID
  var thisId = htmlBody.substring(thisPos, thisPos + 15).replace(/"/,"").replace(/\s.*/g, "")
  //Add to object
  inlineImagesObj[thisId] = allInlineImages[i]
  i++
}

GmailApp.sendEmail(recipient, subject, '', {
  name:senderName,
  from:senderEmail,
  htmlBody:htmlBody,
  cc:allCc,
  bcc:bcc,
  attachments: justAttachments,
  inlineImages: inlineImagesObj
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM