【问题标题】:Inline images break when embedded from the htmlBody of an existing draft [GmailApp] [Apps Script]从现有草稿 [GmailApp] [Apps 脚本] 的 htmlBody 嵌入内嵌图像时会中断
【发布时间】:2021-04-23 23:44:33
【问题描述】:

我正在编写一个邮件合并脚本,该脚本接收带有GmailApp 的草稿邮件,获取其htmlBody 和附件,并使用这些来发送新邮件。

它适用于附件,甚至适用于从外部 URL 插入的内联消息(包括 Gmail 签名中的图像);但是,它无法处理使用 Gmail 中的“插入图像”面板直接插入到草稿邮件中的内嵌图像:这些图像会损坏。

使用getAttachments()includeInlineImages 选项只会更改相关内联图像是否附加到电子邮件,但无论如何,它在正文中已损坏。

代码摘录:

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

欢迎提出任何建议。

【问题讨论】:

    标签: google-apps-script


    【解决方案1】:

    图像的来源或映射未正确定义时会发生图像损坏。

    如果打印htmlBody,则每个内联图像都有cidcid 用于映射图像。

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

    要解决此问题,您必须在 GmailApp.sendEmail 中设置 inlineImages 的值,并且您的 html 中 cid 的值应与 inlineImages 中的图像键匹配。参见示例:link

    我创建了一个演示,介绍如何设置图像从草稿到新电子邮件的映射。

    示例草稿:

    代码:

    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]}
      });
      }  
    }
    

    输出:

    参考:

    sendEmail Advance Parameters

    【讨论】:

    • 非常感谢 Nikko - 它真的很有帮助,正是我所需要的!
    【解决方案2】:

    为了添加 Nikko J. 的上述解决方案,这里有一个获取 all 内联图像的代码:

    //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
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-24
      • 2019-06-12
      • 2020-02-14
      • 2017-04-12
      • 1970-01-01
      • 2020-11-19
      相关资源
      最近更新 更多