SMTP with smtplib

SMTP with smtplib


Uses of Smtplib:

        The Python library smtplib is another amazingly useful library. It's also super fun to use since you can automate one of your most essential uses in technology, email. That's right, smtplib is used to send emails through python code. The name smtplib is a combination of two things, SMTP, and lib. SMTP stands for Simple Mail Transfer Protocol and lib is short for "library". In this post, you will learn all about SMTP, and smtplib, and be able to send emails from Gmail, Yahoo, etc.

SMTP:

        What is SMTP? As you read earlier, it stands for Simple Mail Transfer Protocol—an internet standard communication protocol for electronic mail transmission. Mail servers and other message transfer agents use SMTP to send and receive mail messages. An SMTP server is a computer or an app responsible for sending emails. Sometimes SMTP is paired with IMAP or POP3, which handles the retrieval of messages, while SMTP handles sending them. The library smtplib can send messages but there are no methods to check your mail or something like that.

Setup, Installation, and Initialization:

        To use smtplib, you will need to have your email and password ready. Keeping your password on a document is risky, so you can stop this with an app password. Check out the links below for how to create an app password for an email client.
        Once you've created an app password with the client you use and have saved it in a variable so you don't forget, then save your email into a variable (example@gmail.com). Python comes built-in with smtplib so there is no need to install anything. We do need to import smtplib into your library though and you can do that with the following code.
from smtplib import SMTP
        Now you are ready to learn how to send emails! Check out the next part to see that.

Sending an Email:

        We are finally at the main point in this blog, sending an email. To do this, we are going to need a from address and to address. Pick someone who you can contact immediately to check if the email was sent to who you want it to. Or you can even send it from yourself to yourself. Just check your email after the terminal closes.

        To send an email, you first need to start up the SMTP() class that you imported. SMTP() takes in one mandatory argument and one optional one. The optional one will only need to be put if you want to or if there is an error. The mandatory argument is the host, which is a string to let the class know what email server you are using (e.g. "smtp.gmail.com"). The optional argument is the port, but I recommend you put the port in just in case the library uses the wrong one. Below is a list of all the hosts and port numbers for each email server.
  • Gmail: "smtp.gmail.com", 587
  • Yahoo: "smtp.mail.yahoo", 587 or 465
  • Hotmail: "smtp.live.com" or "smtp-mail.outlook-com" (try the other if one doesn't work), 25 or 465
        Save your host and port in two different variables so you can use them later to start the connection to the email server. The code snippet below shows you how to start your connection with your mail server.
connection = SMTP(HOST, PORT)
        After your connection is saved in a variable, we'll need to activate a method that encrypts our email in case somebody intercepts it. The method is known as starttls(). TLS stands for Transport Layer Security, which basically encrypts the data you have given it so hackers and eavesdroppers are unable to see what you transmit. Check out the code below to see how to use the starttls() method on your connection.
connection.starttls()
        The next step is to log in to your email account using the email and password you saved earlier. There is a method in the SMTP class known as the login that takes in two arguments, user and password. User is your email and password is your password. The following code block shows you how to log in to your email.
connection.login(user=EMAIL, password=PASSWORD)
        After logging in, you have to send the email. We need to use a method known as send_mail(). The method takes in a few arguments. First, we'll need the person sending the mail, which is you. This will be your email, so use the variable you saved before when we logged in. Next is the people you're going to send the message to. It will be a list with each email, even if there is only one person to send the mail to. After that, you need the message, which is a string. Take a look at the code below to see how to send the email.
connection.send_mail(from_addr=EMAIL, to_addrs=["example@gmail.com"], msg=MESSAGE)
        The last bit of code you need is to quit the connection. The code to do that is below.
connection.quit()
        After you have your quit method in your document, your code is complete. Take a look at the solution to sending an email with this link: Sending Emails Solution (GitHub Gists).

        You've finally sent an email using smtplib! Now you can incorporate this into projects of your own! If by any chance this doesn't work, email me at spewingfacts@gmail.com. Use what you've learned today and complete fun projects or useful things that can get you ahead in life. Leave a comment below and check out my other posts.

Comments