Send Gmail with Gmail API

How to get authentication 

1.Turn on the Gmail API

In order to use gmail API, we need authentication. We could get it from the Gmail API page. We could gain the ‘credential.json’ file, which users and authentication information is written.

2. Install the API

Run the following command in your command line.
pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib

3. Run the given code from google

You can see the “” in the website. You can connect to your own gmail by running that code.

These are the authentication process you must take.

Flow of writing code

First, I wrote a code which is copied from this gmail API page.

This still causes some errors. I verified some parts of it and add some codes.

  1. import needed file like base64, MIMEText and etc.
  2. message should be changed to a bytes-like object. It causes error if the text is str.
  3.  add the code that takes the authentication token from the “credential.json”.

After this step, we can send a message to someone through command line.

The following link is a python code that I wrote.

Application to my daily life

I have to send an email before and after the part time job. It is one of the most boring things in my daily life. I want to send it automatically so that I can lead a better life… Also, it might be combined with google calendar or google home.

What I want to do is to make the system that sends email to my boss. All I will do is to input my work time.



  • このエントリーをはてなブックマークに追加