Send Emails with Gmail API
Tags: go, javascript, php, python
Published: 2022-09-29 09:06:29

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](https://mailtrap.io/blog/avoid-spam-filters/). That’s why we decided to flesh out how to send emails with Gmail API.

## Gmail API – why you should consider using it

The API provides you with a RESTful access to the features you usually have with Gmail:

- Send and receive [HTML emails](https://mailtrap.io/html-email-checker/)
- Send and receive emails with attachments
- CRUD (create, read, update, and delete) operations with messages, drafts, threads, and labels 
- Access control of your Gmail inbox
- Full search capabilities of the web UI
- Perform specific queries 
- And many more…

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:

- automated email sending
- mail backup
- mail migration from other email services

### Resource types and methods

With Gmail API, you can deal with several resource types and manage them using the following methods:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0hdphccueum3zmpm7c4h.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tgv4n6un83mxnsfv5nsu.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sro85pf88eg6daspltk9.png)

## How to make your app send emails with Gmail API
### Step 1: Create a project at Google API Console
If you want to have access to your Gmail from your mobile or web app, you should start with [Google Developers Console](https://console.developers.google.com/). 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. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ze2a5hq3u9xc5xsvdw2.png)
Name your new project and press **Create** at the bottom. 

### Step 2: Enable Gmail API
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. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/di7vius6axg26nfs6tbi.png)
Note that you’ll have to enable it separately for each new project you work on. 

### Step 3: Credentials and authentication with OAuth 2.0

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. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wfykifeoze3ferttj0q6.png)
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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/roggy8xyc3ephqmnjq31.png)
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.

### Step 4: Pick a quickstart guide 

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:

