Using the API, you can perform many of the same operations available through the Gmail user interface, e.g. reading, composing, and sending messages. It also lets you manage labels on threads and messages, and search for and retrieve specific messages and threads.
The API is REST-based and can be used to access Gmail mailboxes and send mail. It specifically provides control over:
Google suggests the following use cases for the API:
When I built my own email management application, Simplify Email (read more about it at Tuts+ in Building Advanced Email Features With IMAP and PHP), the only Google email API was restricted to paid subscribers of Google Apps. So, I built everything on IMAP. While this allowed for broad compatibility with other email providers, it created some limitations around performance and security.
The API provides access to Gmail accounts through OAuth. This allows for secure, revokable access to Gmail accounts that doesn't require users to share their email passwords with application providers.
With the Gmail API, you can send and retrieve messages, apply labels, find messages with labels, and manage email within threads.
The API's account history provides a log of message activity for applications to reference, reducing the complexity of monitoring changes to IMAP accounts in real time.
Overall, the Google Gmail API is a great step forward.
I've been interested in extending Gmail ever since I wrote Twelve Gmail Ideas to Revolutionize Email (Again) in 2010. Most of the ideas I wished for, such as white listing and do not disturb, remained out of reach without Google's efforts or an API. For as important an application as Gmail is, innovation on these kinds of advanced features was quite slow.
We’re drowning in email, and managing our inboxes remains a heavy burden. Mail services and clients have done very little to help us with this. Most of the email we receive is sent by machines, not people, and yet we’re the ones who have to individually process all of it.
Analysis of my own email showed I was receiving email from more than 230 automated senders, far fewer actual people. I was tired of constructing filters in Gmail and filling in a myriad of unsubscribe forms. I wanted to have more control over managing my email and simplifying my life.
Using IMAP, I was able to build a number of the email management features I wanted. The result was Simplify Email:
And now, with the Gmail API, you can build secure, optimally performing solutions like these for Gmail specifically.
Also, in October 2014, Google announced Inbox, but it remains invitation only and I've been waiting for three months.
It's worth noting that FastMail (learn more about them here) is leading development of JMAP, a competitive offering to the proprietary Gmail API, and one which other IMAP providers could theoretically add support for.
If you're interested in learning more about JMAP, post a comment below; I may write about it at a later date.
Let's walk through the basic features of the Gmail API.
The Gmail API uses OAuth 2.0 to handle authentication and authorization. There are three authentication scopes which can be used individually or in combination.
This gives users a variety of controls of how apps interact with their account.
The Gmail API provides five primary resource types:
Messages and labels are the basic units of a mailbox. Drafts, history, and threads all contain one or more messages with additional metadata.
Drafts are unsent messages. They can't be modified—only created and deleted. They can also be sent.
Labels are what we use as folders in Gmail. They help with categorizing and organizing messages and threads. There are both system labels such as INBOX, TRASH and SPAM and user generated labels such as TRAVEL.
Threads are collections of messages that represent a conversation. Labels can be added to threads, and messages can be added to threads.
History is a collection of recently modified messages in chronological order. History can be used to synchronize an application or to retrieve actions taken by the user on his or her account.
You can automate sending messages from the user's account by uploading simple and multi-part messages and sending them.
Given the ID of an email from a search or history request, you can download its contents using get message.
You can retrieve the history of account actions over the past 30 days with list history. You can use this to partially synchronize a mailbox account to your application and even to learn about the user's preferences for filtering messages of a certain kind.
You can back up an entire account from beginning to end. The API allows you to page through messages in your account with the message list method.
In the next tutorial, I'll begin to walk you through using the Gmail API with your PHP‑based application.