Home‎ > ‎

Fetch Gmail Attachment to Google Drive using Google Apps Script

Gmail Attachment to Google Drive - Google Apps Script Tutorial


This script will look for attachments in Gmail Message and Extract it to a Google Drive folder. Script can be run manually or through a time driven trigger. The script can be personalized by following the instructions
 given in this article.

Installation notes

  1. Make a copy of this script project
  2. In the new copy of the script, go to Resources > Current Project’s triggers 
    Click on the link displayed to setup a time driven trigger and save the trigger. Frequency depends on how frequently you want to look for new messages in Gmail. Here is a screenshot of the trigger settings to run every minute.

    Gmail to Drive - Google Apps Script Tutorial

    Click on notifications and choose your notification preferences. This will notify you if there occurs execution failure. Here is an screenshot of the notification preferences

    Gmail To Drive - Google Apps Script Tutorial

  3. If you are running the script first time, it will ask you to authorize the script for “Gmail” and “Google Drive” access. Authorize it. Now the script installation is complete.
This procedure can be followed with any Gmail account. Once the installation is complete, script will run as per the specified interval, look for messages in Gmail which are not labelled and have attachments as file types specified. It will extract the attachment to a Google Drive folder and label the Gmail Message 'GmailToDrive'.

Would you like to set your preferences?

You may change line 3, 5 and 7 as per your preferences.

var fileTypesToExtract = ['jpg', 'tif', 'png', 'gif', 'bmp', 'svg'];
This is an array for file types to look for. You may add or remove file types from this array.

var folderName = 'GmailToDrive';
This is the name of the folder to which files will be extracted to in Google Drive. If the script does not finds a folder with this name, it will create the folder in drive.

var labelName = 'GmailToDrive'; This label name is Gmail Label name. This is used by the script to identify which message has been processed and which has not not. Once a message is processed, script will put a label on those messages with below name. You may change the label name too.

Source Code

//Array of file extension which you would like to extract to Drive
var fileTypesToExtract = ['jpg', 'tif', 'png', 'gif', 'bmp', 'svg'];
//Name of the folder in google drive i which files will be put
var folderName = 'GmailToDrive';
//Name of the label which will be applied after processing the mail message
var labelName = 'GmailToDrive';

function GmailToDrive(){
  //build query to search emails
  var query = '';
  //filename:jpg OR filename:tif OR filename:gif OR fileName:png OR filename:bmp OR filename:svg'; //'after:'+formattedDate+
  for(var i in fileTypesToExtract){
query += (query == '' ?('filename:'+fileTypesToExtract[i]) : (' OR filename:'+fileTypesToExtract[i]));
  query = 'in:inbox has:nouserlabels ' + query;
  var threads = GmailApp.search(query);
  var label = getGmailLabel_(labelName);
  var parentFolder;
  if(threads.length > 0){
parentFolder = getFolder_(folderName);
  for(var i in threads){
var mesgs = threads[i].getMessages();
for(var j in mesgs){
  //get attachments
  var attachments = mesgs[j].getAttachments();
  for(var k in attachments){
    var attachment = attachments[k];
    var isImageType = checkIfImage_(attachment);
    if(!isImageType) continue;
    var attachmentBlob = attachment.copyBlob();
    var file = DocsList.createFile(attachmentBlob);

//This function will get the parent folder in Google drive
function getFolder_(folderName){
  var folder;
  try {folder = DocsList.getFolder(folderName)}
  catch(e){ folder = DocsList.createFolder(folderName);}
  return folder;

//getDate n days back
// n must be integer
function getDateNDaysBack_(n){
  n = parseInt(n);
  var today = new Date();
  var dateNDaysBack = new Date(today.valueOf() - n*24*60*60*1000);
  return dateNDaysBack;

function getGmailLabel_(name){
  var label = GmailApp.getUserLabelByName(name);
  if(label == null){
label = GmailApp.createLabel(name);
  return label;

//this function will check for filextension type.
// and return boolean
function checkIfImage_(attachment){
  var fileName = attachment.getName();
  var temp = fileName.split('.');
  var fileExtension = temp[temp.length-1].toLowerCase();
  if(fileTypesToExtract.indexOf(fileExtension) != -1) return true;
  else return false;