How to Send an Email with Python

Sending Emails Using Python
Valeriia Dziubenko Valeriia Dziubenko 05 march 2024, 16:04 46
For beginners

Email notifications are essential for every business with an online presence. Over 4 billion active email users worldwide make it the best medium to reach customers. You can remind customers about important upcoming events, verify password changes, notify of login attempts, etc. Businesses use email to inform their customers of upcoming discounts and promotions to get higher sales.

Apart from sending emails manually, you may use programming languages like Python. This article will show how to implement the latter option. We’ll explain how sending emails via Python works and give you some good examples.

What email sending options do you have with Python?

The Simple Mail Transfer Protocol (SMTP) powers the massive flow of information exchange between email servers worldwide. Python supports the use of SMTP with a built-in smtplib module as part of its standard library. This module makes it easy to send emails via SMTP connections; it implements the standard RFC 821 (SMTP) and RFC 1869 (ESMTP) specifications and needs no extra installations to work.

You can use the smtplib module to automate sending transactional and bulk emails. It does not limit you to plain-text emails: you may also compose HTML messages and add attachments such as photos, PDF documents, etc.

Send emails via a third-party email service provider using API

From a programmer’s viewpoint, there’s hardly any difference between using your own SMTP server or the one provided by another party, be it your company’s IT department or an external service. However, operating your own SMTP servers makes you responsible for maintaining and securing them, which can be cumbersome and disruptive to your core business. Instead, you can choose an external email service provider (ESP) like UniOne to handle the task and make your life easier.

It should be noted that a good ESP usually offers two different ways to send emails. The most basic one is an SMTP API, which can be accessed using the same smtplib module mentioned above. The only difference is that you’ll need to specify the external server’s hostname and port instead of your own.

The second option is to use a specialized application programming interface, or API. This method does not rely on smtplib; instead, it uses the HTTP protocol to send commands to the ESP’s server. This method offers numerous advantages over the first one, but requires more complex programming.

In this article, we’ll show you the most essential examples using the smtplib module and Python on Windows. The same code will also work on Linux machines, but may require some fixes. If you need information about using the HTTP API, refer to the ESP’s documentation.

A step-by-step guide to sending emails with Python

How to send emails using a local SMTP server

A local SMTP server is a server that resides on your system. Typically you don't use it to send actual emails, but it will come handy if you need to check whether your Python script works correctly. You may install any popular server that is available for your OS. However, the easiest approach will be using an smtpd module that comes bundled with Python on Windows. Actually, it does not send your emails anywhere; it just outputs the message to your console, which is all you need for debugging.

To start smtpd, enter the following command at your command prompt:

python -m smtpd -c DebuggingServer -n localhost:2501

Now you have an instance of smtpd listening on port 2501. Let’s proceed with actual coding. Below is the most basic Python script to send an email from a local SMTP server:

import smtplib

sender = 'from@example.com'
receivers = ['to@example.com']
message = """From: Sender <from@example.com>
To: Addressee <to@example.com>
Subject: Test message #1


This is a test message for this UniOne tutorial on sending emails with Python.
"""

try:
    smtpObj = smtplib.SMTP('localhost', 2501)
    smtpObj.setdebuglevel(1)
    smtpObj.sendmail(sender, receivers, message)
    smtpObj.quit()      
    print("Successfully sent an email")
except SMTPException:
    print("Something has gone wrong")

Let’s take a closer look at the code. The first step is to import the smtplib module:

 

import smtplib

 

To send emails, we create an SMTP object:

 

smtpObj = smtplib.SMTP([host [, port]])

 

Provide the correct values for the above parameters:

  • host - This part refers to the hostname of your SMTP server. Use localhost as a host name if the server runs on your local machine.
  • port - Specifies the port number to use. It will be your local SMTP port number, or the one you’ve specified on the smtpd command line.

Now we have an open SMTP connection to the server, and may proceed with sending a simple email message. For this, the SMTP object has an instance method called sendmail(). This method requires three parameters:

  • sender − a string with the sender’s From address
  • receivers − a list of strings, one for each recipient’s address
  • message − a message formatted as specified in RFCs (note the two empty lines separating the headers section from the body)

Before calling this method, we enable the debugging mode for our SMTP object to see what’s happening when the script is run.

The next line checks whether an error has occurred while connecting to the server or sending a message. SMTPException is the base exception class for smtplib; for more sophisticated error processing, consult the module’s documentation.

Using an external SMTP server

