Published on: 15.09.2018
I will show how to send emails from Python programing language using your Gmail account.
Mail server
When you want to send email from GUI/CLI app or from some computer language (in this example Python), you need to have access to a mail server.
The mail server is a computer that is in charge of sending and receiving your email.
Usually, you have access to the mail server via username and password.
With Gmail username is your email address and password should only be known to you :-).
Use yagmail
For sending emails from Python using your Gmail account IMHO best is to use yagmail packet.
You can waste time with smtplib library, but if you are using a Gmail account, just use yagmail.
Install it with:
pipenv install yagmail
Code examples
This is just a simple code example, for production code do not put your password in the source code.
Personally, I use one JSON file (this file is committed to source control with dummy credentials) per project for storing usernames and passwords for my personal projects.
This is a simple one line example:
1 2 3 |
import yagmail yag = yagmail.SMTP('YOUR_EMAIL@gmail.com', 'YOUR_PASSWORD').send('to@someone.com', 'subject', 'This is the body') |
I usually use it like this:
1 2 3 4 5 |
import yagmail yag = yagmail.SMTP('YOUR_EMAIL@gmail.com', 'YOUR_PASSWORD') body = 'large body that I have generated ' yag.send('to@someone.com', 'subject', body) |
Adding image in the body of the email, you need to use yagmail.inline
:
1 2 3 4 5 |
import yagmail yag = yagmail.SMTP('YOUR_EMAIL@gmail.com', 'YOUR_PASSWORD') contents = ["Some Text", yagmail.inline( full_path_to_image )] yag.send(send_to, subject, contents) |
My advice and experience
Gmail have daily limits of how much emails can you send.
At the time of this writing, it is 500 emails per day, if you exceed it you will not be able to send emails for next 24h (at least that happened to me, and I have sent only 100 emails in a row).
So, my advice is to have separate Gmail account for just for sending emails automatically from your code, you do not want to lose the ability to send email from your personal email account (I was there and it is not funny).
Also, have some delay between sending emails, when I got my account locked for 24h I have only sent around 100 emails in a row, but after I have added time.sleep(15)
I never had that problem again.
I am not using Gmail to spam people, I am using it for sending the email report to myself, mostly from web scraping tasks (around 20 per day).
If you have error 534
1 2 3 4 5 6 7 8 |
File "/usr/lib64/python3.6/smtplib.py", line 730, in login raise last_exception File "/usr/lib64/python3.6/smtplib.py", line 721, in login initial_response_ok=initial_response_ok) File "/usr/lib64/python3.6/smtplib.py", line 642, in auth raise SMTPAuthenticationError(code, resp) smtplib.SMTPAuthenticationError: (534, b'5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbvo\n5.7.14 ADvphf123p0OEtQXN-eqs-JnkK6mEO6qRtmz4c4RpYb9J_QmyzfLS8aYf3yjjSGcN7melQ\n5.7.14 5RpQd9saqWhfH6cjNUJclYW1qrLDpLPwbVOcAqipfT8-mHk_v5P1jvvDY3mnES9n4RfeHi\n5.7.14 AV6enQwb-XtJH9CMJUU4JrtJlhYDnOIBOluQoWbCoiYhs7JV-ej11QqeaJQTVkTIZStB4p\n5.7.14 HWPlUWD3LHmsIgnWN1N68psDRNs5C75SnCiYMr4wc7ve_Vj_Yf> Please log in via\n5.7.14 your web browser and then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 204-v6sm5976350wmh.25 - gsmtp') |
Solution is
Google blocks sign-in attempts from apps which do not use modern security standards (mentioned on their support page). You can, however, turn on/off this safety feature by going to the link below:
Go to this link and select Turn On
https://www.google.com/settings/security/lesssecureapps
From:
https://stackoverflow.com/a/26852782/2006674
Conclusion
Sending emails from Python is easy with yagmail.