1

Hello friendly StackOverflow folks,

I am having the most difficult time getting the GmailApp sendEmail() method to successfully use an existing email (e.g. a draft) that contains images inline as a template for new messages.

It seems like this is a problem, as I have found devs having this problem here and here and proposed solutions here, here, and here. Each of these solutions is 4+ years old, so perhaps they're out of date. I have been unable to use any of these solutions to replicate a success.

Currently, I'm running this code from my Google Scripts backend:

function generateMessageFromTemplate () {
  var selectedTemplate = GmailApp.getMessageById('MESSAGE_ID');

  //////////////////////////////////////////////////////////////////////////////
  // Get inline images and make sure they stay as inline images (via Romain Vialard)
  //////////////////////////////////////////////////////////////////////////////
  var emailTemplate = selectedTemplate.getBody();
  var rawContent = selectedTemplate.getRawContent();
  var attachments = selectedTemplate.getAttachments();

  var regMessageId = new RegExp(selectedTemplate.getId(), "g");
  if (emailTemplate.match(regMessageId) != null) {
    var inlineImages = {};
    var nbrOfImg = emailTemplate.match(regMessageId).length;
    var imgVars = emailTemplate.match(/<img[^>]+>/g);
    var imgToReplace = [];
    if(imgVars != null){
      for (var i = 0; i < imgVars.length; i++) {
        if (imgVars[i].search(regMessageId) != -1) {
          var id = imgVars[i].match(/realattid=([^&]+)&/);
          if (id != null) {
            var temp = rawContent.split(id[1])[1];
            temp = temp.substr(temp.lastIndexOf('Content-Type'));
            var imgTitle = temp.match(/name="([^"]+)"/);
            if (imgTitle != null) imgToReplace.push([imgTitle[1], imgVars[i], id[1]]);
          }
        }
      }
    }
    for (var i = 0; i < imgToReplace.length; i++) {
      for (var j = 0; j < attachments.length; j++) {
        if(attachments[j].getName() == imgToReplace[i][0]) {
          inlineImages[imgToReplace[i][2]] = attachments[j].copyBlob();
          attachments.splice(j, 1);
          var newImg = imgToReplace[i][1].replace(/src="[^\"]+\"/, "src=\"cid:" + imgToReplace[i][2] + "\"");
          emailTemplate = emailTemplate.replace(imgToReplace[i][1], newImg);
        }
      }
    }
  }
  //////////////////////////////////////////////////////////////////////////////

    GmailApp.sendEmail('test@email.com', selectedTemplate.getSubject(), '', {
      attachments: attachments,
      htmlBody: emailTemplate,
      inlineImages: inlineImages
    });
};

The Google Scripts documentation on the sendEmail() method is here.

This is the Output of this Function as Is

When I send emails from Apps Script as is, I get emails that look like this: screenshot I've replicated the test with an old yahoo.com email account and had the exact same results as a Gmail account.

Again, this dev also has this same issue.

If you can help, I would be extremely grateful!

Davis Jones
  • 1,504
  • 3
  • 17
  • 25
  • Is your issue to show an image as the inline image of email? Or Do you want to debug your script before ``GmailApp.sendEmail()``? If it's the former, how about testing it using a simple script? If it's the latter, can you provide the detail information for replicating your situation? If these were not the direction you want, I apologize. – Tanaike Jun 01 '19 at 01:09
  • HI @Tanaike. The issue is getting the inline images to show. I've tested many different proposed solutions (linked in my initial question), but none of them are working for me. I'm wondering if Google has recently changed something that's breaking theses solutions. – Davis Jones Jun 03 '19 at 20:47
  • Thank you for replying. I could understand about your situation. For example, how about testing it using a simple script for inserting the inline images? – Tanaike Jun 03 '19 at 21:56

0 Answers0