google-apps-scriptpostinline-images

Google Apps Script doPost image object empty


I have a deployed Web App on a generic Google account I wish to work as a mail server for automation projects. It works well but I can't get inlineImages objects to carry over the POST request. The image object always comes through empty. Can someone explain what's up and whether there's a way to carry images over a POST request to include (inline) in emails for use with a GAS Web App like this one?

Server code (simplified):

function doPost(e) {
  const data = JSON.parse(e.postData.contents);
  const { apikey, recipient, subject, body, images } = data;

  console.log(images) // Always outputs {'img_map: {}}

  MailApp.sendEmail(recipient, subject, "", {name: "TSL mail server", htmlBody: body, inlineImages: images});
  return ContentService.createTextOutput('Email sent successfully.');
}

Client code:

// DriveApp.getFiles()
function myPost() {
  const map = DriveApp.getFileById(MYFILE_ID).getAs('image/png');
  const postData = {
    'apikey': APIKEY,
    'recipient': 'me@example.com',
    'subject': 'Testing GAS mail server',
    'body': '<h1>Hello, Mail World!</h1><img src="cid:img_map"/>',
    'images': {'img_map': map}
  };
  var options = {
    'method' : 'POST',
    'payload' : JSON.stringify(postData),
    'headers': { Authorization: `Bearer ${ScriptApp.getOAuthToken()}` },
    'muteHttpExceptions': true
  };
  const response = UrlFetchApp.fetch(MAIL_ENDPOINT, options);
  console.log(response.getResponseCode());
  console.log(response.getContentText());
}

Solution

  • I believe your goal is as follows.

    In this case, how about the following modification? In this modification, the image file is sent as base64 data. The modified script is as follows.

    doPost

    function doPost(e) {
      const data = JSON.parse(e.postData.contents);
      let { apikey, recipient, subject, body, images } = data;
      images = { img_map: Utilities.newBlob(Utilities.base64Decode(images.img_map), 'image/png') };
    
      console.log(images) // Always outputs {'img_map: {}}
    
      MailApp.sendEmail(recipient, subject, "", { name: "TSL mail server", htmlBody: body, inlineImages: images });
      return ContentService.createTextOutput('Email sent successfully.');
    }
    

    myPost

    function myPost() {
      const map = Utilities.base64Encode(DriveApp.getFileById(MYFILE_ID).getAs('image/png').getBytes());
      const postData = {
        'apikey': APIKEY,
        'recipient': 'me@example.com',
        'subject': 'Testing GAS mail server',
        'body': '<h1>Hello, Mail World!</h1><img src="cid:img_map"/>',
        'images': { 'img_map': map }
      };
      var options = {
        'method': 'POST',
        'payload': JSON.stringify(postData),
        'headers': { Authorization: `Bearer ${ScriptApp.getOAuthToken()}` },
        'muteHttpExceptions': true
      };
      const response = UrlFetchApp.fetch(MAIL_ENDPOINT, options);
      console.log(response.getResponseCode());
      console.log(response.getContentText());
    }
    

    Note:

    References: