Send email using python | 11 line code only

Hello friends how are you, Today in this post "Send email using python" i am going to teach you how you can send Email using Gmail in Python using just 11 lines of code. If you are a computer science students or teacher and want to learn something interesting in programming then just go through this post.

Here to make this program or project i will  use smtplib [Simple mail transfer protocol library] library to send email using Python. 

Now  i am going to explain it step by step so just go through this post to understand this completely.

If you want to understand this through video then watch this video i have explained it step by step live

Step 1: Install Python : Click here to watch a video on how to install python or Open any browser and type Download Python and click the first link you will get official website of python here you wi ll get a Download button and after clicking on this button you will get exe of latest python version just install it into your system. 

Step 2: Install Pycharm | Create Project | Install LibraryClick here to watch a single video on How to install Pycharm | Create Project | Install Library  or To install Pycharm IDE Open any browser and type Download Pycharm and click the first link you will get official website of Pycharm  here you will get a black download button and after clicking on this button you will get an exe of Pycharm , just install it into your system. If you are facing problem to install then watch the above video i have explained step by step.

Step 3: Create Project : Open Pycharm  click on File which is in top left of screen and select New Project. Then you will get a screen. Here first you need to select directory of your project and then type a name for your project like "SendEmail" after directory name, and at last click the create button to create this project. 

Step 4: Create Python file: To create a python file for coding in your project just right click on project name "SendEmail" and select new and click on Python File , you will get popup screen in which you have to type a name like "program" for python file and press enter, it will create a python file with name program.py inside the project Send Email. 

Step 5: Install library (smtplib) : Before doing code for this we have to first, install the library which is used to send Email using Python. To install this library just click on Terminal which will be in Left bottom of screen , you will get a console screen and in this console screen you have to type pip install smtplib and then press enter. After some seconds you will get a message like library is installed successfully. Still facing problem then watch the above video, i have explained everything.

Step 6: Write Python code:  Here is the complete code for this project/program you just need to type this code into your python file or you can copy this code for your personal use.

#import library
import  smtplib
#setting sender email
sender_email="type sender email here"
#setting receiver email
receiver_email="type receiver email here"
#setting password of sender
password="type password of sender email"
#setting message
message="Hello friend This is a Test Message"
#server configuration
server=smtplib.SMTP("smtp.gmail.com",587)
#starting TLS
server.starttls()
#Server Login
server.login(sender_email,password)
print("Login success")
#sending email
server.sendmail(sender_email,receiver_email,message)
print("Email has been sent successfully to ",receiver_email)

Here just copy this code and paste it into program.py file. 

Step 7: Run Code: To run this code just right click on the coding area and click on Run file in Python Console and you will get a message that Email has been sent successfully. But in most of the case you will get an error like below.

Send email using python

It is not the error in code actually you will get an error like this if Less secure app access  
is OFF in your Gmail account setting. So to remove this error login to your email account and go to Manage your google account then click on  security  and then you will get a screen where you will get an option Turn on access under heading Less secure app access, just Turn on it and run the code again, Now you will get  a success message. 

I hope now you can create  "Send email using python". If you have any doubt regarding this post  or you want something more in this post then let me know by comment below i will work on it definitely.
 

Request:-If you found this post helpful then let me know by your comment and share it with your friend. 
 
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

If you like my post then share it with your friends. Thanks­čśŐHappy Coding.

Post a Comment

0 Comments