Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Python HowTo – Sending Emails using Python

Python Send Email

Hello everyone! In today’s article, we’ll be taking a look at how we can use Python to send emails.

Sending Emails – A brief overview

Usually, the task of sending emails is done using the MTP (Mail Transfer Protocol). In the present day, there is a separate protocol called SMTP (Simple Mail Transfer Protocol) which is the widely used protocol for sending emails.

This protocol works on a client-server basis, similar to any other. When we want to send an email to a target machine, we (the client) need to send the mail contents to the SMTP Server. The server will now route it to the desired target machine.

So, in order to send an email, you’ll need to use a SMTP Server. While you may have a custom SMTP Server, we’ll be using Gmail’s free SMTP Server to send emails using Gmail!


Prerequisite Setup for Sending Emails with Python

Before going through the rest of this tutorial, I’d advice you to set up a dummy gmail account that you can use to test sending emails.

After setting up the account, there’s one more thing you need to do.

By default, your Gmail account is not configured to allow access from less secure applications such as SMTP. We need to enable this access for our account.

You can go to your gmail account configuration page and enable access from your Google account.

Less Secure Access Gmail
Less Secure Access Gmail

Now, you’re ready to send emails using Python! Let’s move on.


Send Emails using Python SMTP

Python has an SMTP client library (smtplib), which it will use to send emails to an SMTP server (Gmail).

This is a part of the standard library, so you can directly import it!

import smtplib

Okay, so now let’s try writing a script to send a test email.

Any email using SMTP must have the following contents:

  • The Sender address
  • The receiver address
  • A subject (Optional)
  • The body of the mail

Let’s write all of them down.

import smtplib

sender_address = "sender@gmail.com" # Replace this with your Gmail address

receiver_address = "receiver@gmail.com" # Replace this with any valid email address

account_password = "xxxxxxxxxx" # Replace this with your Gmail account password

subject = "Test Email using Python"

body = "Hello from AskPython!\n\nHappy to hear from you!\nWith regards,\n\tDeveloper"

# Endpoint for the SMTP Gmail server (Don't change this!)
smtp_server = smtplib.SMTP_SSL("smtp.gmail.com", 465)

# Login with your Gmail account using SMTP
smtp_server.login(sender_address, account_password)

# Let's combine the subject and the body onto a single message
message = f"Subject: {subject}\n\n{body}"

# We'll be sending this message in the above format (Subject:...\n\nBody)
smtp_server.sendmail(sender_address, receiver_address, message)

# Close our endpoint
smtp_server.close()

Make sure you replace the sender_address, receiver_address and account_password with your Gmail account information!

What we’re doing is that we use the SMTP Server to access our Gmail account, using a Secure SMTP (SMTP_SSL). After we login, we can send the message to the receiver directly, using smtp_server.sendmail()!

Now, if you enter the same account for the sender and receiver, you’ll get an email similar to mine.

Python Test Email
Python Test Email

Let’s check the contents.

Python Test Email Result
Python Test Email Result

Indeed, we’ve just sent a proper email using Python!

You can improve the code, to ensure that the resources are always closed, using context managers.

import smtplib

sender_address = "sender@gmail.com" # Replace this with your Gmail address

receiver_address = "receiver@gmail.com" # Replace this with any valid email address

account_password = "xxxxxxxxxx" # Replace this with your Gmail account password

subject = "Test Email using Python"

body = "Hello from AskPython!\n\nHappy to hear from you!\nWith regards,\n\tDeveloper"

# We can use a context manager
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as smtp_server:
    # Login with your Gmail account using SMTP
    smtp_server.login(sender_address, account_password)

    # Let's combine the subject and the body onto a single message
    message = f"Subject: {subject}\n\n{body}"

    # We'll be sending this message in the above format (Subject:...\n\nBody)
    smtp_server.sendmail(sender_address, receiver_address, message)

This will give the same results as before – another email!


Conclusion

In this article, we looked at how we could use Python to send emails easily, using gmail’s SMTP server.

References