Sending Gmail Messages with Python : Data Science Code

ritvikmath · Beginner ·🛠️ AI Tools & Apps ·5y ago

Key Takeaways

The video demonstrates how to send Gmail messages using Python by utilizing the Gmail API and Google Cloud Console, enabling users to receive custom messages on the go.

Full Transcript

hey everyone welcome back so in this video we'll be looking at how to send yourself gmail messages in python and the applications of this are pretty much infinite basically any piece of python code that you can imagine writing now you can take the results of that code and send yourself a gmail message containing those results so you can imagine that you might be on the go and your laptop might fire up every hour and run some kind of python script involving stocks or the weather or something and it'll be able to send a gmail and you'll be able to get that notification no matter where you might be on your phone so honestly a very powerful tool and the good news is that it's really really easy to do so i've included all the steps here this code will be available in the description below but i'm going to run through these steps live for you right now so the first thing is to visit this developersgoogle.com site which i've opened right here scroll down click on enable the gmail api so you can just hit next here go ahead and just click create right here and you see that you have this page saying you're all set and click on download client configuration so this contains information that python needs in order to connect to your gmail account so when you click on that button you'll see a file get downloaded called credentials.json go ahead and save that into the working directory where your code lives okay great now let's move on to the next step the next thing we'll do is visit this site this cloud console so this site looks like this the first thing you want to do is click up here and hit new project you'll be taken to this page and all you need to do is give the project a name so i'll be calling mine gmail tutorial go ahead and click create and it's in the process of being created so as soon as the spinning wheel stops spinning you'll be able to switch to that project okay perfect it's done so go ahead and switch to that project so go into your list of existing projects and select gmail tutorial the next thing you want to do is click on go to api's overview and then go ahead and click credentials this little key icon on the left bar perfect now we need to create a new set of credentials and you want to click service account this has enabled server to server app level authentication using robot accounts so essentially the service account is what we'll be using to connect to gmail now all you have to do here is give the service account a name so i'll just be calling it gmail service account perfect go ahead and click create you'll be asked for some additional things but don't worry about them just hit continue and hit done and you will have created that credential now scroll down to that credential which lives right here click on it make sure it's activated so if you see this disable that means it is currently enabled so that's good and then the last thing you have to do is click add key create a new key and make sure it's json click create that'll download a json file and go ahead and put that also in your current working directory where your code lives and where you downloaded that other json file and we're basically done that was all the hard stuff now we basically just run the cells that are contained in this notebook so the first cell here pip installs the google python api client which you probably don't have installed if you've never done this connection between google and python before so this will go ahead and install that for you now go ahead and run these imports most of which you should have but if you don't have any of them you can probably just pip install them pretty easily and the last step here is to set this environment variable called google application credentials and in here you're going to put the name of the most recent json file that you just downloaded from google cloud console so if i just start typing gmail it'll be this file right here and it'll be named something differently for you go ahead and run that the next chunk of code is going to create a file called tokengmail.pickle in your current working directory you can think of this basically just as a safe set of credentials that python uses to talk to google so that opens up a new tab go ahead and click on your email account it'll say this app isn't verified but click advanced click on go to quick start and then it'll say do you want this python code to be able to manage drafts and send emails go ahead and click allow hit allow again and you'll be good to go there so you can close that tab return to your code and that's all done now you can go ahead and run this function which basically just uses the newly created token gmail.pickle file that we just created above and uses it to create a authenticated object that python can use to talk to gmail and that object is stored in this service variable and the last couple functions are just creating a gmail message and sending a gmail message so this first function here creates a gmail message you can go ahead and read that pretty simple and this next function here sends the gmail message once it's created also pretty simple and it uses that service object which allows python to talk to google and that's it now we're just going to prove that it works so first we're going to create a message where it says your email address go ahead and put in whatever your gmail address is both here and here before the sender and receiver because in this application you'll be sending yourself an email the subject of the gmail goes right here and the body or the text of the message goes right here so we go ahead and create that message and then we go ahead and send that message using the service and here's the message and the middle argument should be me in general the middle argument is a user id but since we're sending this message to ourselves we can just put this special keyword me we get the success confirmation saying this is the message id and it's currently unread and it was sent to the person's inbox so let's go ahead and visit my inbox and see if we got a message with subject subject and text message text and indeed we got that email we got this message with subject subject and the text is here message text and we get this warning saying be careful because it was technically sent by a robot but we know that we sent it to ourselves so that's it you guys that's how you authenticate your google account with python and use that to send gmail messages to yourself and again you can use this for practically anything to get messages about stock trading algorithms that you might have developed you can use this to get weather alerts for specific weather patterns really anything you can do in python now you can receive that information on gmail no matter where you might be in the world alright so any questions leave them in the comments below hopefully this was helpful and i'll see you all next time

