electronic brain surgery since 2001

Save Gmail Attachments to Google Drive

Thanks to my Paper Backup I save all kinds of invoices and other documents I get by snail mail in my Google Drive. But of course there are also invoices that get delivered by email. Usually they come as a PDF attachment. Wouldn't it be nice to save those PDFs in Google drive as well?

Unfortunately Google Mail's filters do not allow to automate this. Luckily there's Google Apps Script.

I wrote a script to automatically save all attachments from mails within a certain Gmail label to Google Drive. Here's how you can use it, too:

  1. In Gmail create a new Label. eg. Invoices. Feel free to create sub labels for more fine grained control.
  2. Create Filters to automatically sort your invoices into those labels
    • I recommend to pick the “has attachment” option
    • Be sure to use the “Star it” option - we use stars to track which mails have been processed already
    • you probably also want to apply the filter to matching conversations
  3. Select “Make a copy” from the “File” menu
  4. Give the needed permissions
  5. Adjust the top two variables
    • GMAIL_LABEL is the label you created in Step 1
    • GDRIVE_FILE is the path and file name that will be used to save attachements (see below for the placeholders)
  6. Check “Resources - Current Project's trigger” an make sure the main method is called periodically (up to you how often).

After a while your attachments should start showing up in your Google Drive.

For configuring the location you can use the following placeholders in GDRIVE_FILE:

$name The original attachment name
$ext The file extension of the original attachment
$domain The domain part of the sender who sent the attachment
$sublabel The sub label(s) under your configured label where the message was found
$y Year the message was received at
$m Month the message was received at
$d Day the message was received at
$h Hour the message was received at
$i Minute when the message was received at
$s Second when the message was received at
$mc The message number in the thread, starting at 0
$ac The attachment number in the thread, starting at 0
appscript, gmail, gdrive, javascript
Similar posts: