| Home > Computer & I'net > PHP: PEAR mimeDecode Module... |
|
|
|||||||||||||||
|
|
|||||||||||||||
|
||||||||||||||||
PHP: PEAR mimeDecode Module | |
While most MIME email is totally useless, it would be better if it was sent as a plain text message, MIME sometimes is useful. When sending attachments, for example. In this article, we will see how we can decode MIME messages with PHP. We will use the mimeDecode module to do the actual decoding for us. mimeDecode is part of the PEAR library. PEAR is already installed if you have a newer version of PHP, but if you don't it's also easy to install. How to get PEAR and mimeDecodeBefore trying to install PEAR and the mimeDecode module, you should first make sure that you don't yet have it. If you have a recent version of PHP (> 4.3.0pre1), the PEAR base installation is already installed on your system. Since the mimeDecode module is part of the PEAR core, that's also installed. To check whether you have to install the mimeDecode or not, you can run the following PHP script: <?php
include('Mail/mimeDecode.php');
?>
If this doesn't give an error, you the mimeDecode module is installed on your system. If it does, you'll have to install PEAR (or upgrade your version of PHP). Read the PEAR manual for instructions. MIME: an introductionBefore you can successfully write a script that decodes MIME email, you'll have to know a little about the anatomy of a MIME message. I'll give you a short introduction to MIME. If you want to read more about the details of MIME, there is a chapter available from O'Reilly. A tradition emailThe source of a basic, non-MIME email looks like this: From: Gijs van Tulder <gvtulder@example.com> To: thelist@lists.evolt.org Subject: Decoding MIME mail Date: Wed, 12 Mar 2003 10:26:59 +0100 Hi, this is my message. The first lines of this email contain headers, data about this message. These headers consist of a header name, before the colon, and some data, after the colon. (If you are familiar with the HTTP headers, you'll notice that email headers use the same syntax.) There are actually many more possible headers that I didn't include in this example, but all headers are in the form The body of the message, in our example We'll now see what happens with our message when we add a MIME attachment to it. From: Gijs van Tulder <gvtulder@example.com> To: thelist@lists.evolt.org Subject: Decoding MIME mail Date: Wed, 12 Mar 2003 10:26:59 +0100 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="MyBoundary" This is a multi-part message in MIME format. --MyBoundary Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Hi, this is my message. See the attached image! --MyBoundary Content-Type: image/gif; name="myimage.gif" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="myimage.gif" R0lGODlhogBrAPcAAAAAAP///zwKC2UhIrdPUKU0OMmLjagEDJIEDZEmLduztW8HD64YJlUJEXkX ...omitted many lines like the above... gahQUgx4n5kQWQQJ0sqEREAAADs= --MyBoundary-- As you can see, our message now contains two different parts: the message body, Note that this boundary can be set to any possible string. It's fairly obvious that normal mail programs don't use 'MyBoundary' as the boundary string, but take a longer random string. It still has the same effect, though. The MIME partsNow, let's take a look at the MIME parts. You'll notice that these parts look a lot like the email message: it starts with a number of headers, followed by an empty line and then there is the body. Content-Type: image/gif; name="myimage.gif" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="myimage.gif" R0lGODlhogBrAPcAAAAAAP///zwKC2UhIrdPUKU0OMmLjagEDJIEDZEmLduztW8HD64YJlUJEXkX ...omitted many lines like the above... gahQUgx4n5kQWQQJ0sqEREAAADs= This is the MIME part containing the attached image. You see the The body of this part looks very strange. Many similar lines followed the first line of characters, but I deleted them except from the last line. What you see here is an encoded version of the original file. Since an email message can only contain normal text, the binary form of the image had to be translated to a text form. This is called MIME encoding. The Now that we know a little about MIME email, we can almost start writing the script. But wait, you'll first have to get a message that your script can parse. If you're using Linux/Unix, you can send yourself a MIME email and copy your mbox file to get the source of that message. You can also set up a email to PHP script to get the message source. If you just want to test the mimeDecode module, you can also just download the source of my example message. In this example script, I will assume that the source email is saved as To load the mimeDecode module, we just have to include include('mail/mimeDecode');
Setting the parameters
The mimeDecode module accepts five parameters. The fourth parameter, $params['include_bodies'] = true; $params['decode_bodies'] = true; $params['decode_headers'] = true; $params['input'] = $input;Running decode() It's time to run mimeDecode's $structure = Mail_mimeDecode::decode($params); The decoded message is saved in We've now got the decoded message in the
We can now walk through the foreach ($structure->parts as $part) {
// only save if an attachment
if (isset($part->disposition) and
($part->disposition=='attachment') {
// open file
$fp = fopen($part->ctype_parameters['filename'], 'w');
// write body
fwrite($fp, $part->body);
// close file
fclose($fp);
}
}
For each part, we check if it has a In the same way, we can list all images and their sizes by checking the $list = '';
foreach ($structure->parts as $part) {
// is this an image?
if ($part->ctype_primary=='image') {
$list .= $part->ctype_parameters['filename'].': '.
strlen($part->body)." bytes\n";
}
}
// send this list
$to = $structure->headers['from'];
$subject = 'Re: '.$structure->headers['subject'];
$body = "You sent us these images:\n\n$list\n\nThank you very much!";
$headers = 'From: '.$structure->headers['to'];
mail($to, $subject, $body, $headers);
If the By now, you should be able to write your own scripts using the mimeDecode module. You could, for example, write a script that lets you just email images and get them published on your web log. In the rare event that you are a teacher, you could save all documents that your students send you in your 'incoming files' directory, without having to open each message first. ----------------- Gijs is a full time Dutch student in economics and a spare time Web developer. He spends his time developing scripts using PHP, MySQL and other external programs. Visit his site at: http://gvtulder.f2o.org/ | |
| Articles |
•Auto & Trucks•Business•Computer & I'net·General·Apache·CSS·Database·Hardware·HTML·Javascript&DHTML·Linux·MySQL·Operating System·Perl / CGIPHP·Programming·Publishing·Search Engines·Software Problems·SSI·Tips & Tricks·Utilities·Web Design•Family•Food & Drink•Gardening•Health•Other•Pets•Psychology•Spiritual•Travel•Women |
| Calculators |
|