How to use Gmail API to send an email in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey house come guys welcome to another two to a video my name Jay in this lesson I'm going to show you how to use Google Gmail API to send an email in Python I'm pretty sure most of you guys have used Gmail before and juniors probably the biggest email provider these days so here I'm on the Gmail documentation and I'll leave the link in the description below so basically in this video I'm going to teach you how to send an email using Gmail API before we can start using the Gmail API service we need to enable the API Thurs so which one to do is want to go to your google cloud platform and addresses council that cloud out google.com you want to choose the project I want to use I am using my Google cloud demo paja and a slider paja I want to go to navigation menu on the API and services you want to go to library they on the search for API service and I feel young to search for Gmail API and click on Gmail API service Amy showed the API services enable let me go back to the cloud a platform - ba and the other thing is the client file or your API key and that's basically absolutely right so open a new Python script and I create a utility module called Google Doc hi in here is the the script and can access the source code in the description below for my Google module I want to input the create service function and the create service function is a function I wrote to simplify the API service instance creation and there are couple libraries that we need to import we need to input a base64 library and found email down my moti parts module on to input my moti parts class I seen this as class one hundred percent sure I also need to import a from the email my text module I also need to input a mic text class and unfortunately I don't think Google does a really good job charting or creating the documentation for the Gmail API if we go to the guys session in here they provides you with a couple examples I want to kebabbed in and I found documentation to be a bit outdated so for example if you go to Python QuickStart I'm here they walk you to the steps how to turn the Gmail API how to install the Google client library as well as giving simple by this example for some reason just doesn't work for me yeah me me school no yeah so if you look to the example it's not very clear how this script is doing so to send an email let me go to references page to something email using the Gmail API want to use user dumb messages resource and within this resource won't use the same method in couple things to consider when you send email the maximum file size is certified megabytes meaning that if you send an email with attachment all the attachment size cannot accumulate over 35 megabytes and these are the my data type the message does sense method will accept and below is the parameters so I'll be merely using the user ID parameter and for the scope how is the mail.google.com scope which gives me for permission to using my gmail account so we need to provide the RAL string and which is gonna be bytes datatype into an ID is optional and below is just an example you can try to follow to this example I found this example to be a little bit more difficult to follow for beginners now make sure you're going to break down all the steps so that is easier two graphs how to use the send method I'll be using OAuth 2.0 to often take a my credential all right so here's my me make the font a little bit bigger so here's my client secret file which is my client file in my Python script I'm going to create couple variables to store my variable values the first won't be client secret file and it's going to be my client secret file path in the opinion is going to be Gmail in from the version Jimmy is still Gmail API is still on version 1 and hopefully at one point Google will move on to version 2 so the scope name is going to be mail.google.com and it's going to be Alice I'm going to create my gmail API service instance I always might create service function in my first parameter is calling a secret file a piane API version and scopes in here me go batch my folder I'm going to delete my token file now if I run the highlighted code hands come to launch the authentication page and it's going to ask me to choose my account I want to use I'm going to use my C I'm just an email to my data dummy account at gmail.com so choose my learnt data analysis account and click on advanced to ignore the page they want to click on this link at the bottom now based on the scope that you provide Gmail API is going to ask you to grant the permission I'm going to just say yes too often oh and yeah scope his oh so he I forgot for - let me fix that yeah let me try again so I'll wrong just the highlighted code all right so once you are authenticated you should see this message the authentication flow has complete you must close the window I'm going to close this window now if I prune the attributes and methods of the service instance you should see that users method is not available the next item on the agenda is we need to create message to provide to our email template or email body paint is going to be in your message I'm going to type you won $100,000 and this country our message in the email body Templar okay I forgot to create my my multi-part instance L name this object my message he goes to my multi-part I'll move the email message able to the top once I create my my multi-part our instance I can assign the sub J and recipients inside the mind message object we need to provide recipients in you so with the reference to I'm going to send this image my data dummy account at gmail.com in for the sub shop want to use the subservience for the SAP jar I'm going to just set you one and two it has the message to the email template want to show you want to insert my message object that attached they want to insert a mic text class they want to insert a message in this case would be my email message be able followed by the email body type so this can be plain or HTML I'm going to use plain text and once that's done we need to convert a person to raw string now into the base64 library da URL oops da URL save on the school be 64 inkle instead of 64 income method want to provide the mime message option despite that école and this on a block is going to be our email message I can send an email to data dummy account at gmail.com so here into the service instance that users that messages that sent and for the user ID we can use the spatial would on me which reference to the often decay user I also need to provide body and it's gonna be a rough string so we need to insert to Sherry you'll provide the raw string object and lastly we need to execute the statement to send email and I'm also going to print the message option right so here means to many this session I'm going to launch my email account so just the email dice saying previously and right now if I are brown the script no email so I have a type of symbol okay so this should be email when dragon if I press f5 to run the script and can I email let me check oh so here's a type voicing so my mo teapots importing vison those are the typos I'm shagging a multi-person I define so me wrong so let me import libraries me only and if I run the slap block oh ok so here I mean so here's my mistake so this should be closed when to see and article no me too many the session and hopefully just a loss mistake right so here I'm getting a response so this the the email ID and it's the email ID followed by the label ID and say meaning that this email found often decay users is saved to the sent folder if I go back to my gmail account and here's the email you $100,000 and file by the email subject so the same song I want to share in this video and hopefully you guys found the video useful info so from the source code in the link in the description below and as always send you guys watching I'll see you guys on the next video
Info
Channel: Jie Jenn
Views: 49,606
Rating: undefined out of 5
Keywords: Send an email using Gmail API, how to use Gmail API in Python, send email from Gmail Account in Python
Id: 44ERDGa9Dr4
Channel Id: undefined
Length: 14min 15sec (855 seconds)
Published: Wed Jun 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.