Skip to content

Instantly share code, notes, and snippets.

@dan-diaz
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save dan-diaz/8894374 to your computer and use it in GitHub Desktop.

Select an option

Save dan-diaz/8894374 to your computer and use it in GitHub Desktop.
gmail attachment automation
var autoDrive = {};
autoDrive.settings = {
label: 'Band',
folder: 'Music'
};
//collect attachments by labelName from unread messages within the inbox
//Example: autoDrive.collectAttachments('Band', 'Music');
autoDrive.collectAttachments = function(label, folder){
var targetLabel = GmailApp.getUserLabelByName(label); //GmailLabel
var threadsInLabel = targetLabel.getThreads(); //GmailThread
var attachmentsArray = [] //hold all attachments here
//check that label has threads
if(threadsInLabel.length){
for(var i = 0; i < threadsInLabel.length; i++){
var currentThread = threadsInLabel[i];
//check that thread is in Inbox and Unread
if(currentThread.isInInbox && currentThread.isUnread){
var messageDate = currentThread.getDate();
var messages = currentThread.getMessages();
var messagesLength = messages.length;
//check each message for attachments
for(var j = 0; j < messagesLength; j++){
var currentMessage = messages[j];
var attachments = currentMessage.getAttachments();
//if there are any attachments
if(attachments.length){
var attachmentsLength = attachments.length
for(var k = 0; k < attachmentsLength; k++){
//autoDrive.saveAttachments(attachments[k], folder);
attachmentsArray.push(attachments[k]);
}
}
}
}
}
}
}
//save attachments to a specified folder in drive
autoDrive.saveAttachments = function(attachment, folder){
}
autoDrive.collectAttachments(autoDrive.settings.label, autoDrive.settings.folder);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment