0

I'm trying to set up a Google Sheet with some data to email images. I have the code I found online working below to send a single image attachment but I would like to send more than one image at a time if it is possible. How could I change the code below to work with multiple images? I'm totally new to this and like I said just found this code online. I have no idea what I'm doing and surprised myself getting this part to work. Any help getting multiple images to work?

 function emailImage(){
  var EMAIL_SENT = "EMAIL_SENT";

   var sheet = SpreadsheetApp.getActiveSheet();
    var startRow = 2;
    var numRows = sheet.getLastRow();
   // Fetch the range of cells
   var dataRange = sheet.getRange(startRow, 1, numRows, 5)  
   var data = dataRange.getValues(); 

  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var emailAddress = row[0];  // First column
    var subject = row[1];       // Second column
    var message = row[2];       // Third column
    var image = UrlFetchApp.fetch(row[3]).getBlob();   // Fourth column
    var emailSent = row[4];     // Fifth column
    if (emailSent != EMAIL_SENT) {  // Prevents sending duplicates
      MailApp.sendEmail(emailAddress, subject, message, {attachments: [image]});
      sheet.getRange(startRow + i, 5).setValue(EMAIL_SENT);
      // Make sure the cell is updated right away in case the script is interrupted
      SpreadsheetApp.flush();
    }
  }
}
0

The attachments property in the advanced options parameter takes an [array] of blobs, so you can use something like this to attach multiple images:

MailApp.sendEmail(emailAddress, subject, message, {attachments: [image1, image2, image3]});

Some of the best resources for learning Google Apps Script include the Beginner's Guide, the New Apps Script Editor guide, the Fundamentals of Apps Script with Google Sheets codelab, the Extending Google Sheets pagejavascript.infoMozilla Developer Network and Apps Script at Stack Overflow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.