How to Use Python Script to Send Emails Using Google Account

How to Use Python Script to Send Emails Using Google Account

Nowadays Python is one of the most popular programming language for software development in the world. It is the fully object-oriented and open source programming language that is used by all the top companies like Google, Microsoft, Amazon and many more for software development. It is very easy to learn and as a beginner, anyone can learn it in a very short period.


Today in this article I will teach you how to send a mail using Python 3 in a very easy way. If you are new to programming then you can also learn it using this article step wise step in a very easy way.

Read this article until the end and do not skip any line for a more good understanding of How to send email using python?

Before we start actual coding to send emails using python we have understood some small terms that are useful in sending emails using python.


What is SMTP?

SMTP means Simple Mail Transfer Protocol. Simple Mail Transfer Protocol is a protocol, which handles sending emails and routing emails between mail servers.
A simple syntax to import SMTPLIB and create an SMTP object.

#importing smtp library
import smtplib


#s = smtplib.SMTP("host", "port")


s = smtplib.SMTP("smtp.gmail.com", 587)


Host: This is the host running your SMTP server. Some of the popular SMTP host server names.
Gmail smtp server name: smtp.gmail.com
Yahoo! smtp server name: smtp.mail.yahoo.com
Hotmail smtp server name: smtp.live.com

Port: If you provide a host argument, then you need to give a port, where SMTP server is listing. Mainly the port would be 25.

SMTP object has a method called sendmail, which is mainly used to send mail. It takes three parameters:
sender: A string of the email address of the sender
receiver: A string of the email address of the receiver
message: A message as a string

Example

#python code to send email using google account
#importing smtp library
import smtplib

#s = smtplib.SMTP("host", "port")
#create smtp object
s = smtplib.SMTP("smtp.gmail.com", 587)

#Start TLS for security
s.starttls()

#Authentication
s.login("sender_email_address", "Email Password")

#Message to be send
message = "Hello Dsguider"

#Sending the mail
s.sendmail("sender_email_address", "Reciever's Address", message)

#Terminating the session
s.quit()


Sending the same message to multiple peoples

#python code to send email using google account
#importing smtp library
import smtplib

re = ['xxxx@gmail.com', 'xxxxx@gmail.com']

#s = smtplib.SMTP("host", "port")
#create smtp object
s = smtplib.SMTP("smtp.gmail.com", 587)

#Start TLS for security
s.starttls()

#Authentication
s.login("sender_email_address", "Email Password")

#Message to be send
message = "Hello Dsguider"

for i in range(len(re)):
#Sending the mail
s.sendmail("sender_email_address", re[i], message)

#Terminating the session
s.quit()


Example to send HTML Content as an Email

#python code to send email using google account
#importing smtp library
import smtplib

re = ['xxxx@gmail.com', 'xxxxx@gmail.com']

#s = smtplib.SMTP("host", "port")
#create smtp object
s = smtplib.SMTP("smtp.gmail.com", 587)

#Start TLS for security
s.starttls()

#Authentication
s.login("sender_email_address", "Email Password")

#Message to be send
message = """From: From Person <from@gmail.com>
To: To Person <to@gmail.com>
MIME-Version: 1.0
Content-type: text/html
Subject: SMTP HTML e-mail test

This is an e-mail message to be sent in HTML format

<b>This is HTML message.</b>
<h1>This is headline.</h1>
"""

for i in range(len(re)):
#Sending the mail
s.sendmail("sender_email_address", re[i], message)

#Terminating the session
s.quit()


Post a Comment (0)
Previous Post Next Post