Original Description

How to send the results of ANY Python script to your Gmail so you can get custom messages on the go! Code used in this video: https://github.com/ritvikmath/Time-Series-Analysis/blob/master/Gmail%20API%20Tutorial.ipynb
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from ritvikmath · ritvikmath · 0 of 60

← Previous Next →
1 Math Team Update
Math Team Update
ritvikmath
2 Single Variable Calculus Volume of a Sphere - Proof 1
Single Variable Calculus Volume of a Sphere - Proof 1
ritvikmath
3 Single Variable Calculus Volume of a Sphere - Proof 2
Single Variable Calculus Volume of a Sphere - Proof 2
ritvikmath
4 Multivariable Calculus Volume of a Sphere Proof - Triple Integrals
Multivariable Calculus Volume of a Sphere Proof - Triple Integrals
ritvikmath
5 Multivariable Calculus Volume of a Sphere Proof - Double Integrals
Multivariable Calculus Volume of a Sphere Proof - Double Integrals
ritvikmath
6 The Euclidian Algorithm
The Euclidian Algorithm
ritvikmath
7 Proving the Chain Rule
Proving the Chain Rule
ritvikmath
8 Proving the Fundamental Theorem of Calculus Part 1
Proving the Fundamental Theorem of Calculus Part 1
ritvikmath
9 Proving the Fundamental Theorem of Calculus Part 2
Proving the Fundamental Theorem of Calculus Part 2
ritvikmath
10 Math Puzzle - Poison Perplexity
Math Puzzle - Poison Perplexity
ritvikmath
11 Math Puzzle - Poison Perplexity - Solution
Math Puzzle - Poison Perplexity - Solution
ritvikmath
12 Expected Value and Variance of Continuous Random Variables (Calculus)
Expected Value and Variance of Continuous Random Variables (Calculus)
ritvikmath
13 Expected Value and Variance of Discrete Random Variables (No Calculus)
Expected Value and Variance of Discrete Random Variables (No Calculus)
ritvikmath
14 Array Method
Array Method
ritvikmath
15 Complex Power Series and their Derivatives
Complex Power Series and their Derivatives
ritvikmath
16 Distributions - Intro
Distributions - Intro
ritvikmath
17 The Poisson Distribution
The Poisson Distribution
ritvikmath
18 The Bernoulli Distribution
The Bernoulli Distribution
ritvikmath
19 The Binomial Distribution
The Binomial Distribution
ritvikmath
20 The Continuous Uniform Distribution
The Continuous Uniform Distribution
ritvikmath
21 The Geometric Distribution
The Geometric Distribution
ritvikmath
22 The Triangular Distribution
The Triangular Distribution
ritvikmath
23 The Exponential Distribution
The Exponential Distribution
ritvikmath
24 The Borel Distribution + Notes on Poisson Distribution
The Borel Distribution + Notes on Poisson Distribution
ritvikmath
25 The Gamma Distribution
The Gamma Distribution
ritvikmath
26 The Normal Distribution
The Normal Distribution
ritvikmath
27 The Laplace Distribution
The Laplace Distribution
ritvikmath
28 The Chi - Squared Distribution
The Chi - Squared Distribution
ritvikmath
29 Overfitting
Overfitting
ritvikmath
30 Vector Norms
Vector Norms
ritvikmath
31 Truths Behind the Titanic : K-Nearest Neighbor
Truths Behind the Titanic : K-Nearest Neighbor
ritvikmath
32 The Mathematics of Breakups
The Mathematics of Breakups
ritvikmath
33 Sillyfish
Sillyfish
ritvikmath
34 Finding Optimal Paths - Dynamic Programming
Finding Optimal Paths - Dynamic Programming
ritvikmath
35 HowToDataScience : Scraping Twitter Data
HowToDataScience : Scraping Twitter Data
ritvikmath
36 Decision Trees
Decision Trees
ritvikmath
37 Perceptron
Perceptron
ritvikmath
38 Naive Bayes
Naive Bayes
ritvikmath
39 K-Nearest Neighbor
K-Nearest Neighbor
ritvikmath
40 Evaluating Machine Learning Models
Evaluating Machine Learning Models
ritvikmath
41 Decision Tree Pruning
Decision Tree Pruning
ritvikmath
42 K-Means Clustering
K-Means Clustering
ritvikmath
43 Gaussian Mixture Model
Gaussian Mixture Model
ritvikmath
44 Data Science - Fuzzy Record Matching
Data Science - Fuzzy Record Matching
ritvikmath
45 Time Series Talk : Autocorrelation and Partial Autocorrelation
Time Series Talk : Autocorrelation and Partial Autocorrelation
ritvikmath
46 Time Series Talk : Autoregressive Model
Time Series Talk : Autoregressive Model
ritvikmath
47 Time Series Talk : Moving Average Model
Time Series Talk : Moving Average Model
ritvikmath
48 Time Series Talk : ARMA Model
Time Series Talk : ARMA Model
ritvikmath
49 Time Series Talk : ARCH Model
Time Series Talk : ARCH Model
ritvikmath
50 Time Series Talk : White Noise
Time Series Talk : White Noise
ritvikmath
51 Time Series Talk : Stationarity
Time Series Talk : Stationarity
ritvikmath
52 Time Series Talk : ARIMA Model
Time Series Talk : ARIMA Model
ritvikmath
53 Time Series Talk : Lag Operator
Time Series Talk : Lag Operator
ritvikmath
54 Time Series Talk : What is Seasonality ?
Time Series Talk : What is Seasonality ?
ritvikmath
55 Time Series Talk : Seasonal ARIMA Model
Time Series Talk : Seasonal ARIMA Model
ritvikmath
56 So ... What Actually is a Matrix ? : Data Science Basics
So ... What Actually is a Matrix ? : Data Science Basics
ritvikmath
57 Derivative of a Matrix : Data Science Basics
Derivative of a Matrix : Data Science Basics
ritvikmath
58 Basics of PCA (Principal Component Analysis) : Data Science Concepts
Basics of PCA (Principal Component Analysis) : Data Science Concepts
ritvikmath
59 Eigenvalues & Eigenvectors : Data Science Basics
Eigenvalues & Eigenvectors : Data Science Basics
ritvikmath
60 The Covariance Matrix : Data Science Basics
The Covariance Matrix : Data Science Basics
ritvikmath