- [API for Go](https://developers.google.com/gmail/api/quickstart/go)
- [API for Java](https://developers.google.com/gmail/api/quickstart/java)
- [API for Ruby](https://developers.google.com/gmail/api/quickstart/ruby)
- [API for .NET](https://developers.google.com/gmail/api/quickstart/dotnet)
- [API for Node.js](https://developers.google.com/gmail/api/quickstart/nodejs)
- [API for PHP](https://developers.google.com/gmail/api/quickstart/php)
- [API for Python](https://developers.google.com/gmail/api/quickstart/python)
- [API for browser (JavaScript)](https://developers.google.com/gmail/api/quickstart/js)

For mobile apps, there are G Suite APIs for [iOS](https://developers.google.com/gsuite/guides/ios) and [Android](https://developers.google.com/gsuite/guides/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](https://github.com/gsuitedevs/java-samples/blob/master/gmail/quickstart/build.gradle). So, pay attention when preparing your project. 

### Step 5: API client library

Google provides client libraries to work with the API:
[API client for Go](https://github.com/googleapis/google-api-go-client)

```
Installation:
go get -u google.golang.org/api/gmail/v1
go get -u golang.org/x/oauth2/google
```
[API client for Java](https://github.com/googleapis/google-api-java-client)

```
Installation via Gradle
 repositories {
       mavenCentral()
   }
   dependencies {
       compile 'com.google.api-client:google-api-client:1.30.2' 
   }
```
[API client for Ruby](https://github.com/googleapis/google-api-ruby-client)

```
Installation:
gem install google-api-client
```
[API client for .NET](https://github.com/googleapis/google-api-dotnet-client)

```
Installation via NuGet Package Manager Console:
Install-Package Google.Apis.Gmail.v1
```
[API client for Node.js](https://github.com/googleapis/google-api-nodejs-client)

```
Installation via npm:
npm install googleapis@39 --save
```
[API client for PHP](https://github.com/googleapis/google-api-php-client)

```
Installation via Composer:
composer require google/apiclient:"^2.0"
```
[API client for Python](https://github.com/googleapis/google-api-python-client)

```
Installation:
pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
or
easy_install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
```
[API client for JavaScript](https://github.com/google/google-api-javascript-client)

[API client for Objective-C](https://github.com/google/google-api-objectivec-client-for-rest/)

[API client for Dart](https://github.com/dart-lang/googleapis)

### Step 6: Access to Gmail

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

- Filename: _quickstart.go_
- Directory: _gmail/quickstart/_
- [Code sample for Go](https://github.com/gsuitedevs/go-samples/blob/master/gmail/quickstart/quickstart.go)
- Run with: `go run quickstart.go`

#### Java

- Flinename: _GmailQuickstart.java_
- Directory: _src/main/java/_
- C[ode sample for Java](https://github.com/gsuitedevs/java-samples/blob/master/gmail/quickstart/src/main/java/GmailQuickstart.java)
- Run with: `gradle run`

#### Ruby

- Filename: _quickstart.rb_
- Directory: _gmail/quickstart/_
- [Code sample for Ruby](https://github.com/gsuitedevs/ruby-samples/blob/master/gmail/quickstart/quickstart.rb)
- Run with: `ruby quickstart.rb`

#### .NET

- Filename: _GmailQuickstart.cs_
- Directory: _gmail/GmailQuickstart/_
- [Code sample for .NET](https://github.com/gsuitedevs/dotnet-samples/blob/master/gmail/GmailQuickstart/GmailQuickstart.cs)
- Run by clicking Start in the Visual Studio toolbar

#### Node.js

- Filename: _index.js_
- Directory: _gmail/quickstart/_
- [Code sample for Node.js](https://github.com/gsuitedevs/node-samples/blob/master/gmail/quickstart/index.js)
- Run with: `node .`

#### PHP

- Filename: _quickstart.php_
- Directory: _gmail/quickstart/_
- [Code sample for PHP](https://github.com/gsuitedevs/php-samples/blob/master/gmail/quickstart/quickstart.php)
- Run with: `php quickstart.php`

#### Python

- Filename: _quickstart.py_
- Directory: _gmail/quickstart/_
- [Code sample for Python](https://github.com/gsuitedevs/python-samples/blob/master/gmail/quickstart/quickstart.py)
- Run with: `python quickstart.py`

#### JavaScript (browser)

- Filename: _index.html_
- Directory: _gmail/quickstart/_
- [Code sample for browser (JavaScript)](https://github.com/gsuitedevs/browser-samples/blob/master/gmail/quickstart/index.html)
- Replace <YOUR_CLIENT_ID> with your client ID and <YOUR_API_KEY> with your API key. Run with:
- `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:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m5sy4v73n10akqw2he1r.png)

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:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gmxhvtug5554rp48f8qu.png)

Congrats! Gmail API works and you can send your first email.

### Step 7: Create an email

To send a message, first you need to create one. For this, your app can use the drafts.create method which includes:

- Creation of a MIME message
- Conversion of the message into a base64url encoded string
- Creation of a draft

Let’s see how this is done in practice with Python:

```
def create_message(sender, to, subject, message_text):
  message = MIMEText(message_text)
  message['to'] = to
  message['from'] = sender
  message['subject'] = subject
  raw_message = base64.urlsafe_b64encode(message.as_string().encode("utf-8"))
  return {
    'raw': raw_message.decode("utf-8")
  }
def create_draft(service, user_id, message_body):
  try:
    message = {'message': message_body}
    draft = service.users().drafts().create(userId=user_id, body=message).execute()
    print("Draft id: %s\nDraft message: %s" % (draft['id'], draft['message']))
    return draft
  except Exception as e:
    print('An error occurred: %s' % e)
    return None  
```

and PHP

```
/**
* @param $sender string sender email address
* @param $to string recipient email address
* @param $subject string email subject
* @param $messageText string email text
* @return Google_Service_Gmail_Message
*/
function createMessage($sender, $to, $subject, $messageText) {
 $message = new Google_Service_Gmail_Message();
 $rawMessageString = "From: <{$sender}>\r\n";
 $rawMessageString .= "To: <{$to}>\r\n";
 $rawMessageString .= 'Subject: =?utf-8?B?' . base64_encode($subject) . "?=\r\n";
 $rawMessageString .= "MIME-Version: 1.0\r\n";
 $rawMessageString .= "Content-Type: text/html; charset=utf-8\r\n";
 $rawMessageString .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n\r\n";
 $rawMessageString .= "{$messageText}\r\n";
 $rawMessage = strtr(base64_encode($rawMessageString), array('+' => '-', '/' => '_'));
 $message->setRaw($rawMessage);
 return $message;
}
/**
* @param $service Google_Service_Gmail an authorized Gmail API service instance.
* @param $user string User's email address or "me"
* @param $message Google_Service_Gmail_Message
* @return Google_Service_Gmail_Draft
*/
function createDraft($service, $user, $message) {
 $draft = new Google_Service_Gmail_Draft();
 $draft->setMessage($message);
 try {
   $draft = $service->users_drafts->create($user, $draft);
   print 'Draft ID: ' . $draft->getId();
 } catch (Exception $e) {
   print 'An error occurred: ' . $e->getMessage();
 }
 return $draft;
}
```

### Step 8: Send an email

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

```
def send_message(service, user_id, message):
  try:
    message = service.users().messages().send(userId=user_id, body=message).execute()
    print('Message Id: %s' % message['id'])
    return message
  except Exception as e:
    print('An error occurred: %s' % e)
    return None
```

and PHP

```
/**
* @param $service Google_Service_Gmail an authorized Gmail API service instance.
* @param $userId string User's email address or "me"
* @param $message Google_Service_Gmail_Message
* @return null|Google_Service_Gmail_Message
*/
function sendMessage($service, $userId, $message) {
 try {
   $message = $service->users_messages->send($userId, $message);
   print 'Message with ID: ' . $message->getId() . ' sent.';
   return $message;
 } catch (Exception $e) {
   print 'An error occurred: ' . $e->getMessage();
 }
 return null;
}
```
### Step 8.1: Send an email with attachments

You can also create and send a multi-part MIME message. For example, this is how it looks with Python:

```
def send_message(service, user_id, message):
  try:
    message = service.users().messages().send(userId=user_id, body=message).execute()
    print('Message Id: %s' % message['id'])
    return message
  except Exception as e:
    print('An error occurred: %s' % e)
    return None
def create_message_with_attachment(sender, to, subject, message_text, file):
  message = MIMEMultipart()
  message['to'] = to
  message['from'] = sender
  message['subject'] = subject
  msg = MIMEText(message_text)
  message.attach(msg)
  content_type, encoding = mimetypes.guess_type(file)
  if content_type is None or encoding is not None:
    content_type = 'application/octet-stream'
  main_type, sub_type = content_type.split('/', 1)
  if main_type == 'text':
    fp = open(file, 'rb')
    msg = MIMEText(fp.read().decode("utf-8"), _subtype=sub_type)
    fp.close()
  elif main_type == 'image':
    fp = open(file, 'rb')
    msg = MIMEImage(fp.read(), _subtype=sub_type)
    fp.close()
  elif main_type == 'audio':
    fp = open(file, 'rb')
    msg = MIMEAudio(fp.read(), _subtype=sub_type)
    fp.close()
  else:
    fp = open(file, 'rb')
    msg = MIMEBase(main_type, sub_type)
    msg.set_payload(fp.read())
    fp.close()
  filename = os.path.basename(file)
  msg.add_header('Content-Disposition', 'attachment', filename=filename)
  message.attach(msg)
  raw_message = base64.urlsafe_b64encode(message.as_string().encode("utf-8"))
  return {'raw': raw_message.decode("utf-8")}
```
### Step 9: Read a specific email from your inbox

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:

```
import base64
import email
def get_messages(service, user_id):
  try:
    return service.users().messages().list(userId=user_id).execute()
  except Exception as error:
    print('An error occurred: %s' % error)
def get_message(service, user_id, msg_id):
  try:
    return service.users().messages().get(userId=user_id, id=msg_id, format='metadata').execute()
  except Exception as error:
    print('An error occurred: %s' % error)
def get_mime_message(service, user_id, msg_id):
  try:
    message = service.users().messages().get(userId=user_id, id=msg_id,
                                             format='raw').execute()
    print('Message snippet: %s' % message['snippet'])
    msg_str = base64.urlsafe_b64decode(message['raw'].encode("utf-8")).decode("utf-8")
    mime_msg = email.message_from_string(msg_str)
    return mime_msg
  except Exception as error:
    print('An error occurred: %s' % error)
```

If the message contains an attachment, expand your code with the following:

```
def get_attachments(service, user_id, msg_id, store_dir):
  try:
    message = service.users().messages().get(userId=user_id, id=msg_id).execute()
    for part in message['payload']['parts']:
      if(part['filename'] and part['body'] and part['body']['attachmentId']):
        attachment = service.users().messages().attachments().get(id=part['body']['attachmentId'], userId=user_id, messageId=msg_id).execute()
        file_data = base64.urlsafe_b64decode(attachment['data'].encode('utf-8'))
        path = ''.join([store_dir, part['filename']])
        f = open(path, 'wb')
        f.write(file_data)
        f.close()
  except Exception as error:
    print('An error occurred: %s' % error)
```
## Why is Gmail API better or worse than traditional SMTP? 

### Email protocol used

[Simple Mail Transfer Protocol](https://mailtrap.io/blog/smtp/) (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](https://mailtrap.io/blog/imap-pop3-smtp/) blog post. Google also provides the Gmail SMTP server as a [free SMTP](https://mailtrap.io/blog/free-smtp-servers/) 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.

### How are emails sent?

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](https://mailtrap.io/blog/smtp-commands-and-responses/).

### Authentication

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](https://mailtrap.io/blog/smtp-auth/).

### Quota

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](https://support.google.com/a/answer/166852). Also, keep in mind that the [maximum email size in Gmail](https://mailtrap.io/blog/email-size/) is 25MB.

### SMTP or API?

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](https://mailtrap.io/) 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 for sending emails](https://mailtrap.io/email-api/) can boast an extra level of security, which is crucial if you deal with sending sensitive data in emails.

---

I hope you enjoyed reading our guide on how to [send emails with Gmail API in Ruby](https://mailtrap.io/blog/send-emails-with-gmail-api/), GoLang, JavaScript, Java, NodeJS, PHP and more that was originally published on Mailtrap Blog.