May13
I’ve just added the ability to parse mime mail from standard input into PHP Mime Mail Parser. This allows you to receive and parse email in PHP efficiently and effortlessly.
To pipe email to PHP, follow one of these articles:
Evolt - Incoming Mail and PHP
DevPapers - Incoming Mail and PHP
DevArticles - Incoming Mail and PHP
Then for your PHP code, get the PHP Mime Mail Parser, and use:
<?php
// include the mime-mail-parser class
require_once('MimeMailParser.class.php');
// instantiate
$Parser = new MimeMailParser();
// read the email from stdin
$Parser->setStream(STDIN);
// get the email parts
$to = $Parser->getHeader('to');
$delivered_to = $Parser->getHeader('delivered-to');
$from = $Parser->getHeader('from');
$subject = $Parser->getHeader('subject');
$text = $Parser->getMessageBody('text');
$html = $Parser->getMessageBody('html');
$attachments = $Parser->getAttachments();
?>
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.