DEV Community

Subham
Subham

Posted on

How MIME and SMTP work together to send emails over the internet πŸ“§

Have you ever wondered how emails are sent and received over the internet? How can you attach images, videos, or documents to your messages? How do different email clients communicate with each other? In this article, we will explore the answers to these questions by learning about two important protocols: MIME and SMTP.

What is MIME? πŸ€”

MIME stands for Multipurpose Internet Mail Extensions. It is a standard that defines how to format and encode different types of data for email transmission. MIME allows you to send not only plain text messages, but also rich text, HTML, images, audio, video, and other binary data as attachments. MIME also supports multiple languages and character sets, so you can send emails in any language you want.

MIME works by adding extra headers to the email message that describe the content type, encoding, and boundary of each part of the message. For example, a simple MIME message with a plain text part and an image attachment might look like this:

From: alice@example.com
To: bob@example.com
Subject: Hello
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="abc123"

--abc123
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit

Hi Bob,

This is a test message with an image attachment.

Alice

--abc123
Content-Type: image/jpeg; name="cat.jpg"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="cat.jpg"

/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRof
Hh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwh
MjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAK
AAoDASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAf/xAAUEAEAAAAAAAAAAAAAAAAAAAAA
/8QAFQEBAQAAAAAAAAAAAAAAAAAABgj/xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwC
f8P/AP/Z

--abc123--
Enter fullscreen mode Exit fullscreen mode

The --abc123 is the boundary that separates each part of the message. The first part has a Content-Type of text/plain and a Content-Transfer-Encoding of 7bit, which means it is a plain text message encoded in ASCII. The second part has a Content-Type of image/jpeg and a Content-Transfer-Encoding of base64, which means it is an image file encoded in base64. The Content-Disposition header specifies that this part is an attachment with a filename of cat.jpg.

What is SMTP? πŸ€”

SMTP stands for Simple Mail Transfer Protocol. It is a protocol that defines how to send and receive email messages over the internet. SMTP uses a client-server model, where the client (the sender) connects to the server (the receiver) and exchanges commands and responses to transfer the message.

SMTP works by following these steps:

  1. The sender's email client connects to the sender's SMTP server and initiates the session with a HELO or EHLO command.
  2. The sender's SMTP server responds with a greeting message and a list of supported extensions (such as MIME).
  3. The sender's email client sends the MAIL FROM command with the sender's email address.
  4. The sender's SMTP server responds with an OK message if the sender is valid.
  5. The sender's email client sends the RCPT TO command with the recipient's email address.
  6. The sender's SMTP server responds with an OK message if the recipient is valid.
  7. The sender's email client sends the DATA command to indicate that it is ready to send the message body.
  8. The sender's SMTP server responds with a 354 message to indicate that it is ready to receive the data.
  9. The sender's email client sends the message body (including the MIME headers and parts) followed by a . on a line by itself.
  10. The sender's SMTP server responds with a 250 message to indicate that it has received and stored the message.
  11. The sender's email client sends the QUIT command to terminate the session.
  12. The sender's SMTP server responds with a 221 message to confirm the termination.

Here is an example of an SMTP session between Alice's email client and her SMTP server:

Alice's email client: HELO alice.example.com
Alice's SMTP server: 250 bob.example.com Hello alice.example.com
Alice's email client: MAIL FROM:<alice@example.com>
Alice's SMTP server: 250 OK
Alice's email client: RCPT TO:<bob@example.com>
Alice's SMTP server: 250 OK
Alice's email client: DATA
Alice's SMTP server: 354 Start mail input; end with <CRLF>.<CRLF>
Alice's email client: From: alice@example.com
Alice's email client: To: bob@example.com
Alice's email client: Subject: Hello
Alice's email client: MIME-Version: 1.0
Alice's email client: Content-Type: multipart/mixed; boundary="abc123"
Alice's email client:
Alice's email client: --abc123
Alice's email client: Content-Type: text/plain; charset="utf-8"
Alice's email client: Content-Transfer-Encoding: 7bit
Alice's email client:
Alice's email client: Hi Bob,
Alice's email client:
Alice's email client: This is a test message with an image attachment.
Alice's email client:
Alice's email client: Alice
Alice's email client:
Alice's email client: --abc123
Alice's email client: Content-Type: image/jpeg; name="cat.jpg"
Alice's email client: Content-Transfer-Encoding: base64
Alice's email client: Content-Disposition: attachment; filename="cat.jpg"
Alice's email client:
Alice's email client: /9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRof
Alice's email client: Hh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwh
Alice's email client: MjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAK
Alice's email client: AAoDASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAf/xAAUEAEAAAAAAAAAAAAAAAAAAAAA
Alice's email client: /8QAFQEBAQAAAAAAAAAAAAAAAAAABgj/xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwC
Alice's email client: f8P/AP/Z
Alice's email client:
Alice's email client: --abc123--
Alice's email client:
Alice's email client: .
Alice's SMTP server: 250 OK : queued as 1234567890abcdef
Alice's email client: QUIT
Alice's SMTP server: 221 Bye
Enter fullscreen mode Exit fullscreen mode

How do MIME and SMTP work together? πŸ€”

As you can see from the examples above, MIME and SMTP work together to send emails over the internet. MIME defines how to format and encode different types of data for email transmission, while SMTP defines how to send and receive those messages between different servers.

When you send an email with attachments or rich content, your email client uses MIME to create a multipart message with different headers and parts for each type of data. Then, your email client uses SMTP to connect to your SMTP server and transfer that message to the recipient's SMTP server. The recipient's SMTP server then stores or forwards that message to the recipient's inbox, where their email client can use MIME to decode and display the message.

Conclusion πŸŽ‰

In this article, we learned about two important protocols that enable us to send emails over the internet:

  • MIME (Multipurpose Internet Mail Extensions) defines how to format and encode different types of data for email transmission.
  • SMTP (Simple Mail Transfer Protocol) defines how to send and receive those messages between different servers.

We also saw some examples of how MIME messages look like and how SMTP sessions work.

We hope you enjoyed this article and learned something new! If you have any questions or feedback, please let us know in the comments below! 😊

Top comments (0)