3

What is the correct syntax for emailing an image url in a row as an attachment?

I'm trying to loop through all rows within a Google Sheet and attach an image url as the image. I can send the correct subject line and message body using the below code (minus the attachment stuff below), but when I try and add the attachment stuff it doesn't let me save it and says "Missing : after property ID. (line 22, file "Code")" which is on the line that reads "attachments: attachment, blob]"

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

   for (i in data) {
     var row = data[i];
     var emailAddress = "[email protected]";
     var subject =  row[0];
     var message = row[1].toString();
     var attachment = UrlFetchApp.fetch(row[2]).getBlob();   

     MailApp.sendEmail(emailAddress, subject, message, {
       name: 'Automatic Emailer Script',
       attachments: attachment, blob]  
     });  
   } // for
} // sendEmails

1 Answer 1

2

You need to put the blobs in an array.

 MailApp.sendEmail(emailAddress, subject, message, {
   name: 'Automatic Emailer Script',
   attachments: [attachment]  
 });  
4
  • I think that will work, but in my code above the attachment variable isn't set properly. I get an error "Blob object must have non-null content type for this operation.". Alternately If I simply get the attachment like "var attachment = row[2];" I get an error "Invalid argument: inlineImages".
    – jaybong
    Commented Jun 17, 2015 at 19:25
  • row[2] has to be a proper URL. Commented Jun 18, 2015 at 6:02
  • Thanks, that was one of the problems (fixed by making numcolumns dynamic), but I then hard coded the variable attachments and still got the error. I even put a valid image url directly in the sendEmail function and debug fails on the first line of that function complaining about inlineImages again. GmailApp.sendEmail(emailAddress, subject, message, { name: 'Automatic Emailer Script', attachments: ["http://placehold.it/400"] });
    – jaybong
    Commented Jun 18, 2015 at 12:41
  • Figured this out. I wasn't setting the blob object initially as outlined here. developers.google.com/apps-script/reference/mail/…. Once that was set then the send function looks like: GmailApp.sendEmail(emailAddress, subject, message, { name: 'Auto Emailer', inlineImages: { image: imageBlob } });
    – jaybong
    Commented Jun 18, 2015 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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