Gmail to Drive: How to Save Emails as PDFs Automatically using Apps Script
Do you ever find yourself scrambling to locate important emails buried deep within your Gmail inbox? Wish there was a simple way to preserve email conversations for safekeeping or reference? Then this tutorial is for you! I'll walk you through setting up a custom Google Apps Script that automatically saves specific emails to Google Drive as easily searchable PDFs.
Prerequisites
This tutorial assumes you have a basic understanding of Gmail (like creating and applying labels) and Google Drive, as well as some familiarity with Google Apps Script. If you're new to Apps Script, consider reading our introductory guide on Getting Started with Google Apps Script. It'll help you grasp the basics needed to follow along with this tutorial.
This tutorial assumes that you know the following Apps Script concepts:
Functions: Reusable blocks of code to accomplish a task (tutorial).
Triggers: Event-driven instructions to run your scripts automatically (tutorial).
5 Steps to Automate Saving Emails as PDFs in Google Drive
Step 1 — Setting Up Your Google Drive Folder
First, navigate to Google Drive and create a new folder named SAVED_EMAIL
. This is where your emails will be saved as PDFs.
Step 2 — Labeling Emails in Gmail
In Gmail, create a new label named SaveToDrive
by following these steps: click on the gear icon > select See all settings > select Labels > Click on Create new label > Enter "SaveToDrive" > click Save.
Apply this label to any email you want to save to your Drive folder. You can do this manually or set up filters to automatically apply the label to incoming emails based on certain criteria.
Step 3 — Creating the Apps Script Project
Go to Google Apps Script and create a new project. Name your project something descriptive, like "Save Gmail to Drive".
Step 4 — Writing the Script to Save Emails as PDFs
Copy and paste the following script into the Apps Script editor:
/**
* Automates saving Gmail emails labeled "SaveToDrive" as PDFs in a designated Google Drive folder.
*
* Ensures a folder named "savedEmails" exists in Drive, creating it if necessary.
* Emails are then converted to PDF format and saved within this folder. The "SaveToDrive" label
* is removed from each processed email.
*
*/
function saveEmailsAsPDF() {
// Get emails with the label "SaveToDrive"
let threads = GmailApp.getUserLabelByName("SaveToDrive").getThreads();
// Get or create the "SAVED_EMAIL" folder in Google Drive
let folder = DriveApp.getFoldersByName("SAVED_EMAIL");
if (!folder.hasNext()) {
folder = DriveApp.createFolder("SAVED_EMAIL");
} else {
folder = folder.next();
}
// Process each email thread
for (let i = 0; i < threads.length; i++) {
let messages = threads[i].getMessages();
messages = messages.reverse();
let messageHTML = messages[0].getBody();
let subject = threads[i].getFirstMessageSubject();
let date = threads[i].getLastMessageDate();
let pdfName = subject + " - " + Utilities.formatDate(date, "GMT", "yyyy-MM-dd");
// Generate a PDF blob of the combined thread content
let blob = Utilities.newBlob(messageHTML, MimeType.HTML, pdfName).getAs(MimeType.PDF);
// Create the PDF file in the designated folder
folder.createFile(blob);
// Remove the "SaveToDrive" label after processing
threads[i].removeLabel(GmailApp.getUserLabelByName("SaveToDrive"));
}
}
This script checks your Gmail for any emails with the SaveToDrive
label, saves them as PDFs in the SAVED_EMAIL
folder on Drive, and then removes the label from the email.
Step 5 — Scheduling the Script to Run Automatically
To ensure emails are saved automatically without manual intervention, we need to schedule the script to run every 30 minutes by setting up a trigger. In the Apps Script editor, click on the clock icon (Triggers) > Add Trigger. Set the function to run as saveEmailsToDrive
, the deployment as Head
, the event source as Time-driven
, and the type as Minutes timer
every 30 minutes.
Conclusion
Congratulations! Your automated email archiving is ready. This setup not only helps in keeping your emails organized but also ensures that you have a backup of crucial information that's easily accessible. Test it out by labeling emails with SaveToDrive
and soon you'll find those PDFs in your Drive. Thanks for reading this tutorial!
Master Google Sheets Automation
I'd appreciate any feedback you can give me regarding this post.
Was it useful? Are there any errors or was something confusing? Would you like me to write a post about a related topic? Any other feedback is also welcome. Thank you so much!