Gmail Automation: 5 Useful Google Scripts to Automate Your Gmail

Gmail, by itself, is already a very powerful email client. With the help of filter, you can even set up automation to better organize your inbox. However, for power user, the filter is not sufficient. Here are 5 Google scripts that you can use to further automate your Gmail.

1. Auto delete emails after X number of days

Very often, after we read the email, we will just keep it in our inbox, regardless whether it is useful or not. While Google gives you tons of space to store your emails, you might still want to clean up your inbox and get rid of those useless emails. The following script can check emails with the “Delete Me” label and delete them after “x” number of days.

1. Go to Google Scripts and create a blank project (make sure you are logged into your Google account).

google-script-blank-project

Paste the following script and save it.

You can change the number of days (under delayDays) to pass before it deletes that email from your inbox. Set a trigger (Resources -> Current Project's Triggers -> Add one now) to run it daily.

google-script-set-trigger

Once activated, it will create a label "Delete Me" in your Gmail account. All you have to do is to tag the unwanted emails with this label and they will be deleted after the expiry day (as set in delayDays.

2. Snooze your emails

Sometime, after reading an email, you want it to return to your inbox after a few days. With the following Google script, you can do so:

1. Create a new Google script with the following code:

Next, save it and run the "Setup" function. This will add several new label to your Gmails (such as "Snooze for 2 days", "Snooze for 7 days" etc.) Lastly, just add a trigger for "moveSnoozes" to run everyday. Now, emails marked with the "Snooze" label will return to the inbox with unread status after the number of days have passed. (via Gmail blog)

3. Send SMS for important emails

This Google script make use of the Google Calendar's SMS feature to send you SMS for important emails.

1. Create a new Google script with the following code:

2. Save it and set a trigger for it to run every 5 minutes.

3. Lastly, you have to set a filter to add the "Send Text" label to all important incoming emails. The script will scan your inbox every 5 minutes and when it detects an email with the "Send Text" label, it will create an immediate event in Google Calender which will then trigger the SMS.

4. Schedule email to send at a later date

Boomerang is one web service that you can use to schedule emails to send at a later date, but that requires you to install a browser extension. Gmail Delay Send is a Google Script that can do the same task.

1. Go to this link and click the "Install" link. Once you have authorized the script to access your Gmail, it will redirect you to another page where you can configure the script.

google-script-gmail-delay-send-options

2. Once configured, you can then proceed to draft an email and include the future date/time for it to send and save it as draft with the "GmailDelaySend/ToSend" label.

5. Save Gmail message as PDF in Google Drive

If you have an email that you want to archive in Google Drive, you can use Google script to save it as PDF in your Google Drive account. The following script will save all the messages in an email thread as one PDF file in your Google Drive. If it comes with attachments, it will create a folder and store the messages and attachments within.

1. Create a new Google script with the following code:

2. Save it and set a trigger for it to run at regular interval. Whenver you want to save an email and its attachments to Google Drive, simply tag it with the "Save to PDF" label.

Conclusion

With Google Script, there are tons of things that you can do to your Gmail, Google Docs, Calendar and various Google Apps. If you have any other Google script that you use to make your life better, feel free to share them with us in the comment.

Image credit: gmail cookies

17 comments

  1. Wow, thanks for the insight.

  2. For “5. Save Gmail message as PDF in Google Drive”: I can run the script and the folder and files (attachments) are created, but I get an error in Google Drive when I try to open any of them. Only the email (converted to PDF) itself opens OK.
    Anyone else having this problem or know what I am doing wrong?

    1. I have made some changes to the script. The attachment should open properly now.

      1. Works great. Thanks! Appreciate your attention to this.

  3. THis is so cool. Just used the Save Email as PDFs. Any other Scripts we cud learn. AWesome. THis rocks.

  4. Hey Damien I got this error Error: invalid_client when continue to Authorization required, plz help me to fix it.

    1. At which stage did you get this error?

  5. How do you get the auto delete function to work for sub labels? I.e [Gmail]/Ebay

    1. The script looks for a “Delete Me” label. If you want it to work for other labels, simply change the “Delete Me” text to your preferred label in the script.

  6. Is possible to have a gmail script to open a particular email ?

  7. did gmail applies timestamp when an email is tagged with a label?
    is there a way to save email to a spreadsheet as it is tagged with a label? or when a label is removed?

  8. Re: 5. Save Gmail message as PDF in Google Drive

    //A. In this section I added ‘EM ‘ to identify that the source was from E-Mail

    var bodydochtml = DocsList.createFile(‘EM ‘ + subject + ‘.html’, body, “text/html”)
    var bodyId=bodydochtml.getId();

    //B. I value the HTML file so I included it into the folder when there is a document.

    {DocsList.createFolder(from);
    var folder = DocsList.getFolder(from);
    for (var j = 0; j < attachments.length; j++)
    {folder.createFile(attachments[j]);Utilities.sleep(1000);}
    {folder.createFile(bodydocpdf);}
    // Added
    folder.createFile(bodydochtml);}

    else

    //C. Here I modified to place into a folder all material even though no attachment

    {DocsList.createFolder(from);
    var folder = DocsList.getFolder(from);
    {folder.createFile(bodydocpdf);Utilities.sleep(1000);}
    folder.createFile(bodydochtml);}

    //D. I added this next line, to prevent threads from being hidden without a label.
    It simply moves the thread to E-Mail Trash where it can be deleted without
    searching. Or placed back into the Original Label for other uses.
    //Added
    {threads[i].moveToTrash();}

    DocsList.getFileById(bodyId).setTrashed(true);
    label.removeFromThread(threads[i]);
    }
    }
    }

  9. I Want to fetch some data from crbug, that i am doing using importxml function.
    however it extract the data using public access account the data gets varied.

    before fetching the data from crbug i want to login to crbug using my crendentials.
    please let me know how to do this.

  10. Awesome, I tried a couple of things and it worked well!

    Thanks for Sharing

  11. Is there any easy way to change this so that all the messages in a label are concatenated to the same PDF? That would make it an easy way to archive sets of messages

  12. Excellent scripts, thank you much. When saving an email as a PDF, are the images in the body supposed to be saved as well? The formatting and links are good, I just get the blank outlined spaces where the images are supposed to be. Thanks for any help!

  13. No.5 nice script. is there a way to make it save the PDF’s into a spacific folder even if it has no attatchments?

Comments are closed.