Hey there! As a fellow productivity geek, I know you‘re always looking for ways to get more out of Gmail and save time in your day. Well, I‘ve got some great news for you – Google Apps Script is like a cheat code for leveling up your Gmail game.
Apps Script allows you to automate tasks, customize workflows, and integrate data from multiple sources. Gmail power users love Apps Script for the sheer amount of automation and productivity it unlocks.
I‘ve been using Apps Script for years to optimize my own Gmail experience. In this guide, I‘ll share 6 of my favorite scripts that have supercharged my productivity.
These are some of the most useful automations I‘ve come across for common Gmail tasks. I‘ll explain how each one works with detailed code samples you can tweak for your needs.
Let‘s dive in!
A Quick Intro to Google Apps Script
First, a fast primer if you‘re new to Apps Script:
It‘s a JavaScript-based scripting language for automating tasks across G Suite products like Gmail, Drive, Sheets, Docs, and more.
You can access Apps Script code editor directly within Gmail – go to Tools > Script editor. This allows the script to easily read and modify your Gmail data.
The key steps are:
- Open script editor from within Gmail
- Write your code
- Run the script with the ▶ button
- Authorize permissions when prompted
- Set triggers to execute on a schedule
And that‘s it! Your script will now run automatically on the specified time to handle tasks for you in Gmail.
1. Send Recurring Emails Like Clockwork
Let me start with a script to send recurring emails, meeting invites, reminders, newsletters etc. automatically.
Gmail allows you to schedule individual emails to be sent later. But Apps Script opens up more powerful options like:
- Send emails at specified intervals – daily, weekly, monthly etc.
- Fully customize message content dynamically
- Integrate data from Spreadsheets or Calendar to create dynamic templates
- Attach files from Drive to send as attachments
Here‘s a basic script to send a recurring email:
function sendRecurringEmail() {
let recipient = "[email protected]";
let subject = "Meeting tomorrow at 10 AM";
let message = "Hi John, Just a quick reminder that we have a meeting tomorrow at 10 AM. Please let me know if you need to reschedule.";
MailApp.sendEmail(recipient, subject, message);
}
To use it:
- Update the recipient, subject, and message as needed
- Set up a time-driven trigger to run daily, weekly etc.
The script will now send this email on the configured schedule automatically!
Here are a few ways you can extend this further:
- Look up event data from Calendar to dynamically generate the email content for upcoming meetings, calls etc.
- Mail merge emails from a spreadsheet containing names, email ids, and custom fields
- Attach proposal documents from Drive as attachments
- Integrate email analytics to track open rates, clicks etc.
The possibilities are endless! Running this for my daily standup reminders and weekly team meetings has been a game changer.
2. Label Incoming Emails With Attachments
I don‘t know about you, but I receive a ton of important documents and files via email. Being able to quickly scan for and find emails with attachments is so important.
This script looks at recent emails in your inbox, and if any attachments are found, it labels the thread automatically so they stand out:
function labelEmailsWithAttachments() {
// Get label, create if doesn‘t exist
let label = GmailApp.getUserLabelByName("Has Attachment");
if (!label) {
label = GmailApp.createLabel("Has Attachment");
}
// Get 100 most recent threads
const threads = GmailApp.getInboxThreads(0, 100);
threads.forEach(thread => {
// Check each message for attachments
const messages = thread.getMessages();
messages.forEach(message => {
if (message.getAttachments().length > 0) {
// Found attachment, apply label
label.addToThread(thread);
}
});
});
}
I have it set to check emails from the past 24 hours for attachments. You can adjust the number of threads or use filters like from:, subject: etc.
Adding the "Has Attachment" label makes these critical emails much easier to scan and find later. I love setting this up for clients who email me documents regularly.
You can even extend it further to:
- Save attachments directly to Drive
- Star messages with attachments for later follow up
- Set color rules like red label for unsigned documents
So many possibilities to automate your attachment management workflows!
3. Maintain a Tidy Inbox By Deleting Old Emails
I‘m sure your inbox gets overloaded with old threads and conversations over time. This script provides some nice maintenance by cleaning out old emails automatically.
Here‘s how it works:
function cleanInbox() {
// Delete threads older than 6 months
const cutoff = 6 * 30; // in days
const threads = GmailApp.search(‘in:inbox older_than:‘ + cutoff + ‘d‘);
threads.forEach(thread => {
thread.moveToTrash();
});
}
It first searches for all emails older than 6 months, then proceeds to delete them by moving the threads to trash.
The older_than: search operator makes it really straightforward to target emails based on age.
You can tweak the cutoff period as needed – shorten to 1 month if you want more aggressive deletion, or increase to 1 year to preserve emails longer.
Pro tip: Use labels like "Archive" to tag emails you want to keep around indefinitely before running this script.
Set it up to run weekly or monthly and your inbox will stay nice and lean automatically!
4. Never Lose Contact Details Again
Having scattered email addresses in inbox threads makes it really hard to find and connect with people later.
I love this script that neatly saves any new email senders/recipients to your Google Contacts:
function saveNewSendersToContacts() {
// Match emails from last 10 days
const threads = GmailApp.search(‘after:10d‘);
threads.forEach(thread => {
// Extract all unique senders from thread
const senders = getUniqueSenders(thread);
senders.forEach(sender => {
// Check Contacts for existing based on name
const existing = ContactsApp.getContactsByName(sender);
if (existing.length == 0) {
// Create contact if new
ContactsApp.createContact(sender, sender);
}
});
});
}
// Helper to get unique senders from thread
function getUniqueSenders(thread) {
// Logic to extract unique senders
}
It goes through your recent emails, extracts the unique senders, checks for existing contacts, and creates new ones if needed.
Having all senders saved as contacts has so many benefits:
- Easily find contact details needed for followup
- See full history of emails from a contact
- Auto-complete names when drafting new emails
- Integrate with your CRM, newsletter etc.
I have mine run every two weeks and it‘s amazing how many new contacts get added from inbound emails. Give it a spin and watch your contacts list expand!
5. Archive Important Attachments to Drive
Here‘s a super handy one – this script saves email attachments directly to a folder in Drive.
function saveAttachmentsToDrive() {
const folder = getOrCreateFolder(‘Email Attachments‘);
// Loop through inbox threads
const threads = GmailApp.getInboxThreads();
threads.forEach(thread => {
// Get all message attachments
const messages = thread.getMessages();
messages.forEach(message => {
const attachments = message.getAttachments();
attachments.forEach(attachment => {
// Extract file data
const fileData = attachment.getBytes();
// Save attachment to folder
folder.createFile(attachment.getName(), fileData);
});
});
});
}
It loops through all threads > messages > attachments – extracting the file data and saving it to Drive with the original filename.
The key benefits are:
- Attachments are saved securely in Drive
- Organized centrally in one place for easy access
- Can search, share, preview etc.
- Backed up so you never lose important attachments
I have it run every couple hours to capture attachments from newly arrived emails as well. Give it a try – it‘s a total game changer!
6. Get Your Day Off To a Motivated Start
Finally, I thought I‘d share a fun one I built that sends a daily productivity email to kickstart my mornings.
It includes:
- To-do list – Fetches incomplete tasks from Google Tasks
- Calendar – Gets my schedule for the day
- Inspirational quote – Fetches quote of the day from an API
- News headlines – Top news stories to stay updated
Bringing these elements together into a single email keeps me organized and motivated to crush the day ahead.
Here‘s a look at the script powering it:
function sendDailyDigest() {
const emailContent = `
<body>
${getTasks()}
${getCalendar()}
${getQuote()}
${getNews()}
</body>
`;
MailApp.sendEmail(Session.getActiveUser().getEmail(),
"Your Daily Digest", "", {htmlBody: emailContent});
}
// Helper functions to generate each section
function getTasks() {
// Fetch incomplete tasks
}
function getCalendar() {
// Get today‘s calendar events
}
function getQuote() {
// Call Quotes API
}
function getNews() {
// Call News API
}
The main script generates the email with each section provided by helper functions calling the respective APIs.
Pro tip: Use conditional formatting in Google Sheets to make tasks turn red as the deadline approaches for added urgency in the email!
I love starting my day with this automated email to set the tone. Give it a try and experiment with sections that would kickstart your own mornings!
Level Up Your Gmail with Apps Script
There we go! As you can see, Apps Script unlocks some incredibly useful automations for Gmail users like you and me.
The key benefits for your productivity:
- Save hours through automation and eliminating repeat tasks
- Customize workflows that streamline your processes
- Centralize information from multiple sources
- Get more organized with rules and filters
- Unlock analytics with event tracking
The limit is your imagination – Apps Script gives you the building blocks to create any workflow specific to your needs.
I hope these 6 scripts spark some ideas you can implement yourself. Start small by picking one annoyance or bottleneck to optimize. Over time you‘ll build an extensive toolbox of scripts fine-tuned to run your Gmail like a well-oiled machine.
Let me know if you have any other genius Apps Script automations for Gmail! I‘m always looking for new ideas to further optimize my own workflow.
Talk soon,
[Your Name]