A PHP Mime Mail parser using MailParse Extension
Ever tried parsing Mime Mail? Not for the faint hearted, I assure you. The great thing is that PHP has an extension for parsing Mime Messages called MailParse. The bad news is that using this extension is probably just as hard as writing your own Mime Parser.
Fortunately with a bit of help I’ve put together a Mime Mail Parser Class that wraps the MailParse extension functions making it simple, efficient and fast to parse mime mail in PHP.
Why another Mime Mail parser? Well for two main reasons.
1) Pure PHP implementations are slow and inefficient compared to MailParse.
2) MailParse is too hard to use, and is not OO.
Therefore welcome to MimeMailParser.
Here is a an example that shows how easy it is to parse raw mime mail using MimeMailParser:
<?php
require_once('MimeMailParser.class.php');
$path = 'path/to/mail.txt';
$Parser = new MimeMailParser();
$Parser->setPath($path);
$to = $Parser->getHeader('to');
$from = $Parser->getHeader('from');
$subject = $Parser->getHeader('subject');
$text = $Parser->getMessageBody('text');
$html = $Parser->getMessageBody('html');
//$attachments = $Parser->getAttachments();
$attachments = $Parser->getAttachmentsAsStreams();
?>
You can find the source code here for your enjoyment.
Related posts:
- Incoming mail with PHP Mime Mail Parser I’ve just added the ability to parse mime mail from standard input into PHP Mime Mail Parser. This allows you...
- Google AJAX Language API with PHP I had noticed some time ago that Google had released an API for their language translation service. A recent forum...
- Secure PHP Programming for Web Developers Security in PHP is the same as any server side programming language, they are all vulnerable to the same attacks....
Thanks, looks great! I got this error when I tried to use it: “Fatal error: Call to undefined function mailparse_msg_parse_file() in MimeMailParser.class.php on line 66″
Is this an error or is it my bad? Thanks!
Hi Sandra,
Please refer to this documentation.
http://code.google.com/p/php-mime-mail-parser/wiki/RequirementsAndInstallation
The error you are getting is because you do not have the MailParse extension built into PHP.