Python is a multi-purpose programming language that is used for various purposes such as data analytics, web scrapping, sending emails, etc. Python makes it super easy to use built-in modules to automate emails rather than other programming languages that provide complex methods.

This write-up demonstrates a complete procedure for sending an Email using Python.

How to Send an Email Using Python?

Python allows us to send emails using the built-in module named “smtplib”. SMTP stands for Simple Mail Transfer Protocol and is a protocol to send emails. For instance, if we want to send files, we can use FTP(File Transfer Protocol), if we want to access WWW(World Wide Web), we use HTTP protocol.

Let’s demonstrate it practically with the help of an example:

import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()

server.login('[email protected]','pjxlxzlzoxwsyfdh')
server.sendmail('[email protected]','[email protected]',' Welcome to Python Course 2023!')
print('Mail Sent')

In the above code:

  • The smtplib library is imported.
  • An instance(object) is created called server that encapsulates the SMTP connection. It takes 2 arguments, the server address and the port number. As we use gmail, not any third-party service so our server address would be smtp.gmail.com and the port number for gmail is 587.
  • server.start is used to create a connection but in order to make our connection secure we use tls (Transport Layer Security).
  • server.login method is used to provide the login credentials such as username and password.
  • server.sendmail is used to send the mail. This method takes 3 parameters, your email address, the email address to whom you want to send the message, and the message.
  • Finally, to verify that the code works perfectly fine, Mail sent message is printed on the terminal.

The output of the above code is as follows:

Let’s open our gmail and check whether the message has been sent or not.

It can be seen clearly that the message is sent to the user.

We can also send emails to multiple recipients using Python.

Let’s see an example:

import smtplib
li = ["[email protected]", "[email protected]"]

for mails in li:
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login("[email protected]", "pjxlxzlzoxwsyfdh")
    server.sendmail("[email protected]", mails, "Welcome to Python Programming! ")
    print('Mail has been sent successfully.')

In the above code:

  • A list of emails is provided to whom we want to send the message.
  • A for loop is used which iterates through the list and sends the email.
  • The remaining steps are the same as in the previous example.

The output of the above code is as follows:

Important Things to Do Before Sending Email

It is mandatory to enable the 2-step verification by clicking on the Security Section in your browser as shown below:

If the 2-step verification is not enabled, the program will throw an error as shown below:

Conclusion

Python is a multi-purpose programming language and is also used to send emails. The built-in module smtplib in Python is used. SMTP connection is started using the server address and the port number. To secure the connection tls is used. Afterward, the login credentials are provided using the login() method, and the mail is send to the provided email with the message using the sendmail() method. This article has explained how to send an email in Python.