Software, your way.
burger menu icon
WillMaster

WillMaster > LibrarySnooping (Information Retrieval)

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Decoding Encoded Email

When you look at the source code of an email, it sometimes contains blocks of what appears to be random alphanumeric characters. It could be an encoded image in the email, an encoded attachment, or the entire email content may be encoded.

It's encoded with MIME Base64, a content transfer encoding scheme to convert binary data (like images) into plain text characters so they can be safely sent via email.

Email readers generally allow viewing of the source code of individual emails through their menu system. The menu item may be named "View Source," "View Raw Source," "View Full Headers," or something reminiscent of those. The menu item you're looking for may be within another menu item such as "Reader" or "Other Actions." The email reader's help function is likely to have instructions.

I'll describe how to decode the encoded sections of email content and attachments.

Reasons for decoding range from inability to view the email in a regular reader to just plain curiosity.

I'll describe what to look for in the email, how to determine the file type and file name that's encoded, and how to extract the encoded portion from the email.

And there's a PHP script you can use to do the decoding.

What To Look For In the Email

If the email has any sections encoded with MIME Base64, you'll see from one to hundreds of lines that look something like this:

2zqR3aGhoev0/EaJw3p6eqfO8Gus5Wqh0lua0Y2jtr6+vjaQ3TOO3M3NzX646dHl90t8p8TE
xJbE7MLd9bHF13ykxoOpyvT5/ZvH7urq6uXx+6m6yBd+18bY6FRmdZezzM/k9rGxsY7A69vr
+VGe4Xay53OYuTyIynSp2LzT5vX19W2u5pmuv8nh9pu714eYp9bo+IWy2l2g28zj9jCM26rQ

The lines will be the same length, generally 72-76 characters, except the last line may be shorter.

When you've found a section like that in the source code of an email, you've found a section that can be decoded.

How To Determine the File Type and File Name

The encoded section will generally exist between delimiters and have a header of several lines. (Even if the delimiter and header are missing, if the encoded section is intact it may still be decoded.)

Here's an example:

--------------030805010800090005050804
Content-Type: image/gif;
 name="wmlogo.gif"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename="wmlogo.gif"

R0lGODlhMgAyAOYAAEub4KzQ8fn7/FWh4pnG7WKbzdvb21OGsom965ubm7za89fX16LM77PV
8uPj41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Ia8pCAfAgRMsCBRjry
kZCMpCQjmQGOtCIgSxGLJjfJSbGAoYchABkAGC5AylKa8pSoTCUpwUAEhwRDAH/kCVFmGZQl
AFIv4AgABYgghF768pfADKYviUCBEIKjECIwARoiUJNmQiACaHjGMS3xgj5QAAAYgIAMtsnN
bm4TAhgAAAX68IJptkITnPBDBIrgAgK4kwAuKEIE/NCJVZgTHIhQBCMcAQlJUGKagQAAOw==
--------------030805010800090005050804

In the above code, the delimiter is colored red. If the bottom delimiter is the last delimiter in the email, it's likely to have an extra two hyphens ("--") appended to it (which tells the email reader "this is the last delimiter").

The header is colored blue. The table below addresses the information in the several header lines in the example. Your email might have some, all, or none of the same headers, but usually there will at least be a Content-type header key.

The header keys have no spaces and end with a colon character. They are case-insensitive and they may wrap. If wrapped, the subsequent lines are indented by at least one space.

Notes follow the table.

Header KeyHeader Value
Content-Type: image/gif; name="wmlogo.gif"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="wmlogo.gif"

Notes:

  1. Content-Type:

    Here, you'll find what type of content is encoded. In the example, it's a GIF image.

    Sometimes, you'll also find the encoded file name here. But the file type is most important because, so long as you know the type, you can save the file with any name that has the correct file type extension.

  2. Content-Transfer-Encoding:

    If it's available in your email, verify that the encoding is "base64" or "MIME Base 64" (not case sensitive). If it has a different encoding, the PHP script with this article is unlikely to be able to decode it.

  3. Content-Disposition:

    This tells the email reader what to do with the encoded section. In the example, the email knows it's an attachment. The disposition might also be "inline" if the content is to be inserted within the visible email content.

    If it's an attachment and sometimes otherwise, the filename to save the file as should be specified.

Extracting the Encoded Portion From the Email

The black colored lines of the example is the encoded content or file.

The encoded section is plain text. Most email readers, when displaying email source code, do so with plain text, ready for copying and pasting into the box of the PHP script further below.

If yours is in OpenOffice or Word or in other software that can format text, export it as plain text before copying the encoded section. Otherwise, the decoding may fail.

The PHP Script

The PHP code is good to go as is — no customization needs to be done. To install:

  1. Copy the PHP code.

  2. Save it as EmailDecode.php (or other name that you prefer, so long as it has a .php file name extension).

  3. Upload it to your server.

  4. Make a note of its URL.

