Reader small image

You're reading from  Google Apps Script for Beginners

Product typeBook
Published inFeb 2014
Reading LevelIntermediate
Publisher
ISBN-139781783552177
Edition1st Edition
Languages
Right arrow
Author (1)
Serge Gabet
Serge Gabet
author image
Serge Gabet

Serge Gabet has been a professional audio equipment manufacturer for 20 years and is now working for an artistic upper school in Brussels, Belgium as a teacher and Technical Manager. He is also in charge of the Google Apps administration of this school. He develops custom applications using Google Apps Script mainly for his school, though he also works in other areas. He has been designated a Top Contributor by Google since June 2011. He was active on the Google Group Help forum till 2012, then on the Stack Overflow forum (the Google Help Group forum was closed in June 2012), and became a first ranker and an all-time contributor on the Stack Overflow forum a few months back.
Read more about Serge Gabet

Right arrow

Chapter 3. Managing an E-mail Account

One of the reasons I began using Google Drive and Google Apps Script is that I had a Gmail account for my personal mail service, and the school I work for decided to get Google Apps for educational accounts in 2009.

This choice was probably made because not only did it seem complete and efficient, but also because it was free.

If you are interested in Google Apps Script, you are most probably a Gmail user too, so this chapter might interest you a lot.

The Gmail Browser UI has a lot of built-in tools that work pretty well; you can search, sort, archive, and delete about everything you want in a couple of clicks; you can create filters, labels, and automatically forward mails very easily too; and to be honest with you, you won't be able to do a lot more with Google Apps Script!

Bad news?

Not sure. However, there are two things that you will be able to control: how and when to use the built-in tools offered by the Google Apps Script.

Let's take a simple example...

Retrieving Gmail data


After a couple of years, a normal user's Gmail account is often a compilation of thousands of threads and a lot more messages of all kinds. Some have labels, some remain unread sometimes; I even know people who never delete or archive anything and it can easily become a real mess.

Let us assume you are not one of these people and that you already have a couple of labels to sort your messages in a clever way.

The Gmail app is a service that provides methods to retrieve messages in many ways. We can get all threads without any criteria, threads with a specific label, starred threads, and a few other threads but in this first example, we will focus on our inbox threads (https://developers.google.com/apps-script/reference/gmail/gmail-app#getInboxThreads()).

If you look at the documentation, you'll find two occurrences of the getInboxThreads() method; one has no parameter and the other has two parameters—start and max—to retrieve partial data so that we can process them in...

Creating labels


Labels in Gmail can be considered as an equivalent to folders; they allow us to sort messages in as many categories as we want, to make them easier to find when we need to.

There is already, by default, a spam filter in your Gmail account and a newly introduced feature that shows promotions, social networks, and forum threads separately, but we might want to be more selective and have separate labels for all our mail activities.

As far as I am concerned, I like that every thread has a label. My computer (and its related online data) is the only thing that I keep tidy; too bad that's not the the case with my desktop.

So, I created labels for every possible type of e-mails: Facebook, Stack Overflow forum, computer spare parts distributors, audio equipment ads, and so on.

I might have done that using Gmail filters of course, or using my favorite mail-client application, but I did it with Google Apps Script; go figure out why.

Creating a label is very straightforward, as shown in...

Counting, sorting, archiving, and deleting e-mails and threads


We have seen in the earlier examples that it is quite easy to manipulate threads and messages with regard to labels.

Once we get a thread object, we can do what we want with it. A quick look at the documentation shows the many methods available but as I have already mentioned, these operations might take some time and we may easily face issues with the maximum execution time limit of five minutes.

Execution time versus time limit

The solution to this is to proceed with relatively small bunches of threads. A careful approach is to treat 100 threads at a time and continue as long as we need or until it is finished.

Google Apps Script has all the tools we need:

  • A programmable time trigger to automate the process

  • A convenient place to store the number of processed items

  • A way to delete the trigger when it is finished

The following code is an example that will examine all your e-mails and add labels inside every thread according to the thread...

Creating e-mail messages


There are two Google services available to send e-mails: Gmail app and MailApp. The latter has only methods to send e-mails, while we have seen that Gmail app can do much more! When simply sending a message, we can use either of them interchangeably; I used to type MailApp more frequently but it is nothing more than a personal habit.

The basic process is really simple as described in the documentation (example taken from Google documentation page):

// Send an email with a file from Google Drive attached as a PDF.
 var file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');
 GmailApp.sendEmail('mike@example.com', 'Attachment example', 'Please see the attached file.', {
     attachments: [file.getAs(MimeType.PDF)],
     name: 'Automatic Emailer Script'
 });

In the optional parameters, you can use some interesting values as follows:

Automating a complete workflow


Now that we have discovered the most important methods available (not all of them, that's what the documentation is for), we can try and get our hands on the workflow that we suggested in the introductory example.

Just a reminder of that example is as follows:

You want to set up a filter that sets a label on every message coming from your boss, but you also want to keep an archive of it in a document along with a link to the attachments stored in your drive to be able to quickly search any reference he might have sent you. You would prefer that his e-mail messages remain in your main mailbox the whole day to be sure that you don't forget to read them.

I'm warning you, this code is a bit long.

When I imagined the example workflow, I thought it could be quite simple but some of the features need a lot of code lines, particularly the part that shows the attachment data in a table. I also wanted it to be simple to set up, so it creates the folders, the labels, the...

Summary


In this chapter, we saw that the Gmail service offers an impressive range of methods that allow for almost any possible manipulation of your mailbox content.

Anything that you can do manually but would take a long time to achieve can be done using script, and as this boss' e-mail example shows you, you can even imagine other scenarios without difficulty. Let Google Apps Script do your work for you.

This latter example also gave us an opportunity to use the document service and this will be the main point of interest in the next chapter.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Google Apps Script for Beginners
Published in: Feb 2014Publisher: ISBN-13: 9781783552177
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Serge Gabet

Serge Gabet has been a professional audio equipment manufacturer for 20 years and is now working for an artistic upper school in Brussels, Belgium as a teacher and Technical Manager. He is also in charge of the Google Apps administration of this school. He develops custom applications using Google Apps Script mainly for his school, though he also works in other areas. He has been designated a Top Contributor by Google since June 2011. He was active on the Google Group Help forum till 2012, then on the Stack Overflow forum (the Google Help Group forum was closed in June 2012), and became a first ranker and an all-time contributor on the Stack Overflow forum a few months back.
Read more about Serge Gabet