How To Send Emails Using Python?

Manan B Shah
Dev Genius
Published in
4 min readJul 14, 2020

--

In this blog, we shall be talking about how can you send an email using python in just a few lines of code. Do read the blog completely in detail to avoid error’s while executing. Let’s get to the main flesh of today’s blog.

“Image by author”

Methods to send Email using Python?

There are two Approaches to send email using python which is as follows:

  1. One is web browser automation using selenium. It is a powerful tool to automate stuff on a web browser. It works with all OS and browsers and it’s script is written in various programming languages such as Python, c# and many other languages

If you really want to make your daily routine jobs automated selenium is your thing. Mastering selenium will help you automate your logins, checking on your tweets and automating your Whatsapp, Facebook, and even sending your mails.

2. The Second Approach is using python’s Library: smtplib

The smtplib module defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener. For details of SMTP and ESMTP operation, consult RFC 821(Simple Mail Transfer Protocol) and RFC 1869(SMTP Service Extensions).

SMTP stands for Simple Mail Transfer Protocol. The smtplib modules are useful for communicating with mail servers to send mail. Sending mail is done with Python’s smtplib using an SMTP server.

In Today’s Blog, we shall be using the 2nd Approach to send an email using python.

Photo from MasHable

Setting up your email Account before we get started

In todays blog we shall be using Gmail as our email services. I suggest everyone to follow the below procedure care fully before we get started as python requires few permission’s from the Gmail Security for it to function. The Steps are as follows:

  1. Create a new Google account or use your existing account

2. Turn Allow less secure apps to ON.

Be careful this might compromise the security of your Gmail account. So its is Advisable to use a spare Gmail account or create a new one. Don’t Worry you turn ON and OFF the security feature as per your choice whenever you want. so once you are done performing the below email automation you can turn the security feature ON again back to normal.

Let’s check out the python code to send a email using python

Explanation

  1. firstly we import the python in-build library smtplib. The smtplib module defines an SMTP client session object that can be used to send mail to any Internet machine
  2. Then we create two variable’s recipient and message such that recipient is to enter the receivers mail and message variable to enter the message to be sent.
  3. Then we define a function SendEmail with parameter’s as recipient and message.
  4. Then we tell the smtp server which kind of email platform we are using to send the mail(Gmail) and the port number Gmail uses to send mail is 587 which we need to specify
  5. The server.ehlo () command identifies the server.
  6. server.starttls() activate Transport Layer Security to the outgoing mail message.
  7. In the next step we log in with our own credentials of our Gmail account → “SenderEmail@gmail.com” = your Gmail address “SendersEmailPassword” = Your password of the above mail

8. server.sendmail() commands the server to send the mail where → “SenderEmail@gmail.com” = your Gmail address and two other parameters required to send the mail.

9. close the server and then call the function with the required parameters and then see the result. In which you shall input the mail of the receiver and message to be sent.

OUTPUT :

“Image by author”

REFRENCE :

https://docs.python.org/3/library/smtplib.html

Conclusion

We have learned one of the most simple automation to send emails using python. If you want me to write more such content on Automation of Boring stuff which we do in our day-to-day lives reach me out below and let me know.

Like what you’ve read? Make sure to check out all our blogs for more and check out the below website to contact us with any doubts , suggestion , and any new ideas regarding the blogs

CONTACT

https://pythonmanin.wordpress.com

--

--