Hey fellas! If you know basics of Python and want to learn more, then this article is for you. In this article we will discuss on use of SMTP for sending emails with Python. SMTP stands for Simple Mail Transfer Protocol and you can read its definition from here. Here, we are going to show you how to simply send emails with Python code.
To send emails with Python, we use a module called smtplib. Basically, what you have to do is write a piece of code including the sending and receiving emails, the password for your email and the message. The necessary things to be considered to send emails with Python are listed below:
- host : The host for this purpose is “smtp.gmail.com”
- port : The port we are going to use is 587.
- sender :It is the email address of sender.
- receiver : It is basically the email of receiver.
username = “firstname.lastname@example.org”
password = “enter your password here”
host = “smtp.gmail.com”
port = 587
sender = username
receiver = “name of receiver @gmail.com”
message =”Yo man you are a genius. ”
emailfn = smtplib.SMTP(host, port)
emailfn.starttls() #This is for security layer.
emailfn.login(username, password) #This line is for login.
emailfn.sendmail(sender, receiver, message) #This function is for sending the mail.
emailfn.quit() #It is very important to quit and logout after our work is done.
All you have to do is enter your email in the username and the receiving email in receiver part. I have tried to make everything clear in the comment of the code. Fairly simple right? The best thing about Python is it’s code is so simple to read that you can guess what is going on just by looking at the code. It is also fun to learn Python as it is easy to understand.
If you have any queries on the article you can ask in the comment section. Also, if you have any suggestions or complaints on our contents we will be happy to see them in the comment section. If you want more articles on Python feel free to comment so that we can give you articles that will be useful. Also leave a like on our Facebook page.