In this lesson on python coding, we will learn how to send mail using Gmail SMTP?. To send email using Gmail smtp we required a Gmail account (email and password) if you don't have an account click here to sign up with google.
I will use smtplib a python library to send emails, to use it we need to install it through pip. Run the following command In your terminal to install this library
pip install smtplib
After installing this library, it is required to create a file anywhere on your computer with .py extension (ex - send_mail.py) and paste the following code in your file. After that change username and password in the code, you copied.
Python basic program to send email using Gmail smtp -
import smtplib fadd = '' # sender's email address tadd = '' # receiver's email address msg = 'Mail sent through Python!' # Message to be sent! username = '' # Your username(email ID) password = '' # Your password for above email ID server = smtplib.SMTP('smtp.gmail.com',587) server.ehlo() server.starttls() server.login(username,password) server.sendmail(fadd,tadd,msg)
After that save your file and open the terminal in the current directory. Type
python file_name.py (python send_mail.py in my case), this will send a mail to defined mail in fadd with from email defined tadd
I hope you like this python basic program to send mail using Gmail smtp and if you have any doubt regarding anything, please comment below.
Hello, I am Harendra Kumar Kanojiya - Owner of this website and a Fullstack web developer. I have expertise in full-stack web development using Angular, PHP, Node JS, Python, Laravel, Codeigniter and, Other web technologies. I also love to write blogs on the latest web technology to keep me and others updated. Thank you for reading the articles.