Skip to main content
Practice

Email Message Format and MIME

Emails consist of more than just plain text. They can be formatted and sent in various ways.

This is where MIME plays a crucial role.

MIME stands for Multipurpose Internet Mail Extensions, a standard that allows the inclusion of different types of data in emails.

In this lesson, we'll explore the basic structure of an email message and how to use the MIME format to compose emails.


Structure of an Email Message

An email is primarily divided into two parts: Header and Body.


The header contains the basic information of the email such as the sender, recipient, and subject.

Key header elements include:

  • From : Sender's email address

  • To : Recipient's email address

  • Subject : Email subject

  • Date : Date and time the email was sent

  • Cc : Carbon copy email address

  • Bcc : Blind carbon copy email address


Body

The body contains the actual content of the email.

It can include text, images, links, HTML files, and more.

Typically, the body of an email is written in either text/plain (plain text) or text/html (HTML format).


Adding Multiple Formats to an Email with MIME

MIME allows for the inclusion of different types of data in an email.

Initially, emails supported only plain text, but thanks to MIME, we can now include images, audio, video files, and attachments in emails.


Key Components of MIME

  • Content-Type : Defines the type of the content in the email body or attachment, e.g., text/plain for plain text or text/html for HTML text.

  • Content-Transfer-Encoding : Specifies the encoding (encryption) method of the email content, usually base64 or quoted-printable.

  • Boundary : A delimiter used to separate different MIME parts. It's useful when the email body includes various formats.


Composing an Email Using MIME

Let's create a simple HTML email using MIME.

You can use HTML to create visually appealing emails as shown below.

Example of an Email Using MIME
# Import necessary libraries
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# Define recipient email
receiver_email = "youremail@example.com"

# Define sender email credentials
sender_email = "admin_email"
sender_password = "admin_password"
smtp_server = "smtp.gmail.com"
port = 587

# Set up the email structure
msg = MIMEMultipart('alternative')

# Set email headers
msg['Subject'] = "Hello, this is an example email."
msg['From'] = sender_email
msg['To'] = receiver_email

# Set HTML format for the email body
html = """
<html>
<body>
<h1>HTML Email</h1>
<p>
The email has been sent
<span style="color:blue;">successfully</span>!
Congratulations!
</p>
</body>
</html>
"""

# Attach the HTML content as a MIME part
html_email = MIMEText(html, 'html')
msg.attach(html_email)

# Connect to the SMTP server
server = smtplib.SMTP(smtp_server, port)

# Start TLS (email encryption)
server.starttls()

# Log in to the email account
server.login(sender_email, sender_password)

# Convert the email message to a string
text = msg.as_string()

# Send the email
server.sendmail(sender_email, receiver_email, text)

# Disconnect from the SMTP server
server.quit()

print("Email sent! Please check your inbox.")

The code above uses simple HTML and CSS to create an email body containing a header (h1) and a paragraph (p).

The span tag and CSS color attribute style certain words in blue, and the b tag is used for bold text.


Practice

In the code editor provided, make sure the receiver_email is correctly set or updated to test it, and execute the code to send the email.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.