This video teaches how to send Gmail messages using Python by setting up a Gmail API project in Google Cloud Console, creating credentials, and using the gmail-python-api to authenticate and send messages.

Key Takeaways
  1. Visit Google Cloud Console and enable the Gmail API
  2. Create a new project and navigate to the API's overview
  3. Create a service account and generate a private key file
  4. Install the google-api-python-client library
  5. Set environment variables for authentication
  6. Create a token file using the authentication credentials
  7. Use the token file to create an authenticated object for sending Gmail messages
💡 Using a service account and private key file enables server to server authentication, allowing Python scripts to send Gmail messages without requiring user interaction.

Related Reads

📰
Creativity AI #82: Anthropic maps how people really use AI, designers shift from making to mending…
Explore how people interact with AI and the shift in design from making to mending, and learn to apply these concepts in your own work
Medium · AI
📰
The End of YouTube Search? Why AI Creator Discovery Is Becoming the Smarter Way to Learn in 2026
AI creator discovery is becoming a smarter way to learn, shifting focus from video content to creator expertise
Medium · AI
📰
Why AI Tools Are Becoming Essential for Modern Professionals
Learn how AI tools are revolutionizing everyday work for modern professionals, increasing productivity and efficiency
Medium · AI
📰
The Food Stayed Real. The World Around It Changed.
Learn how AI transformed real breakfast photographs into various art forms without altering the food itself
Medium · AI
Up next
I Built a Live Dashboard With Claude - Zero Coding, Zero IT Skills
Nicolas Boucher
Watch →