2

Could anyone help me?

I've created a Google Form that uploads a file (image) to a private drive folder, the form also sends responses to a Google Sheet including the image's URL. The code below gets the file id and sends the image as an attachment but it doesn't do it in the body of the email. The folder must be private.

function sendImage(){
  var sheet = SpreadsheetApp.getActive().getActiveSheet();
  var fileUrl = sheet.getRange('C2').getValue();
  var index = fileUrl.indexOf('=') + 1;
  var fileId = fileUrl.substring(index);
  var img = DriveApp.getFileById(fileId).getBlob();

 MailApp.sendEmail({
    name: "my Name",
    to: 'myEmail@email.com',
    subject: "Test 1",
    body:  img,
    attachments: img,
     });
}
1

How about this modification? You can send an email including images using inlineImages of Advanced parameters at MailApp.sendEmail(). You have already been able to retrieve the image blob. In this modified script, the blob is used.

Modified script:

function sendImage(){
  var sheet = SpreadsheetApp.getActive().getActiveSheet();
  var fileUrl = sheet.getRange('C2').getValue();
  var index = fileUrl.indexOf('=') + 1;
  var fileId = fileUrl.substring(index);
  var img = DriveApp.getFileById(fileId).getBlob();

  MailApp.sendEmail({
    name: "my Name",
    to: 'myEmail@email.com',
    subject: "Test 1",
    htmlBody: "<img src=\"cid:sampleImage\">", // Modified
    inlineImages: {sampleImage: img} // Modified
  });
}

Reference:

| improve this answer | |
  • @Edden Thank you for replying. I'm glad your issue was solved. Thank you, too. – Tanaike Sep 6 '18 at 22:04
1

For some reason, Tanaike's answer keeps giving me an error when running the line when it gets the blob using DriveApp. It only worked after I changed that line to get the file using

var img = UrlFetchApp.fetch(fileUrl).getBlob();

Just in case it helps someone from google like me.

| improve this answer | |

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.