Use Python to send email from Gmail

Matjaž Hozjan
2 min readSep 20, 2021

Send anything with your gmail or any email from any of your scripts using Python. That’s one of the first things that I did when I started learning python. My most used case is a local Craiglist web page, which am scraping for things my kids need as they are constantly growing :) (skies, bikes etc) and once some new ad pop’s up with my criteria mail is in my inbox. That’s how am reducing personal time for browsing and am usually 1st one to call. Is like win win scenario.

Please note that I am not taking any responsibility if you will modify your Gmail settings and use my code. It is a fact that you will make your Gmail less secure and your password will be stored somewhere near the code. That’s why use anything from these articles at your own risk!

1st: go to gmail settings here to enable less secure apps

2nd: Got to your gmail account than to Settings/Forwarding and POP/IMAP and enable IMAP

Gmail, settings, IMAP, Forwarding

3rd: Now is the python part time. In these code port 465 is used as is meant for SSL. If for some reason you are wanna be even less secure (TSL) than go with port 587.

Config your mail details in separate file (mailDetails.json) and make that file secure as much as you can:

{
"username": "your gmail username",
"password": "your gmail password",
"receiver_email": "mail to whoom you are send mail to @ mail.com",
"receiver_emails": ["SendmultipleEmail1 @ test.com", "SendmultipleEmail2 @ something.com"]
}

Python part here:

Have fun and gave me some claps or follow me for more tips. Cheers!!

--

--

Matjaž Hozjan

FX, stock trader and full time DataScientist at SportRadar AG