Sending emails manually is no doubt a time-consuming and tough task, a programmer can easily automate this using his favorite programming language, in this tutorial, you will learn how you can send emails using smtplib module in Python.
The smtplib module defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP (or Extended-SMTP) listener. It comes pre-installed with Python, so we don't have to install anything.
If you're not sure what SMTP (Simple Mail Transfer Protocol) is, it is basically a communication protocol for electronic mail transmission, it was first introduced in 1982 and updated in 2008 by RFC 5321 to Extended SMTP additions (which is what's used today). So in a nutshell, mail servers use this protocol to send mail messages.
Related: How to Read Emails in Python.
Alright, let's get started, let's send our email:
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart
Message bodies with MIME formatting consist of multiple parts, each part may contain any type of data you actually want to send.
Let's define our parameters:
# your credentials email = "firstname.lastname@example.org" password = "password" # the sender's email FROM = "email@example.com" # the receiver's email TO = "firstname.lastname@example.org" # the subject of the email (subject) subject = "Just a subject"
Pretty straightforward, email and password variables are the credentials of the email address you wanna send with, FROM and TO are the sender's email and receiver's email respectively (email and FROM are usually the same), and subject is the title (or subject) of the mail we'll send. Make sure you edit these variables on your own needs, you can send to multiple email addresses by using a list of emails in TO variable.
Now let's construct our mail, we gonna use MIMEMultipart object here, so we can attach multiple parts of the mail such as HTML, image and so on.
# initialize the message we wanna send msg = MIMEMultipart() # set the sender's email msg["From"] = FROM # set the receiver's email msg["To"] = TO # set the subject msg["Subject"] = subject
Note: If you're using Gmail account to send, make sure you turn "Allow less secure apps" to ON, although this will make everyone can access your gmail account just with your email and password.
We also set the From, To email addresses and the subject. Let's construct the message body, I'm gonna use HTML in this example:
# set the body of the email text = MIMEText("This email is sent using <b>Python</b> !", "html") # attach this body to the email msg.attach(text)
After the construction of the message body, we attached it to the MIMEMultipart object we just created. Let's see how this email looks like:
Content-Type: multipart/mixed; boundary="===============3518853130198016098==" MIME-Version: 1.0 From: email@example.com To: firstname.lastname@example.org Subject: Just a subject --===============3518853130198016098== Content-Type: text/html; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit This email is sent using <b>Python</b> ! --===============3518853130198016098==--
As you may notice, each part is separated with a fixed set of characters, the first part contains the mail header and the second contains the actual message body we just attached, note that if there is any non ASCII characters, it'll automatically change the encoding for us, so you usually don't have to worry about that. For more information about the MIME standard, check this Wikipedia page.
You can also attach other file types using email module, for example, if you want to send audio files, you can use MIMEAudio class from the email.mime.audio package, please refer to the official Python documentation for detailed information.
Now that we have the mail ready to be sent, let's connect to the SMTP server using smtplib:
# initialize the SMTP server server = smtplib.SMTP("smtp.gmail.com", 587)
I've used Gmail SMTP server here, with the port 587. If you want other SMTP servers (such as Outlook or Yahoo), check the list of SMTP servers and their port numbers.
Putting the connection to the SMTP server into TLS mode:
# connect to the SMTP server as TLS mode (secure) and send EHLO server.starttls()
And finally, we login using the account credentials and terminate the session after we send the email:
# login to the account using the credentials server.login(email, password) # send the email server.sendmail(FROM, TO, msg.as_string()) # terminate the SMTP session server.quit()
sendmail() method performs an entire mail transaction, it takes several arguments: the address sending this mail (FROM), a list of addresses to send this mail to (if it's a string, then it will be treated as a list with 1 address), and the message to send.
After I executed this piece of code (of course I used real gmail credentials), here is the screenshots of the email received:
Awesome, There are many use cases for this ! You can for example, create your own custom alerts to notify you when something happened, or you want to send email confirmation to users when they create an account on your website, or send emails to members of your organization, or sending keylogger results, possibilities are endless !
An example use case of this, is that you can use an email extractor to grab a good mail list and send marketing compaigns to reach a broad number of audience !
Let us see what you built with this in the comments below !
Finally, if you're a beginner and want to learn Python, I suggest you take Master Python in 5 Online Courses from University of Michigan, in which you'll learn a lot about Python, good luck!
Happy Coding ♥View Full Code