Join us

Sending HTML email in Python

In most cases, you need to add some formatting, links, or images to your email notifications. We can simply put all of these with the HTML content. For this purpose, Python has an email package.

We will deal with the MIME message type, which is able to combine HTML and plain text. In Python, it is handled by the email.mime module.

It is better to write a text version and an HTML version separately, and then merge them with the MIMEMultipart(“alternative”) instance. It means that such a message has two rendering options accordingly. In case an HTML isn’t be rendered successfully for some reason, a text version will still be available.

Input:

Output:

Sending emails with attachments in Python

The next step in mastering sending emails with Python is attaching files. Attachments are still the MIME objects but we need to encode them with the base64 module. A couple of important points about the attachments:

  1. Python lets you attach text files, images, audio files, and even applications. You just need to use the appropriate email class like email.mime.audio.MIMEAudio or email.mime.image.MIMEImage. For the full information, refer to this section of the Python documentation. Also, you can check the examples provided by Python for a better understanding.
  2. Remember about the file size: sending files over 20MB is a bad practice.

In transactional emails, the PDF files are the most frequently used: we usually get receipts, tickets, boarding passes, orders confirmations, etc. So let’s review how to send a boarding pass as a PDF file.

Input:

To attach several files, you can call the message.attach() method several times.

How to send an email with images

Images, even if they are a part of the message body, are attachments as well. There are three types of them: CID attachments (embedded as a MIME object), base64 images (inline embedding), and linked images. We have described their peculiarities, pros and cons, and compatibility with most email clients in this post.

Let’s jump to examples.

For adding a CID attachment, we will create a MIME multipart message with MIMEImage component:

Output:

The CID image is shown both as a part of the HTML message and as an attachment. Messages with this image type are often considered spam: check the Analytics tab in Mailtrap to see the spam rate and recommendations on its improvement. Many email clients  – Gmail in particular – don’t display CID images in most cases. So let’s review how to embed a base64 encoded image.

Here we will use base64 module and experiment with the same image file:

Output:

Now the image is embedded into the HTML message and is not available as attached file. Python has encoded our jpg image, and if we go to the HTML Source tab, we will see the long image data string in the img src.

To learn about sending to multiple recipients, using SMTP and Gmail head to the comprehensive guide on sending emails in Python by Mailtrap.


Only registered users can post comments. Please, login or signup.

Start blogging about your favorite technologies, reach more readers and earn rewards!

Join other developers and claim your FAUN account now!

User Popularity
807

Influence

81k

Total Hits

49

Posts