I am using Google Apps Script to automate saving attachments from emails to unique Google Drive folders depending on the Label the messages have been filtered into.
So far I have managed to get the message ID using some code snippet I have found on line. However I am stuck trying to access the body of the messages & save the attachment to drive folders.
function getMessage(){
var folder = DriveApp.getFolderById('xxxxxxxxxxxxx');
var userId = "myemail@gmail.com";
var query = "label:Global Alcohol";
var res = Gmail.Users.Messages.list(userId, {q: query});
var attachment = msgs.Attachments.get(userId, messageId, id);
var ids = res.messages.map(function(e){return e.id});
Logger.log(ids);// Message IDs with the specific labels.
}
Thanks in advance for your help.
Try this:
function saveAttachmentInFolder(){
var folder = DriveApp.getFolderById('xxxxxxxxxxxxx');
var userId = "myemail@gmail.com";
var query = "label:Global Alcohol";
var res = Gmail.Users.Messages.list(userId, {q: query});//I assumed that this works
res.messages.forEach(function(m){
var attA=GmailApp.getMessageById(m.id).getAttachments();
attA.forEach(function(a){
var ts=Utilities.formatDate(new Date(),Session.getScriptTimeZone(), "yyMMddHHmmss");
folder.createFile(a.copyBlob()).setName(a.getName()+ts);
});
});
}