Skip to content Skip to sidebar Skip to footer

Save Gmail Attachments On Google Drive

I would like to save gmail attachments(.pdf files) from a specific email in a specific Google Drive folder. I also need to rename file with a string composed by some string of the

Solution 1:

Stefano, I had the same issue, if this is the same code as adapted from https://bit.ly/-extractgmail

I removed the line "for (var i in fileTypesToExtract) {" which was causing duplicates for me. It was running the query for each of the file types.

Solution 2:

// `has:pdf` searches for messages with PDF attachmentsvar query = 'has:pdf in:inbox from:noreply@agyo.io has:nouserlabels '; 

var results = Gmail.Users.Messages.list(userId, {q: query});
results.messages.forEach(function (m) {
    var msg = GmailApp.getMessageById(m.id);
    msg.getAttachments().forEach(function (a) {
        var fileName = a.getName();
        fileName = saveAttachmentToFolder(folder, a, fileName, msg.getDate(), input.tz);
    });
});
functionsaveAttachmentToFolder(folder, attachment, fileName, date, timezone) {
    if (timezone) {
        fileName = standardizeName(attachment, date, timezone);
    }
    Logger.log(fileName);
    folder.createFile(attachment.copyBlob()).setName(fileName);
}

The code snippet above is based on a Gmail add-on that I created, specifically for saving attachments to labeled folders in Drive: https://github.com/ellaqezi/archiveByLabel/blob/main/Code.gs#L24

In the label field, you can define nested directories to create in Drive e.g. foo/bar.

In the query field, you can copy the parameters as you would use them in Gmail's search bar.

Post a Comment for "Save Gmail Attachments On Google Drive"