<?php
/* 
   Decode Base64-encoded Content
   Version 1.0
   August 18, 2014

   Will Bontrager Software LLC
   https://www.willmaster.com/
   Copyright 2014 Will Bontrager Software LLC

   This software is provided "AS IS," without 
   any warranty of any kind, without even any 
   implied warranty such as merchantability 
   or fitness for a particular purpose.
   Will Bontrager Software LLC grants 
   you a royalty free license to use or 
   modify this software provided this 
   notice appears on all copies. 
*/
$Process = false;
$Content = $Message = $Location = $Decoded = '';
if( isset($_POST['submitter']) ) { $Process = true; }
if( $Process )
{
   $_POST['content'] = trim($_POST['content']);
   if( empty($_POST['content']) ) { $Process = false; }
}
if( $Process )
{
   $Content = $_POST['content'];
   $Content = base64_decode($Content);
   $_POST['save'] = trim($_POST['save']);
   if( ! empty($_POST['save']) )
   {
      if( file_exists($_POST['save']) ) { $Message .= '<div style="margin:15px;">A directory entry for ' . $_POST['save'] . ' already exists. File not saved.</div>'; }
      elseif( file_put_contents($_POST['save'],$Content) === false ) { $Message .= '<div style="margin:15px;">Unable to save decoded content to ' . $_POST['save'] . '</div>'; }
   }
   if( empty($_POST['view']) or $_POST['view'] != 'yes' ) { $Content = ''; }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Email Decode</title>
<link href="//www.willmaster.com/12willmaster.css" rel="stylesheet" type="text/css">
<style type="text/css">
html {
   font-size:100%; 
   font-family:verdana,arial,sans-serif;
   }
body {
   font-size:1em;
   color:#000000;
   background-color:white;
   margin:0;
   text-align:left;
   }
p { font-size:1em; line-height:120%; margin-bottom:3px; }
h2 { font-size:1.6em; text-align:left; }
a { text-decoration:none; color:#1c5292; font-weight:bold; }
a, a img { border:none; outline:none; }
.nowrap { white-space:nowrap; }
.input { width:500px; }
.submit { width: 512px; }
.input { border:1px solid black; border-radius:5px; padding:5px; }
#content { margin:0 0 0 150px; padding:75px 0 100px 50px; width:550px; border-left:6px groove #2F83E5; }
</style>
</head>
<body><div id="content">
<form method="post" enctype="multipart/form-data" accept-charset="utf-8" action="<?php echo($_SERVER['PHP_SELF']); ?>">
<h2>Email MIME Base64 Decode</h2>
<div id="message-container" style="display:none; margin-bottom:50px;">
<p>
<b>Message:</b>
</p>
<div style="margin-bottom:50px; border:3px dashed red; color:red; border-radius:11px;">
<?php echo( $Message ); ?>
</div>
</div>
<div id="view-container" style="display:none; margin-bottom:50px;">
<p>
The converted content:
</p>
<div style="margin-bottom:50px; border:3px solid #999; padding:15px; border-radius:11px;">
<?php echo( htmlspecialchars($Content) ); ?>
</div>
</div>
<script type="text/javascript">
if( "<?php echo(preg_replace('/\W/','',$Message)); ?>".length ) { document.getElementById("message-container").style.display = "block"; }
if( "<?php echo(preg_replace('/\W/','',$Content)); ?>".length ) { document.getElementById("view-container").style.display = "block"; }
</script>
<p>
Paste the encoded section here:
</p>
<textarea name="content" class="input" style="height:200px;"></textarea>
<p>
To save the decoded content to a file, specify the location and file name <span class="nowrap">(location must be writable):</span>
</p>
<input type="text" name="save" class="input">
<p>
<input type="checkbox" name="view" value="yes" checked="checked"> View decoded content.
</p>
<p>
<input type="submit" name="submitter" class="submit" value="Decode">
</p>
<p>
Copyright 2014 <a href="//www.willmaster.com/">Will Bontrager Software LLC</a>
</p>
<div style="position:fixed; left:15px; top:50px; text-align:center;">
<a href="//www.willmaster.com/">
<img src="//www.willmaster.com/images/wmlogo_icon.gif" style="width:50px; height:50px; border:none;" alt="Willmaster logo">
</a>
<br><a href="//www.willmaster.com/">Willmaster.com</a>
<br><a href="//www.willmaster.com/contact.php">Contact</a>
</div>
</body>
</html>

To use the PHP script, type its URL into your browser. Then paste the encoded section of the email into the text box.

Optionally, the decoded section of the email can be saved to a file, which generally would be desired for binary files like images and PDF documents. The directory where the file is to be saved must be writable — which might mean, depending on how your server is set up, creating a directory with 777 permissions for saving the files.

Here's a screenshot of the PHP script's control panel.

PHP script control panel screenshot

When the script is installed, copy the encoded section of the email and paste it into the control panel.

Then click the button :)

Bonus: How To Encode a File Into Base64

If you have occasion to need a file encoded into Base64, this PHP script can do the trick:

<pre>
<?php
echo chunk_split(base64_encode(file_get_contents("FILE_LOCATION")));
?>
</pre>

Replace FILE_LOCATION with the location of the file to encode. (With some PHP configurations, you can optionally specify the file's URL. With others, the only option is to specify the file's location on the server.)

Upload it to the server and type it's URL into your browser.

The script's output is the plain text encoding of the specified file.

(This article first appeared in Possibilities ezine.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC