DEV Community

Ashish Nair
Ashish Nair

Posted on

Send Emails with Python : sending emails with just 6 lines of code

If you are looking for a way to send emails with python, look no more.

Lets jump into it,

Emails with Python

We'll use the Smtp module a built-in python library to send emails,

So lets start with importing it first,

import smtplib

Now that this is out of the way lets look at the code,

message = f'Subject : Upcoming Due Date Alert : {left} Days \nDear User, \n\n\nThis message is regarding the upcoming due date for {Details} on {Date}. \n\nRegards,\nPython ;)'
        server = smtplib.SMTP(host = 'smtp.gmail.com',port=587)
        server.ehlo()
        server.starttls()
        server.login(sender_mail,sender_password)
        server.sendmail(sender_mail,receiver_mail,alert_message)
        server.quit()

The server variable defines the email host which is 'gmail.com' and the port that we will be using to send the email. The server.ehlo() function is a hostname argument for the email client and the server.starttls() is to encrypt the email.

That is it, that's all it takes to send emails using python. Making sending bulk emails easy, all thanks to python. You can pass a list of receiver emails to the receiver_mail variable which would send the emails to multiple people.

Go and try send some emails with Python.

Top comments (0)