Python is versatile language and it can be used to automate lot of stuff. In this article we will see how to automate sending email. This is very important considering, you can have a script created to watch for changes or trigger some news and by using email script, you can get notification on your email.
We will look at following :
- Simple python script to send email (without subject).
- Simple python script to send email (with subject).
- Module python script for sending email with attachment.
Simple python script to send email (without subject).
Here is sample script
While running it, I received following error.
$ python3.6 email_simple.py Traceback (most recent call last): File "email_simple.py", line 9, in <module> server.login(sender,sender_password ) File "/usr/local/lib/python3.6/smtplib.py", line 730, in login raise last_exception File "/usr/local/lib/python3.6/smtplib.py", line 721, in login initial_response_ok=initial_response_ok) File "/usr/local/lib/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=AKgnsbvX\n5.7.14 Dzy6Au7xIS9KSK1j-pI_9paJGuDyWAHWQ4B14jcckx6T8llJK-Jo_IBUX07XvLj_nHczud\n5.7.14 NUp86Os99aF5vqElcKxUoQDNnvP-xfDtaBH1Zq7dlMk9TEPbhEFo8DUKuGF5PhbkMXJN7O\n5.7.14 DFiLiu2hzzwgEBqs4nSh79AtcXAJIupUa4E_sdvxQ3kue6Z-qWPD2sxnGeUFm_ap2y2bto\n5.7.14 7fre7Gti4FNOCa29QALLDuNZo1Fms> Please log in via your web browser and\n5.7.14 then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 g78-v6sm37246326pfe.39 - gsmtp')
To resolve this issue go to following link and allow less secure app to access.
Once you have done this change, your script will work like a charm.