Start a team blog
Start a team blog, invite your team, and start publishing.

Sending emails using simple Python script

Codes on how to send emails using Python

Dapo Adedire 's photo
Dapo Adedire
·Jan 6, 2022·

1 min read

Sending emails using simple Python script

Hello, ready to have fun? 🤓

There are more than one way to send email using Python. In this article, I will be sharing the code for one of the ways.

It is using the Python inbuilt smtplib module. The SMPT(Simple Mail Transfer Protocol) is used by mail servers to send and receive e-mail messages on the internet.

You might wish to create a new account for this fun project, but most importantly to enable this code to work with your email, login to your Google Account and go to My Account > Security > Less Secure App Access and Turn On.


import smtplib 

message = """From: Me <sender@gmail.com>
To:You <receiver@gmail.com>
MIME-Version: 1.0
Content-type: text/html
Subject: <b>Hey, Python is Cool</b>

Yes, it's cool cos I'm sending this email to you with the help Python. 
    ml = smtplib.SMTP('smtp.gmail.com',587) 
    ml.login('sender@gmail.com', 'password')  
    ml.sendmail('', 'receiver@gmail.com',message) 
    print("Email has been successfully sent")
    print("Email not sent")

Probably you to have much fun and bomb the receiver's mail box, try this:

while True: 

    ml.sendmail('receiver@gmail.com', 'message')

It's so cool that you can import datetime module and write some lines of code to send the email at a specific time.