The above example assumes you’re running an SMTP server locally on your PC. If not, you’ll need to modify the script accordingly, as described below.

When sending emails via SMTP, you’ll also want to encrypt the connection. Unencrypted connections can be intercepted by hackers looking to steal sensitive information. Encrypting prevents this problem.

For an encrypted connection, you’ll need this code to create an SMTP object:

smtpObj = smtpObj.SMTP_SSL(host, port)

This object is identical to smtplib.SMTP, but uses an implicit SSL connection to the server. For the host parameter, you specify your server's IP address or domain name. The port value defaults to 465, the standard for SMTP over SSL. For more information on secure connections, see our blog article.

When connecting to an external server, you’ll also need to properly introduce yourself. For this, the following method call is used:

smtpObj.login(username, password)

In the examples below, we’ll use one of the SMTP servers provided by UniOne. Connecting to our SMTP server is simple; you just need to use the proper connection parameters:

  • host: either smtp.us1.unione.io or smtp.eu1.unione.io (for accounts registered at us1.unione.io and eu1.unione.io, respectively).
  • port: 25, 465 or 587. You can use any of these ports; all our connections are encrypted by default, so you don't need to worry about that.
  • username: your account’s user_id or project_id (you can look up these parameters in your UniOne dashboard).
  • password: your account's API key or project_api_key.
  • encoding: use UTF-8.

For more things to note when using UniOne, see info on our SMTP API.

This is how the code will finally look like:

import smtplib

port = 465 
smtp_server = "smtp.us1.unione.io"
login = "123456789" # your UniOne user_id or project_id
password = "*******" # your UniOne API key or project_api_key

sender_email = "UniOne@example.com"
receiver_email = "recipient@example.com"
message = """From: Sender <from@example.com>
To: Addressee <to@example.com>
Subject: Test message #2


This is a test message sent from UniOne with Python using a secure connection.
"""

try:
    smtpObj = smtplib.SMTP_SSL(smtp_server, port)
    smtpObj.login(username, password)
    smtpObj.sendmail(sender, receivers, message) 
    smtpObj.quit()        
    print("Successfully sent an email")
except SMTPException:
    print("Something has gone wrong")

How to send HTML emails with Python

In the next example, we'll use the MIME message type that combines HTML/CSS and plain text. In Python, MIME messages are handled by the email.mime module.

It's advisable to write separate text and HTML versions of your email and merge them with the MIMEMultipart("alternative") Python object instance. This approach means that an email client can render your message in either HTML or text.

# Import the necessary components
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

port = 587 
smtp_server = "smtp.us1.unione.io"
login = "123456789" # your UniOne user_id or project_id
password = "*******" # your UniOne API key or project_api_key

sender_email = "UniOne@example.com"
receiver_email = "recipient@example.com"
message = MIMEMultipart("alternative")
message["Subject"] = "Multipart email test"
message["From"] = sender_email
message["To"] = receiver_email

# the plain text part
text = """\
Hi,
Check out our new post on the UniOne blog about sending emails with Python:
We hope you learn a lot from it"""

# the HTML part
html = """\
<html>
  <body>
    <p>Hi,<br>
      Check out our new post on the UniOne blog about sending emails with Python.</p>
    <p>We hope you learn a lot from it.</p>
  </body>
</html>
"""

# convert both parts to MIMEText objects and add them to the MIMEMultipart message
part1 = MIMEText(text, "plain")
part2 = MIMEText(html, "html")
message.attach(part1)
message.attach(part2)

# send your email
with smtplib.SMTP_SSL(smtp_server, port) as server:
    server.login(login, password)
    server.sendmail(
        sender_email, receiver_email, message.as_string()
    )

print('Message sent')

How to send emails with attachments

The next thing to learn is how to send emails with attachments using Python.

Python lets you attach text files, documents, images, videos, audio, etc. You just need to use the proper email class, such as email.mime.image.MIMEImage or email.mime.text.MIMEtext.

Keep in mind that the maximum total size for a UniOne email, including text and all attachments, is 10 MB.

import smtplib

# import the required modules
from email import encoders
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

port = 587 
smtp_server = "smtp.us1.unione.io"
login = "123456789" # your UniOne user_id
password = "*******" # your UniOne API key or project_api_key

subject = "How to send emails using Python"
sender_email = "UniOne@example.com"
receiver_email = "recipient@example.com"

message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject

# Add body to email
body = "This is a tutorial for sending attachments via Python"
message.attach(MIMEText(body, "plain"))

