note 83366 deleted from function.imap-fetchbody by nlopess

From: Date: Sun, 25 May 2008 09:13:42 +0000
Subject: note 83366 deleted from function.imap-fetchbody by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-139738@lists.php.net to get a copy of this message
Note Submitter: iberkner at gmail dot com ---- I found the comments below very useful in learning about accessing mailboxes. In particular, I wanted to capture an attachment (text file) from an e-mail. The examples below which use "munpack" were perfect except that for some reason my shared hosing account at hostgator.com does not allow us to use "munpoack" only with a dedicated server. After looking at the examples from below, I wasn't able to easily figure out how to save the parts of an e-mail and extract the text file attachment as a file. I did end up finding a great PHP MIME parsing class which essentially does what "munpack" does but using PHP code. The class can be found here: http://www.phpclasses.org/browse/package/3169.html The code below modifies the writeAttachmentsToDisk function as follows: <?php require_once('mimeparser/rfc822_addresses.php'); require_once('mimeparser/mime_parser.php'); function writeAttachmentsToDisk($mailbox, $msg_number) { $returnResult = "OK"; $message_file = "tmp.eml"; // write the message body to disk (eml format) imap_savebody($mailbox, $message_file, $msg_number); $mime=new mime_parser_class; /* * Set to 0 for parsing a single message file * Set to 1 for parsing multiple messages in a single file in the mbox format */ $mime->mbox = 0; /* * Set to 0 for not decoding the message bodies */ $mime->decode_bodies = 1; /* * Set to 0 to make syntax errors make the decoding fail */ $mime->ignore_syntax_errors = 0; $parameters=array( 'File'=>$message_file, /* Save the message body parts to a directory */ 'SaveBody'=>'', ); if(!$mime->Decode($parameters, $decoded)) { $returnResult = 'MIME message decoding error: '.$mime->error.' at position '.$mime->error_position; } return $returnResult; } ?> We're using this on http://www.spellingcity.com, it is working great.

« previous php.notes (#139738) next »