Last active
April 13, 2016 12:00
-
-
Save akiross/15212752e60a0c1e584d to your computer and use it in GitHub Desktop.
This is a Google App Script that "downloads" many attachment at once to Google Drive. Label the mails to download with "GmailDownload" and run the script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onOpen() { | |
// Setup the drive folder | |
var date = new Date(); | |
var fold = DriveApp.createFolder("GmailDownload-" + date.toISOString()); | |
var log = ""; | |
// Get the label with gmail threads | |
var label = GmailApp.getUserLabelByName("DriveDownload"); | |
//Logger.log(label.getUnreadCount()); | |
var threads = label.getThreads();//0, 5); // for DEBUG only | |
for (var i = 0; i < threads.length; i++) { | |
log += "\nProcessing thread" + threads[i].getFirstMessageSubject() + "\n"; | |
var msgs = threads[i].getMessages(); | |
for (var j = 0; j < msgs.length; j++) { | |
// Get array of attachments | |
var atc = msgs[j].getAttachments(); | |
if (atc.length == 0) { | |
var errorMessage = "WARNING! No attachment in message " + j + " for mail " + msgs[j].getSubject() + " from " + msgs[j].getFrom(); | |
log += " " + errorMessage + "\n"; | |
Logger.log(errorMessage); | |
continue; | |
} | |
// Create a folder for each message | |
var fname = msgs[j].getFrom() + " - " + msgs[j].getSubject(); | |
var msgFold = fold.createFolder(fname); | |
// Download the files | |
for (var k = 0; k < atc.length; k++) { | |
var file = msgFold.createFile(atc[k].copyBlob()); | |
file.setName(atc[k].getName()); | |
} | |
} | |
} | |
var logFile = fold.createFile("log.txt", "GMail Bunch Downloader - Logfile\n\n" + log); | |
// Remove the label | |
label.deleteLabel(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment