Buca Bay - Always nice

Dua tiko noqu toa loaloa, na yacana ko… laga mai…

A PHP Mime Mail parser using MailParse Extension

April21

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:

  1. 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...
  2. 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...
  3. 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....
posted under web development
2 Comments to

“A PHP Mime Mail parser using MailParse Extension”

  1. On April 22nd, 2009 at 3:01 pm Sandra MonsterID Icon Sandra Says:

    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!

  2. On April 23rd, 2009 at 1:29 pm admin MonsterID Icon admin Says:

    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.

Email will not be published

Website example

Your Comment:

 
Tag Cloud