-2

I want to send an email with an image instead of an attachment through the Google Sheets spreadsheet.

Here is my code (this code is used to send emails with text to people according to the chosen column. I want to add an image sending option, but I don't know how):

function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 120;
  var numRows = 2;
  var dataRange = sheet.getRange(startRow, 1, numRows, 2)
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (i in data) {
    var row = data[i];
    var emailAddress = row[1];
    var message = row[5];
    var subject = row[6];
    MailApp.sendEmail(emailAddress, subject, message);
  }
}
Kheira
  • 7
  • 7
  • 1
    Does this answer your question? [How to include inline images in email using MailApp](https://stackoverflow.com/questions/36178369/how-to-include-inline-images-in-email-using-mailapp) – ale13 Aug 17 '20 at 11:29

1 Answers1

1

An example of inline images is shown in the sendMail docs.

You will need to use an HTML body for the email. This is best done through the HTML Service, but the example shows a simple version with just strings in the JavaScript file.

You will need to put <img> tags in your HTML body, and set the src attribute to cid:key where key is a label you choose.

Finally, you also need the photo BlobSource, which will be the value of the key you choose. In the example, this is accomplished through fetching the image from a URL.

Your call to MailApp.sendEmail will then become

var photoBlob = UrlFetchApp
  .fetch(urlForMyImage)
  .getBlob()
  .setName("myImage");

MailApp.sendEmail({
    to: emailAddress,
    subject: subject,
    htmlBody: message + "<img src='cid:myImage'>",
    inlineImages: { myImage: photoBlob }
  });
dwmorrin
  • 2,704
  • 8
  • 17