I'm trying to write a script that will take all the files from a specific Google Drive folder and send them as email attachments. I need each attachment to be its own individual email.
Here's what I have so far:
function sendEmails() {
var files = DriveApp.getFolderById("FolderID").getFilesByType(MimeType.JPEG);
var attachments = [];
while (files.hasNext()) {
var file = files.next();
attachments.push(file.getAs(MimeType.JPEG));
Logger.log(attachments)
var emailAddress = "testest@test.com";
var message = "image attached";
var subject = "test";
var attachment = (attachments)
MailApp.sendEmail({
to: emailAddress,
subject: subject,
htmlBody: message,
attachments: attachments
})
}
}
At the minute, the script is sending the emails but adding attachments consecutively. So, if the folder has 3 JPEGs, it'll send 3 emails, one containing 1 attachment, another containing 2, and a 3rd containing all 3 attachments.
I'm really new to all this but really enjoying messing around with it. I'm confused as to what to do after getting the files and how I can then link them as attachments to their own individual email. Do I need to give each attachment its own array?
Thanks in advance.