Send E-Mail using Python language

Today we are going to learn how to send mails using python language. Python is a very powerful language, here is a module called SMTP ( Simple Mail Transfer Protocol ), with this module you can automate your mails by using you Gmail account.

In this article, we are going to using an Gmail Account to send mail ,in this article we are goin to use port number “587”.

send emails with python

Automate WhatsApp with python

Steps to send mail from Gmail account

First of all, “smtplib” library must be foreign.

After that, to make a session, we are going to be exploitation its instance SMTP to encapsulate AN SMTP association.

s = smtplib.SMTP(‘’, 587)

In this, you would like to pass the primary parameter of the server location and therefore the second parameter of the port to use. For Gmail, we have a tendency to use port variety 587.

For security reasons, currently place the SMTP association within the TLS mode. TLS (Transport Layer Security) encrypts all the SMTP commands.

After that, for security and authentication, you would like to pass your Gmail account credentials within the login instance.The compiler can show AN authentication error if you enter invalid email id or positive identification.

Store the message you would like to send out a variable say, message. exploitation the sendmail() instance, send your message. sendmail() uses 3 parameters:

sender_email_id, receiver_email_id and message_to_be_sent. The parameters ought to be within the same sequence.

how to automate emails with python
# Python code to illustrate Sending mail from
# your Gmail account
import smtplib

# creates SMTP session
s = smtplib.SMTP('', 587)

# start TLS for security

# Authentication
s.login("sender_email_id", "sender_email_id_password")

# message to be sent
message = "Message_you_need_to_send"

# sending the mail
s.sendmail("sender_email_id", "receiver_email_id", message)

# terminating the session

Sending same message to multiple people

If you want to send the same messages to various peoples then you can use for loop for that, you can make the lists of all the emails and mails in just one click.

# Python code to illustrate Sending mail
# to multiple users
# from your Gmail account
import smtplib

# list of email_id to send the mail
li = ["[email protected]", "[email protected]"]

for dest in li:
	s = smtplib.SMTP('', 587)
	s.login("sender_email_id", "sender_email_id_password")
	message = "Message_you_need_to_send"
	s.sendmail("sender_email_id", dest, message)
automation with python

Important Points:

  • This code will send straightforward mail that doesn’t have any attachments or any subject.
  • One of the foremost superb things concerning this code is that we will send various emails in just one click this and Gmail principally place your mail within the primary section. Sent mails wouldn’t be detected as Spam typically.
  • File handling may be wont to fetch email id from a file and more used for causing the emails.

Python Automation with Python

Leave a Comment