Join us
@sofiatarhonska ă» Sep 29,2022 ă» 8 min read ă» 2126 views ă» Originally posted on mailtrap.io
Gmail is one of the most popular email services so far, and you will very probably want to use it as a mailbox for your web or mobile app. It is safe and credible, which is crucial to prevent your emails from going into the spam folder. Thatâs why we decided to flesh out how to send emails with Gmail API.
The API provides you with a RESTful access to the features you usually have with Gmail:
Developers love Gmail API because itâs easy to implement. Weâll talk about that a bit later. Also, you can use this option for versatile cases like:
With Gmail API, you can deal with several resource types and manage them using the following methods:
Resource type | Method |
---|---|
Draft an unsent message that you can modify once created |
- create (creating a new draft) - delete (removing the specified draft) - get (obtaining the specified draft)list (listing drafts in the mailbox) - send (sending the specified draft according to the To, Cc, and Bcc headers) - update (updating the specified draftâs content) |
Message an immutable resource that you cannot modify |
- batchDelete (removing messages by message ID) - batchModify (modifying labels on the specified messages) - delete (removing the specified message) - get (obtaining the specified message) - import (importing the message into the mailbox (similar to receiving via SMTP)) - insert (inserting the message into the mailbox (similar to IMAP) - list (listing messages in the mailbox) - modify (modifying labels on the specified message) - send (sending the specified message according to the To, Cc, and Bcc headers) - trash (transferring the specified message to the trash) - untrash (transferring the specified message from the trash) |
Thread a collection of messages within a single conversation |
- delete (removing the specified thread)get (obtaining the specified thread) - list (listing threads in the mailbox)modify (modifying labels in the thread) - trash (transferring the specified thread to the trash) - untrash (transferring the specified thread from the trash) |
Label a resource to organize messages and threads (for example, inbox, spam, trash, etc.) |
- create (creating a new label) - delete (removing the specified label) - get (obtaining the specified label) - list (listing labels in the mailbox) - patch (patching the specified label) â this method supports patch semantics - update (updating the specified label). |
History a collection of changes made to the mailbox |
- list (listing the history of all changes to the mailbox) |
Settings setting up Gmail features |
- getAutoForwarding (auto-forwarding setting) - updateAutoForwarding (updating the auto-forwarding setting) - getImap (IMAP settings) - updateImap (updating IMAP settings) - getLanguage (language settings) - updateLanguage (updating language settings) - getPop (POP3 settings) - updatePop (updating POP3 settings) - getVacation (vacation responder settings) - updateVacation (updating vacation responder settings) |
If you want to have access to your Gmail from your mobile or web app, you should start with Google Developers Console. Those who visit this page for the first time ever will have to agree with the Terms of Service and pick their Country of residence. Then click Select a project and create a new one.Â
Name your new project and press Create at the bottom.Â
Once thatâs done, you can press the Library tab on the left and find yourself in the API Library page. Enter âGmail APIâ in the search bar and click on it once found. Now, you need to enable the API for your project.Â
Note that youâll have to enable it separately for each new project you work on.Â
Once the API is enabled, youâll be taken to a nice dashboard that says, âTo use this API, you may need credentialsâ. If you click Create credentials, youâll have to pass through a set of questions to find out what kind of credentials you need. We advise you to go another way since we already know what it is: OAuth client ID. So, click the Credential tab on the left, and then pick OAuth client ID from the drop-down list of the Create Credentials button.Â
Youâll see the Configure consent screen button. It will bring you to a page with many fields. You can just enter the name of your app and specify authorized domains. Fill in other fields if you want.Â
Click save and then pick the type of your app (web app, Android, Chrome App, iOS, or other). After that, name your OAuth Client ID. Also, enter JavaScript origins and redirect domains for use with requests from a browser or a web server respectively. Click create to finalize. Thatâs it. Download a JSON file with your credentials â youâll need it later.
The next step is to select a quickstart guide according to the technology your app is built with. So far, there are the following options:
For mobile apps, there are G Suite APIs for iOS and Android as well.Â
What you need first in this quickstart guide is the Prerequisites section. Letâs say your choice is PHP. In this case, make sure your PHP version corresponds to the given one. Also, install the JSON extension and the Composer dependency management tool if you havenât already. After that, you can install the Google Client Library. For Java, youâll need to create a new project structure and the src/main/resources/
directory. Then, copy the JSON file with credentials to this directory and replace the content of the build.gradle
file with this code. So, pay attention when preparing your project.Â
Google provides client libraries to work with the API:
In this step, we need to authorize access to your Gmail account from the app, and then youâll be able to manage emails. For this, you need to create a file in your working directory. Below youâll find the specific file names for each technology. Copy-paste a corresponding code sample from the chosen Quickstart Guide and run it. Here are the links to the code samples:
go run quickstart.go
gradle run
ruby quickstart.rb
node .
php quickstart.php
python quickstart.py
python -m SimpleHTTPServer 8000
â for Python 2+python -m http.server 8000
â for Python 3+It workedâŠor not. Google will warn you about a probable failure of the sample you run to open a new window in your default browser. If this happens, youâll need to do it manually. Copy the URL from the console and paste it in the browser. It will look like this:
Next, youâll be asked to either log into your Google account or select one account for authorization. Press allow and youâll see all your inbox labels in the SSH shell like this:
Congrats! Gmail API works and you can send your first email.
To send a message, first you need to create one. For this, your app can use the drafts.create method which includes:
Letâs see how this is done in practice with Python:
and PHP
Once you have created your message, you can either call messages.send or drafts.send to send it. Here is how it may look:
Python
and PHP
You can also create and send a multi-part MIME message. For example, this is how it looks with Python:
It would be weird if you canât use the API to read messages from Gmail. Luckily you can by using the get method by the message ID. Here is how it may look in a Python app:
If the message contains an attachment, expand your code with the following:
Simple Mail Transfer Protocol (SMTP) is a set of rules for sending emails either from the sender to the email server or between servers. Most email service providers use SMTP to send and POP3/IMAP4 to receive emails. To learn more about these protocols, you can read our IMAP vs. POP3 vs. SMTP blog post. Google also provides the Gmail SMTP server as a free SMTP service. Application Programming Interface (API) is an interaction channel used by apps, platforms, and codes to reach each other. With Gmail API, you can send emails using only HyperText Transfer Protocol (HTTP), a set of rules that defines how messages are formatted and transmitted.
You can call the API from the app to communicate with an email service that is used to send emails from another server.Â
For SMTP, a client establishes a TCP connection to the SMTP server and transfers an email. After authorization, the server sends the email to the recipientâs SMTP server, which, in turn, forwards it to the IMAP4 or POP3 server. Client and server communicate with each other using SMTP commands and responses.
Gmail API uses open authentication (Oauth2), which only lets you request the scope of access you need. SMTP provides full access to the account using client login and password SMTP authentication.
The usage limit of Gmail API is one billion quota units per day. Each method requires a particular number of quota units. For example, a drafts.create
is 10 units and a messages.send
is 100 units. Gmail API enforces standard daily mail sending limits. Also, keep in mind that the maximum email size in Gmail is 25MB.
Each option has its own pros and cons. SMTP is a widely adopted and easy-to-set-up solution to send emails. Moreover, you donât need any coding skills to handle stuff. Also, you can benefit from using a fake SMTP server such as Mailtrap as a playground for safe email testing.
Besides, it is a great option to automate processes and provide a wide range of functionality for the app. Also, API can boast an extra level of security, which is crucial if you deal with sending sensitive data in emails.
If you want to use email API with easy onboarding process, comprehensive stats and reliable support, try Mailtrap Email API.
I hope you enjoyed reading our guide on how to send emails with Gmail API using GoLang, PHP, Python, JavaScript and more that was originally published on Mailtrap blog.
Join other developers and claim your FAUN account now!
Influence
Total Hits
Posts
Only registered users can post comments. Please, login or signup.