Sending Gmail Messages with Python : Data Science Code
Skills:
Prompting Basics60%
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Math Team Update
ritvikmath
Single Variable Calculus Volume of a Sphere - Proof 1
ritvikmath
Single Variable Calculus Volume of a Sphere - Proof 2
ritvikmath
Multivariable Calculus Volume of a Sphere Proof - Triple Integrals
ritvikmath
Multivariable Calculus Volume of a Sphere Proof - Double Integrals
ritvikmath
The Euclidian Algorithm
ritvikmath
Proving the Chain Rule
ritvikmath
Proving the Fundamental Theorem of Calculus Part 1
ritvikmath
Proving the Fundamental Theorem of Calculus Part 2
ritvikmath
Math Puzzle - Poison Perplexity
ritvikmath
Math Puzzle - Poison Perplexity - Solution
ritvikmath
Expected Value and Variance of Continuous Random Variables (Calculus)
ritvikmath
Expected Value and Variance of Discrete Random Variables (No Calculus)
ritvikmath
Array Method
ritvikmath
Complex Power Series and their Derivatives
ritvikmath
Distributions - Intro
ritvikmath
The Poisson Distribution
ritvikmath
The Bernoulli Distribution
ritvikmath
The Binomial Distribution
ritvikmath
The Continuous Uniform Distribution
ritvikmath
The Geometric Distribution
ritvikmath
The Triangular Distribution
ritvikmath
The Exponential Distribution
ritvikmath
The Borel Distribution + Notes on Poisson Distribution
ritvikmath
The Gamma Distribution
ritvikmath
The Normal Distribution
ritvikmath
The Laplace Distribution
ritvikmath
The Chi - Squared Distribution
ritvikmath
Overfitting
ritvikmath
Vector Norms
ritvikmath
Truths Behind the Titanic : K-Nearest Neighbor
ritvikmath
The Mathematics of Breakups
ritvikmath
Sillyfish
ritvikmath
Finding Optimal Paths - Dynamic Programming
ritvikmath
HowToDataScience : Scraping Twitter Data
ritvikmath
Decision Trees
ritvikmath
Perceptron
ritvikmath
Naive Bayes
ritvikmath
K-Nearest Neighbor
ritvikmath
Evaluating Machine Learning Models
ritvikmath
Decision Tree Pruning
ritvikmath
K-Means Clustering
ritvikmath
Gaussian Mixture Model
ritvikmath
Data Science - Fuzzy Record Matching
ritvikmath
Time Series Talk : Autocorrelation and Partial Autocorrelation
ritvikmath
Time Series Talk : Autoregressive Model
ritvikmath
Time Series Talk : Moving Average Model
ritvikmath
Time Series Talk : ARMA Model
ritvikmath
Time Series Talk : ARCH Model
ritvikmath
Time Series Talk : White Noise
ritvikmath
Time Series Talk : Stationarity
ritvikmath
Time Series Talk : ARIMA Model
ritvikmath
Time Series Talk : Lag Operator
ritvikmath
Time Series Talk : What is Seasonality ?
ritvikmath
Time Series Talk : Seasonal ARIMA Model
ritvikmath
So ... What Actually is a Matrix ? : Data Science Basics
ritvikmath
Derivative of a Matrix : Data Science Basics
ritvikmath
Basics of PCA (Principal Component Analysis) : Data Science Concepts
ritvikmath
Eigenvalues & Eigenvectors : Data Science Basics
ritvikmath
The Covariance Matrix : Data Science Basics
ritvikmath
More on: Prompting Basics
View skill →Related Reads
📰
📰
📰
📰
Creativity AI #82: Anthropic maps how people really use AI, designers shift from making to mending…
Medium · AI
The End of YouTube Search? Why AI Creator Discovery Is Becoming the Smarter Way to Learn in 2026
Medium · AI
Why AI Tools Are Becoming Essential for Modern Professionals
Medium · AI
The Food Stayed Real. The World Around It Changed.
Medium · AI
🎓
Tutor Explanation
DeepCamp AI