Python SMTP – Sending Emails



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.


Installation

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 = ('smtp@example.com', 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 starttls() function.

smtpobject.starttls()

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 SMTP_SSL() function.

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('Coderslegacy@example.com', '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 "Subject: subject\n".

smtpobject.sendmail('CodersLegacy@example.com', 'Contributor@example.com',
'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 ProviderSMTP server name
Gmail smtp.gmail.com
Outlook.com/Hotmail.com smtp-mail.outlook.com
Yahoo Mail smtp.mail.yahoo.com
AT&T smpt.mail.att.net (port 465
Comcast smtp.comcast.net
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.

Leave a Reply

Your email address will not be published. Required fields are marked *