• Around The HomeToggle Menu

    • Entertainment
    • Productivity
    • Smart Home
  • FamilyToggle Menu

    • Parenting
    • Toys
    • Pets
    • Travel
  • Product ReviewsToggle Menu

    • Phones
    • Tablets
    • Laptops
    • Desktops
    • Wearables
    • Audio
    • Cameras
    • Headphones
    • Printers
    • Smart Home
    • TVs
    • Gaming and Video
  • One Cool ThingToggle Menu

    • Frugal Tech
    • Kickstarters
    • Videos
Techwalla
  1. Home
  2. Around The Home
  3. Productivity
  4. How to Decode an Email in MIME Format

How to Decode an Email in MIME Format

By: Techwalla Editor
  • Share
  • Share on Facebook

Most emails sent today are MIME (Multipupose Internet Mail Extensions) formatted. This allows emails to be sent with plain text and rich text/HTML versions, inline images, and attachments. MIME extensions can be added to a message in standard RFC/822 format so backward compatibility is achieved with older mail systems.

Step

Load the contents of the email message.

Step

Check the "Content-Type" header. If the content type is multipart (i.e. "multipart/mixed", "multipart/alternative", etc) the message will have multiple sections to parse. If the content type is multipart, continue with below steps.

Step

The Content-Type header should contain a unique string used by the code that generated the MIME message to designate MIME part boundaries. Example:

Content-Type: multipart/mixed; boundary="part_c7161025_fe8a_45f4_83ef_6befcfa5d021"

Video of the Day

The boundary string in this case is "part_c7161025_fe8a_45f4_83ef_6befcfa5d021".

Step

Each MIME part will be separated by the boundary string preceeded by two dashes ("--") on a blank line. You will need to scan the message line by line looking for the part boundaries. All text in between the boundaries except the final CR/LF is part of that MIME part.

Important: The last MIME part will end with the boundary string followed by two more dashes.

Example:

--part_c7161025_fe8a_45f4_83ef_6befcfa5d021 First MIME part.

--part_c7161025_fe8a_45f4_83ef_6befcfa5d021 Second MIME part

--part_c7161025_fe8a_45f4_83ef_6befcfa5d021 Last MIME part.

--part_c7161025_fe8a_45f4_83ef_6befcfa5d021--

Step

Each MIME part then needs to be parsed. The format of each MIME part is similar to the original RFC/822 formatted message. It will contain a series of headers, followed by a blank line, then the body data.

Step

A MIME part can itself be multipart, with its own "Content-Type" header and boundary string. Your code must recursively parse MIME parts until it reaches the child nodes.

Step

A MIME part can simply be a binary attachment. In this case a Filename header will usually be supplied, along with the transfer encoding. BASE64 is a popular encoding type. In this case the entire MIME part body must be BASE64 decoded. As an example here are the first few lines of an attached JPG image:

------=NextPart_003_01C755EF.43F2628D Content-Type: image/jpeg; name="testimage.jpg" Content-Transfer-Encoding: base64 Content-Description: testimage.jpg Content-Disposition: attachment; filename="testimage.jpg"

/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0a HBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIy MjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAQABQQDASIA AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA

Step

The HTML body of the message should be in a MIME section with ContentType: Text/Html, inside a ContentType: Multipart/Alternative MIME section.

Step

The plaintext body of the message should be in a MIME section with ContentType: Text/plain, inside a ContentType: Multipart/Alternative MIME section.

Show Comments

Related Articles

How to Insert the Date and Time in a Website

How to Insert the Date and Time in a Website

Around The Home
Productivity
By: demand86857
The Advantages of Hypertext Transfer Protocol

The Advantages of Hypertext Transfer Protocol

Around The Home
Productivity
By: Stephen Byron Cooper
How to Read MIME Format Emails

How to Read MIME Format Emails

Around The Home
Productivity
By: Joshua Duvauchelle
How to Create a Blank HTML Page

How to Create a Blank HTML Page

Around The Home
Productivity
By: Katelyn Kelley
How to Set Up a Legal Document Format

How to Set Up a Legal Document Format

Around The Home
Productivity
By: Mason Howard
How to Set Up a Memo Format

How to Set Up a Memo Format

Around The Home
Productivity
By: WendyL
  • HOW WE SCORE
  • ABOUT US
  • CONTACT US
  • TERMS
  • PRIVACY POLICY
  • COPYRIGHT POLICY
  • Advertise

An error occurred. Try again later.

Thanks for signing up!
© 2019 Leaf Group Ltd. Leaf Group Media

Get great tech advice delivered to your inbox.

Keep your family productive, connected, entertained, and safe.

Please enter a valid email.