Search code examples
emailgoogle-apps-scriptgoogle-drive-apiemail-attachments

Sending individual email for each attachment in a Google Drive folder


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.


Solution

  • push - adds file to stack.

    pop - retrieve and remove file from stack.

    With push, you simply add another file to your current list of files.

    attachments.push(file.getAs(MimeType.JPEG));*

    What you need to do is pop files from the list to remove them. So instead of grabbing the whole list like:
    var attachment = (attachments)

    You should instead do:

    var attachment = attachments.pop()

    Currently your while {...} statement begins with adding another file to the list, then sending the whole list of files without removing the already sent file.

    So your code should look like this:

    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.pop())
      MailApp.sendEmail({
          to: emailAddress,
          subject: subject,
          htmlBody: message,
          attachments: attachments
        })
      }
    }
    

    Personally, I would remove the the list since you do not need to store each file, you just want to send them in separate mails.

    function sendEmails() {
      var files = DriveApp.getFolderById("FolderID").getFilesByType(MimeType.JPEG);
      while (files.hasNext()) {
        var file = files.next();
        // gets curent file only
        var attachments = (file.getAs(MimeType.JPEG));
        Logger.log(attachments)
      var emailAddress = "testest@test.com";
      var message = "image attached";
      var subject = "test";
      MailApp.sendEmail({
          to: emailAddress,
          subject: subject,
          htmlBody: message,
          attachments: attachments
        })
      }
    }