# Open PDF file in binary mode
filename = "PythonTutorial.pdf"
with open(filename, "rb") as attachment:
    # Create an attachment from your PDF file
    part = MIMEApplication(attachment.read())
    attachment.close()

# Add attachment to your message and convert it to string
part['Content-Disposition'] = 'attachment; filename="%s"' % filename
message.attach(part)
text = message.as_string()

# send your email
with smtplib.SMTP_SSL(smtp_server, port) as server:
    server.login(login, password)
    server.sendmail(
        sender_email, receiver_email, text
    )
print('Sent')

Sending emails to multiple recipients with Python

You can send emails to multiple recipients using Python code via the UniOne API. As mentioned earlier, each recipient will receive separate email copies that will count toward your subscription.

Adding more recipients is simple; just type their address separated by commas and add CC and BCC. However, this method can be stressful if you're sending messages to thousands of addresses (you likely can’t type them all). The alternative is to place all the addresses in a CSV file and point the SMTP server to that database to fetch all them.

Input:

import csv, smtplib

port = 587 
smtp_server = "smtp.us1.unione.io"
login = "123456789" # your UniOne user_id
password = "*******" # your UniOne API key or project_api_key
sender = "UniOne@example.com"
message = """Subject: Read our tutorial for sending emails via Python
To: {recipient}
From: {sender}

Hi {name}, we have written a detailed tutorial for sending emails via Python"""

with smtplib.SMTP_SSL(smtp_server, port) as server:
    server.login(login, password)
    with open("contacts.csv") as file:
        reader = csv.reader(file)
        next(reader)  # skip the header row
        for name, email in reader:
            server.sendmail(
                sender,
                email,
                message.format(name=name, recipient=email, sender=sender)
            )
            print(f'Sent to {name}')

For the above example, we have created a CSV file named “contacts.csv” with all the recipients’ names and addresses (put this file in the same folder as your Python script). This program instructs the server to read the file, fetch the email addresses, and send the email to each one.

Sending emails with images

You can send images by adding them as attachments to your emails with Python code. The best option is to add the image as a CID attachment (embedded it as a MIME object).

Input:

# import all necessary components
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart

port = 587 
smtp_server = "smtp.us1.unione.io"
login = "123456789" # your UniOne user_id
password = "*******" # your UniOne API key or project_api_key

sender_email = "UniOne@example.com"
receiver_email = "recipient@example.com"
message = MIMEMultipart("alternative")
message["Subject"] = "CID image testing"
message["From"] = sender_email
message["To"] = receiver_email

# write the HTML part
html = """\
<html>
 <body>
   <img src="cid:UniOneImage">
 </body>
</html>
"""

part = MIMEText(html, "html")
message.attach(part)

# This code assumes that you saved the image file in the same directory as your Python script
fp = open('UniOneImage.jpg', 'rb')
image = MIMEImage(fp.read())
fp.close()

# Specify the ID according to the img src in the HTML part
image.add_header('Content-ID', '<UniOneImage>')
message.attach(image)

# send your email
with smtplib.SMTP_SSL(smtp_server, port) as server:
    server.login(login, password)
    server.sendmail(
        sender_email, receiver_email, message.as_string()
    )
print('Sent OK')

The above code illustrates attaching an image named “UniOneImage.jpg” that you’ve already saved in the same directory as your Python script. The program instructs the SMTP server to fetch and send the image to the recipients.

Conclusion

We have explained how to write Python scripts to send plain text emails, emails having attachments, and emails with multiple recipients. By following our tips, you can send messages easily via the UniOne SMTP API.

UniOne is a secure and reliable email service that guarantees high deliverability rates if you adhere to email regulations and best practices.

Related Articles

Blog
For beginners
How to Check My Email Sender Reputation? Tools and Tips
Behind every great campaign is a solid sender reputation. It affects whether your emails will reach the inbox or end up in spam.
Denys Romanov
01 november 2022, 10:038 min
Blog
For beginners
How to “Send on Behalf of” in Outlook?
Outlook is one of the most popular email solutions for businesses. One of its unique features is that a user can send an email on behalf of someone else.This feature enables effective organizational communication even when some staff members aren’t available.
Denys Romanov
26 december 2023, 10:426 min
Blog
For beginners
When is the Best Time to Send Your Email Newsletter?
Read our guide to find out when is the best time to send an email.
Valeriia Dziubenko
27 may 2022, 11:399 min