note 83366 added to function.imap-fetchbody
| From: | iberkner at gmail dot com | Date: | Thu, 22 May 2008 15:28:19 +0000 |
| Subject: | note 83366 added to function.imap-fetchbody | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-139615@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 69.147.83.197
Probable Submitter: 74.164.110.127
----
Manual Page -- http://www.php.net/manual/en/function.imap-fetchbody.php
Edit -- https://master.php.net/note/edit/83366
Del: integrated -- https://master.php.net/note/delete/83366/integrated
Del: useless -- https://master.php.net/note/delete/83366/useless
Del: bad code -- https://master.php.net/note/delete/83366/bad+code
Del: spam -- https://master.php.net/note/delete/83366/spam
Del: non-english -- https://master.php.net/note/delete/83366/non-english
Del: in docs -- https://master.php.net/note/delete/83366/in+docs
Del: other reasons-- https://master.php.net/note/delete/83366
Reject -- https://master.php.net/note/reject/83366
Search -- https://master.php.net/manage/user-notes.php