Hi! In this post I’m gonna show you guys how to send emails using Python! In this post I’m using Gmail, so make sure you’ve set up a Google account to follow through this tutorial.

This post is based on a tutorial I made on YouTube

Creating a bot email account

First, we’ll have to create a new bot account through which our bot can send the email and I recommend you to not use your personal account for this. Creating a new Google account is pretty straight-forward so I’m not gonna go over that. Before we proceed though, we need to enable a setting in our Google account to make this whole thing work.

  • Log into your bot account
  • Turn on Less secure app access
    We do this because the Python module which we’re gonna use to send the email is not verified, and Google won’t let such apps access you’re Google account. Now we’re all set for coding!

The Modules we need

There are some modules we can use to help us achieve our goal. All of these modules are built-in with your Python installation and you don’t need to install anything separately with pip.

  • smtplib: smtplib allows us to connect to SMTP (Simple Mail Transfer Protocol) servers. Email providers like Gmail, Outlook and Yahoo uses SMTP.
  • email: This module contains a class called EmailMessage which will help us compose our email by configuring the subject, body etc.

Composing the email

First let’s import all the stuff mentioned in the previous section.

import smtplib
from email.message import EmailMessage

Now we’re gonna instantiate the EmailMessage class and create the structure of our email.

email = EmailMessage()

email['from'] = 'Email Bot' # Name of the sender
email['to'] = 'youraccount@gmail.com' # Email address of the receiver
email['subject'] = "Automated Message sent with Python" # Subject of the email

# Body of the email
email.set_content("This is an automated email sent using Python!")

Sending the email

# If you're using outlook replace the host with smtp.outlook.com
# The port 587 is the de-facto for emails
with smptlib.SMTP(host="smtp.gmail.com", port=587) as smtp:
    smtp.ehlo() # ehlo (Extended HELO) is a command sent by an email server to identify itself when connecting to another email server to start the process of sending an email. 
    smtp.starttls() # TLS (Transport Layer Security) encrypts data sent over the Internet for security 
    smtp.login("ebot1042@gmail.com", 'password') # Use your bot's email credentials to login
    smtp.send_message(email) # This finally sends our email using our email variable as the message

Final Code

import smtplib
from email.message import EmailMessage

email = EmailMessage()

email['from'] = 'Name of sender\s account'
email['to'] = 'receiver@email.com'
email['subject'] = 'Automated Message sent with Python'

email.set_content('This is an automated email sent using python!!')

with smtplib.SMTP(host="smtp.gmail.com", port=587) as smtp:
	smtp.ehlo()
	smtp.starttls()
	smtp.login('sender@gmail.com', 'senderspassword')
	smtp.send_message(email)

Adding attachments

We can also add attachments to our e-mails with an extra line of code.

email.add_attachment(open("attachment.txt", "r").read(), filename="attachment.txt")

Here we are opening our attached file in read mode because we’re not making any changes to its contents. And the filename is set to our attachment file’s name. That’s basically it!

NOTE: You have to set the content of the email before adding the file attachment code!

Thanks for reading, I’ll catch you guys in my next post!