Steps to send an email using Python
Yes, you heard it right. We can send emails using python.
When you try to google "how to send email with python", you may see many outdated ways. This post helps you with updated version/latest way.
We are going to use SMTP library to do this.
What is SMTP and why we are using it:
SMTP stands for simple mail transfer protocol. It's a protocol or a standard for emails to be sent from computer to computer. Anytime you send emails to anybody it needs to have a server that communicates the language of the email. SMTP is going to allow us to create what we call 'SMTP server'.
Note: This email section will not work if the user has 2 factor Authentication for the account without app specific password. I have given a workaround for this at the bottom.
Go ahead and create a dummy Gmail account or you can use your regular one if you want.
Create a new python file. (make sure your file name is not email.py. If you do, you may get an error)
- Go ahead and type the below code.
Save it and run your python file.
You will see the print message "email sent sucessfully!" if you have not done any mistakes
Now login to your gmail account and check.
Hurray!! You can see an email in your account.
If you are getting an error, follow the link below. This could be because if the user has 2 factor Authentication for the account without app specific password.
- Head to the following URL and log in:
For the Select App dropdown, select Mail.
For the Select Device dropdown, select Custom and then give it any name you want.
It will provide you with a 16 character code like:bghyfrjudekiyhdr
In the Python app, replace your password with the code above:
After doing this I got a doubt, whether I can send a text message to my phone using Python. Let me try that and get back to you soon.
Meanwhile, Take care!