SENDING EMAILS IN PYTHON
In this tutorial, we are going to learn about sending emails in Python. Now, because of the accessibility that the built-in python libraries offer, we just need to import the smtplib module and start sending mails through the SMTP client server.
What is SMTP?
SMTP stands for Simple Mail Transfer Protocol which is usually used to send emails to any existing email ID active. In this tutorial, we are going to use a gmail account to send an email. We will be looking at the two ways to send an email through the SMTP client server by following simple steps as follows:
- Using SMTP method (for port 587)
- Using SMPT_SSL method (for port 465)
If port 587 and 465 doesn’t make sense right now, don’t worry! Different websites use different ports and for gmail we use port numberS 587 and 465 (depending on which method you want to use).
Now before we actually start coding make sure that you have changed your gmail security settings a bit by going on this link below (make sure you are logged on to your gmail account before accessing this link):
https://myaccount.google.com/lesssecureapps
Once you open the link, turn ‘ON’ the Allow less secure apps. Make sure this change is temporary just to test the SMTP mail sending through Python. You can turn it off by going on the same link.
Importing the SMTP Module
Once you have allowed less secure apps, it’s time to import the smtplib module on your python IDE.
import smtplib
Establishing the SMTP Connection
As we already know that Python comes with the smtplib module which handles all the relevant areas of establishing a connection with the client server, authenticating login details and sending emails.
There are different ways or methods to create a connection. Right now, we will be focusing on creating a simple connection to quickly test sending mails. The simple protocol mainly uses 587 port, which we will use as well.
import smtplib try: server = smtplib.SMTP('smtp.gmail.com:587') server.ehlo() server.close() print("connection established!") except: print("Something went wrong")
That’s all you need to do, pass on the server address which is smpt.gmail.com in our case, entering port number which is 587 for establishing a simple connection. Next we call the ehlo()
method which helps us identify to the SMTP client server. And finally close()
the connection.
import smtplib email_user = 'sender@abc.com' email_send = 'sender@abc.com' email_receive = 'receiver@abc.com' password = '' email_text = 'Hello guys. This is testing!' try: server = smtplib.SMTP('smtp.gmail.com:587') server.ehlo() server.login(email_user, password) server.sendmail(email_send, email_receive, email_text) server.close() print("mail sent!)") except: print("Something went wrong")
After verifying and establishing a connection, we have to login by adding the sender details (email and password) and the receiver details (email). Once you follow the steps, you can run the code and check your inbox, it should have a an email.
Establishing a SSL Secure Connection
When an email is sent through a secure connection (SSL), it is done via port 465. This is far more secure practice to use SSL. You can simply use SSL method using the starttls() method, this method is an upgrade of the simple SMTP connection.
import smtplib email_user = 'sender@abc.com' email_send = 'sender@abc.com' email_receive = 'receiver@abc.com' password = '' email_text = 'Hello guys. This is testing!' try: server = smtplib.SMTP_SSL('smtp.gmail.com:465') server.starttls() server.ehlo() server.login(email_user, password) server.sendmail(email_send, email_receive, email_text) server.close() print("mail sent!)") except: print("Something went wrong")
Make sure you place the starttls() method before the elho() method, otherwise you may encounter an error. Once you run this, you will receive an email in your inbox.
Conclusion
Lastly, after working through all the code, you can try out sending email through python by using other email provider that supports SMTP.