Send Email using SMTP in python
In this tutorial, we gonna show you how you can send an email using the Simple Mail Transfer Protocol(SMTP) in python.
How to send email in Python using SMTP
Before jumping to the code you should know what is SMTP protocol. If you know already that’s great. Here is a short definition of SMTP.
SMTP – Simple mail transfer protocol
SMTP is a transport layer protocol. It provides the facility to send the e-mail and routing e-mail between the mail server.
In python, there is a package smtplib which has SMTP client object that can be used to send the mail.
now we have to import smtplib package:
Import smtplib package in Python
now we have to follow the following steps to send the mail in Python:
- Establish SMTP connection using smtp.gmail.com as SMTP server.
- use port no. 587.
- Identify client with ehlo() method.
- start transport layer security to encode the message.
- use the login() method by Gmail credential.
- call sendmail() method.
- you have to go on your browser and type :
and click OFF .so it will be ON.
Server setting for gmail in Python smtplib
identify the computer system and transport layer security:
user email and password:
username='[email protected]' password='your password'
now pass username and password to login function and send mail:
mail.login(username,password) mail.sendmail(username,'[email protected]','Subject:Email \n\n Hello this is an email ') mail.quit()
Complete Python program:
Example Python program to send gmail
import smtplib mail=smtplib.SMTP('smtp.gmail.com',587) mail.ehlo() mail.starttls() username='[email protected]' password='your password' mail.login(username,password) mail.sendmail(username,'[email protected]','Subject:Email \n\n Hello this is an email ') mail.quit() print("successfully sent email please check your gmail.............")
successfully sent email, please check your gmail...........
open the email and check you will get a message:
Hello this is an email
If not please check again and follow instructions correctly it will definitely work.
- Send SMS using Python msg91-sms PyPI package
- Voice Command Calculator in Python using speech recognition and PyAudio