circuit

How to Send Mail Using Python

Introduction to Simple Mail Transfer Protocol in Python




Automation using Python is a fun task and it can help in providing fast and efficient solutions. With the launch of the new Python 3.10, Python is gaining huge popularity among all the programmers.

Python is a programming language that is very easy to learn. Apart from that their many protocols which are very useful for programmers. We can send and receive mail using one of such Python protocols.

The protocol which is used to send emails in Python is called Simple Mail Transfer Protocol and is commonly referred to as SMTP. Python provides smtplib module for us to use SMTP.

Let us see how to send HTML email using Python smtplib module…

Following is a simple example to send email using Python script:

import smtplib
sender='writeaniketz@gmail.com'
recievers=['myfollowers@gmail.com']
message="""From: From aniket<writeaniketz@gmail.com>
To: To my followers <myfollowers@gmail.com>
Subject: Your Weekly Newsletter from Aniket
Hi readers,
This month is interesting month because it is the last month of the year. Not only that, I will also be writing about new technologies this month which includes NFT, Blockchain, Crypto, New Internet, Web3 and many more trending technologies.
Hence, don't forget to follow me if you are interested in new technologies and programming tutorials.
Your Favourite Writer,
Aniket
"""
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message)
 print "Email is successfully sent"
except SMTPException:
print "There was a error and email is not sent, please check"

In the given Python Script the main important aspects are sender, receiver, and message. Please note that we are using a localhost server and we can also use a service provider like Gmail, Yahoo, etc to send an email.

Sending Email from your Gmail Account Using Python Script:

import smtplib
s=smtplib.SMTP('smtp.gmail.com',20)
s.starttls()
s.login("sender_email_id", "email_id_password")
message = """ From: From aniket<writeaniketz@gmail.com>
To: To my followers <myfollowers@gmail.com>
Subject: Your Weekly Newsletter from Aniket
Hi readers,
This month is interesting month because it is the last month of the year. Not only that, I will also be writing about new technologies this month which includes NFT, Blockchain, Crypto, New Internet, Web3 and many more trending technologies.
Hence, don't forget to follow me if you are interested in new technologies and programming tutorials.
Your Favourite Writer,
Aniket
"""
s.sendmail("sender_email_id", "receiver_email_id", message)

s.quit()

Here the main components are sender, receiver, and message. Firstly we import smptlib the module. Then we start TLS mode which is transport layer security that provides extra security to your email.

After that we login into our email using s.login() which has our email address and our password. Later we write a message in double or triple quotes, it is the main body of our email and it can contain any text which you are planning to send.

Further, we send our email using s.sendmail() which has three parameters mainly sender email id, receiver email id, and message that contains the main text.

Lastly, we use s.quit() to completely terminate the python script and log out of our email also note that we are using port 20 here.

Bonus — You can also send email to multiple email ids using for loop which iterates over the list that contains all the email addresses.

I hope this article was helpful for programmers out there who are not aware of Python protocols and modules which helps in automation and makes our tasks easier. Keep learning and keep growing. Best of luck in your programming journey!




Continue Learning