0

Hi im having trouble in sending email with image attachment. because the image file name is randomized and have no way to find out if the body of my message fits the image it will send from my drive. Here is a step by step of the process i have done:

  1. online form integration to google spreadsheet (done)
  2. online form to google drive (done) (images from each row of spreadsheet are saved by folder with folder name contains a unique id that is also present in the spreadsheet cell of each row)
  3. What i would like to do here is get the images of folder in google drive by a.(searching the folder name which contains a ceratin text) b.(getting the folder contents.)(all are images) c.(attaching the contents of the folder to the email .)

Example:

function send() {
  var picture1 = DriveApp.getFilesByName('snottyboy.jpg');
  var picture2     = DriveApp.getFilesByName('daryl.jpg');
  var recipientsTO = "fgh@gmail.com" + "," + "sdd@gmail.com"+ "," + "spaz@gmail.com"+ "," + "def@gmail.com"+ "," + "abc@gmail.com";
  MailApp.sendEmail({
    to:recipientsTO, 
    subject: "LOOK A LIKE",   
    body:"Final Message",  
    attachments: [picture1.next(),picture2.next()]
  });
}

Thank you for your help.

See image:enter image description here

user6537067
  • 357
  • 2
  • 6
  • 16

1 Answers1

2

To attach a file, you use File.getBlob() to attach it as a blob. For example:

attachments: [picture1.next().getBlob(),picture2.next().getBlob()]

If you know the exact id of a file (e.g. '0BxDqyd_bUCmvN1E3N0dQOWgycEF'), you can get it as a blob like this:

var picture3Blob = DriveApp.getFileById('0BxDqyd_bUCmvN1E3N0dQOWgycEF').getBlob();

Here's a working example:

function sendPics() {
  var picture1 = DriveApp.getFileById('0BxDqyd_bUCmvN1E3N0dQOWgycFE'); //public with link
  var picture2 = DriveApp.getFileById('0BxDqyd_bUCmvTFNjRkRXbXA2Tms'); //public with link
   MailApp.sendEmail({
     to: 'testa@example.com, testb@example.com', 
     subject: "This is a test", 
     body:"Test message",
     attachments: [picture1.getBlob(), picture2.getBlob()]
  });
}

and here's an example of the pictures being added inline instead of as attachments:

function sendPicsInline() {
  var picture1 = DriveApp.getFileById('0BxDqyd_bUCmvN1E3N0dQOWgycFE'); //public with link
  var picture2 = DriveApp.getFileById('0BxDqyd_bUCmvTFNjRkRXbXA2Tms'); //public with link
  var inlineImages = {};
  inlineImages[picture1.getId()] = picture1.getBlob();
  inlineImages[picture2.getId()] = picture2.getBlob();
   MailApp.sendEmail({
     to: 'testa@example.com, testb@example.com', 
     subject: "This is a test", 
     body:"Test message",
     htmlBody: 'Test message with pics inline <br>' +
     'first:<br><img src="cid:' + picture1.getId() + '" /><br>' +
     'second:<br><img src="cid:' + picture2.getId() + '" />',
     inlineImages: inlineImages   
  });
}
Peter
  • 5,501
  • 2
  • 26
  • 42
  • Thanks alot @peter just have a question what if some part of the folder name is what i know? and dont know the Id. of its contents or name? – user6537067 Dec 23 '16 at 01:21
  • Could you please accept this answer or edit your question to clearly give an example of what clarification you need. – Peter Dec 23 '16 at 01:23
  • Hi Ive already updated my question ..Thanks.. Im really having trouble solving this – user6537067 Dec 23 '16 at 02:11