How to Send Simple Emails in Python

It’s not exactly the hardest thing to get on a web browser or open an email app and send emails. Wouldn’t it be cool to be able to send emails programmatically though? In this post we’ll go over how to send a simple email with Python. At the end of this tutorial, you should be able to set up your own Python program to send simple emails to anyone you want. This tutorial specifically covers how to send emails from Gmail. Before we get started, you’ll need to head over to your Google account and set up two-factor authentication. To do that, follow the steps below.

Get your Email Authorization Key

  1. Go to “myaccount.google.com” and then click on the Security Tab
get your gmail auth token
  1. Once you’re in the Security Tab, scroll down until you see “2-Step Verification”
complete 2 step on gmail
2 step on gmail
  1. After you complete the steps for setting up 2-Step Verification, go to the “App Passwords” section underneath
set up app passwords on gmail
set up app passwords on gmail
  1. Create a custom app and enter whatever name you’d like, I picked Python
create custom app for the code
  1. Once you enter a name and click Generate, you’ll see a pop up screen like the one below. Copy the code you get (I crossed mine out) and store it somewhere you have access to.
copy password

Great, now that we’re done setting up 2-Step Verification and an app password, we’re ready to move on to the actual programming bit. The first step is to set up a config.py file where we’ll keep the configuration details for our email program. We’ll store our app password, user (Gmail address), host, and port. The host and port I have in the example are the universal ones for Gmail, they will always, or at least for the foreseeable future, be “smtp.gmail.com” and port 465.

gmail_pass = "<your app password here>"
user = "<your gmail here>"
host = "smtp.gmail.com"
port = 465

The Python Code to Create and Send Your Emails

Now let’s set up our actual email script. First let’s handle the imports. The first library we need is ‘smtplib’, this is the native Simple Mail Transfer Protocol (smtp) library for Python and doesn’t require any extra installation. We’ll also need the MIMEMultipart for building the message and MIMEText for writing the body. MIME stands for Multipurpose Internet Mail Extension. We’ll also need a Header library, this is for creating the “to”, “from”, and “subject” portions of your email. At the end, we’ll also import our config from our config file, mine is called email_config.py, but you should feel free to name yours just “config.py” or “configuration.py” or whatever you’d like as long as it’s understandable.

import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
 
from email_config import gmail_pass, user, host, port

Let’s build our send_email function. We’ll let our function take three parameters, “to”, “subject”, and “body”. These indicate the address we’re sending the email to, the subject of the email, and the body text. First we’ll craft our “message” or email object. Then we use the Header library and assign the From, To, and Subject values. After setting up our email header, we’ll attach the body as plain text, encoded in utf-8 format. Once we’ve set up our message, all that’s left to do is fire up an email server, login using our Gmail account and app password, send an email, and close the server.

def send_email(to, subject, body):
    # create message object
    message = MIMEMultipart()
 
    # add in header
    message['From'] = Header(user)
    message['To'] = Header(to)
    message['Subject'] = Header(subject)
 
    # attach message body as MIMEText
    message.attach(MIMEText(body, 'plain', 'utf-8'))
   
    # setup email server
    server = smtplib.SMTP_SSL(host, port)
    server.login(user, gmail_pass)
 
    # send email and quit server
    server.sendmail(user, to, message.as_string())
    server.quit()

That’s it, once you’ve done that, you can supply your own “to”, “subject”, and “body” parameters and get sending emails from the command line!

Further Reading

I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly
Yujian Tang

4 thoughts on “How to Send Simple Emails in Python

  1. This program isn’t working bro I am pasting the code here.

    def send_email(to, subject, body):
    # create message object
    message = MIMEMultipart()

    # add in header
    message[‘from’] = ‘from email’
    message[‘to’] = ‘recevier email’
    message[‘Subject’] = ‘Just test’

    # attach message body as MIMEText
    message.attach(MIMEText(body, ‘Let it be Successfull’, ‘utf-8’))

    # setup email server
    server = smtplib.SMTP_SSL(host, port)
    server.login(user, gmail_pass)

    # send email and quit server
    server.send_mail(user, to, message.as_string())
    server.quit()

    import smtplib
    from email.mime.text import MIMEText
    from email.header import Header
    from email.mime.multipart import MIMEMultipart

    from email_config import gmail_pass, user, host, port

Leave a Reply

%d bloggers like this: