Apps Script - inline Images

500 views Asked by At

I am trying to add an inline image to an email, which is working, but it's adding the image as attachment instead. Am I doing something wrong here?

function sendEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet1=ss.getSheetByName('Data');
var n=sheet1.getLastRow();
for (var i = 2; i < n+1 ; i++ ) {
var emailAddress = sheet1.getRange(i,1).getValue();
 
var html = HtmlService.createTemplateFromFile('Document.html');
var img = DriveApp.getFileById('1NgViZuezxzqugtGFp8vPXe234s00ZNwg').getBlob();

var aliases = GmailApp.getAliases();
Logger.log(aliases);
 
GmailApp.sendEmail(emailAddress, 'Annual Review', html,{
  htmlBody: html.evaluate().getContent(),
  name: "Administrators",
  from: aliases[0],
  inlineImages: {image: img}
});
}
}
1

There are 1 answers

0
Rafa Guillermo On

Answer:

The HTML portion of the email needs to have the img tags for the files.

Code Example:

function myFunction() {
  const image = DriveApp.getFileById("<id-of-file>").getBlob().setName('name')

  const html =  '<body>' + 'Test <img src="cid:image">' + '</body>'

  GmailApp.sendEmail('[email protected]', 'subject', 'message body', {
    htmlBody: html,
    inlineImages: {
      'image': image
    }
    from: aliases[0]
  })
}