Last active
September 22, 2019 09:31
-
-
Save chriskyfung/196f56bbee4d7ac93b55b4ac6ab71659 to your computer and use it in GitHub Desktop.
Google App Script for Copying Attachments from a Gmail Message to Your Google Drive
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
/************************************************************************************** | |
* Gmail to Google Drive Template v1.0.0 | |
* Short Desc.: | |
* - Google App Script for Copying Attachments from a Gmail Message to Your Google Drive | |
* Description: | |
* - Search first email thread that matches the subject and has attachement(s), then make a copy of all the attachments to a specific Google Drive folder. | |
* Services: | |
* - Gmail | |
* - Drive | |
* Author: Chris KY Fung | |
* Created: 2019-06-19 | |
* Website: https://gist.github.com/chriskyfung/196f56bbee4d7ac93b55b4ac6ab71659 | |
***************************************************************************************/ | |
function gmail2gdrive() { | |
// Please define a concise search query for searching your target email | |
var search_query = 'from:example.com subject:"keywords" has:attachment' | |
// Please your target attachment filename and destination folder ID | |
var filename = ''; | |
var foldId = ''; | |
// Find the first email thread that matches with the specified search query | |
var thread = GmailApp.search( search_query , 0, 1)[0]; | |
var message = thread.getMessages()[0]; | |
var subject = message.getSubject(); | |
var attachments = message.getAttachments(); | |
// Make a copy of all email attachments to the specific Google Drive Folder | |
if (foldId) { | |
var dest = DriveApp.getFolderById(foldId); | |
} else { | |
var dest = DriveApp; | |
} | |
var i; | |
for ( i = 0; i < attachments.length; i++) { | |
dest.createFile(attachments[i].copyBlob()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment