This Article is based on (semi) automating the task of sending emails using SMTP concepts in Python.
What is SMTP?
The rules of the internet are defined by the many protocols used on it. For instance, we have HTTP which is used in the transfer of web pages. Likewise, we have a protocol called SMTP or Simple Mail Transfer Protocol for sending emails across the internet.
SMTP defines how email messages should be formatted, encrypted, and relayed between mail servers, and any other small details that your computer has to deal with.
The protocol for receiving emails is different. You can either use POP3 or IMAP. Since IMAP is more advanced and flexible, we use it to demonstrate how to receive emails. Check it out if you’re interested.
Note: If you are not connected to the Internet, Python will raise a socket.gaierror: [Errno 11004] getaddrinfo failed or similar exception.
Run the following command in the command prompt to install the
smtplib library. If you’re having some trouble refer to our Installation guide for Python.
pip install smtplib
Using smtplib to send Emails
The first step is to create a SMTP object that you can use to execute various methods and perform specific functions.
smtpobject = ('[email protected]', 587)
The port 587 is the port used by TLS, the encryption standard. May be different in some rare occasions. Once the
SMTP() connection has been made, you can start the TLS session using the
If the TLS call fails, you can always use SSL, another encryption protocol. It operates on the 465 port. You can call it using the
smtpobject = smtplib.SMTP_SSL('smtp.example.com', 465)
Now that we’ve established a connection and created end-to-end encryption we can begin safely with the login process. Use the login() function which takes your email and password as it’s parameters.
smtpobject.login('[email protected]', 'password')
Finally, it’s time to use the
sendmail() function. It takes three parameters, your email, the recipent’s email and the message to be sent.
You can use the newline character “
\n” to to separate the lines in your message. Keep in mind that your email message must begin with
smtpobject.sendmail('[email protected]', '[email protected]', 'Subject: Appreciation\n Dear Contributor, Your contributed article is greatly appreciated')
The return value from this function is a dictionary. If this dictionary is empty, it means your message was successfully sent to the recipient(s). Otherwise, there will be one key:value pair in the dictionary for each recipient the message failed to be sent.
Once you’re done with everything we use the
smtpobject.quit() to end the connection.
The table below shows the server names of each Email provider for the smtplib module. You’ll need these while connecting to your email account.
|Email Provider||SMTP server name|
|AT&T||smpt.mail.att.net (port 465|
|Verizon||smtp.verizon.net (port 465)|
This marks the end of our, “Sending Emails with Python SMTP” Article. Let us know if you have suggestions or contributions to make. Anything to help improve Coders Legacy is more than welcome.