note 100558 deleted from book.mailparse by danbrown
| From: | danbrown@php.net | Date: | Sat, 23 Oct 2010 05:39:09 +0000 |
| Subject: | note 100558 deleted from book.mailparse by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-173393@lists.php.net to get a copy of this message | ||
Note Submitter: gabe at fijiwebdesign dot com
----
I've put together an open source mail parser class using mailpase functions.
http://code.google.com/p/php-mime-mail-parser/
This should make it more straight forward to parse the email. Example usage:
<?php
// mail parser class
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();
// saving attachments
$save_dir = '/path/to/save/attachments/';
foreach($attachments as $attachment) {
// get the attachment name
$filename = $attachment->filename;
// write the file to the directory you want to save it in
if ($fp = fopen($save_dir.$filename, 'w')) {
while($bytes = $attachment->read()) {
fwrite($fp, $bytes);
}
fclose($fp);
}
}
?>
Hope this helps.