View Full Version : Getting body of pop3 email msg fails on some systems when using PHP's IMAP functions

11-09-2010, 09:42 PM
Hey guys..... I've built a POP3 mail retriever apart from a couple functions I got from the PHP.net site in the comments section & modified a bit, which basically gets the message structure & parses the "parts" into separate variables.

Now this is all working fine on my local system & my server on the web..... I get the contents of the msg fine... for my purposes I'm using the "$plainmsg" variable.

However for some reason it doesn't appear to be working on my clients local machine.. when the script is run he always ends up with an empty body.... here is the relevant 2 methods for the body portion..

function getMsg($connection,$uid) {

global $htmlmsg,$plainmsg,$charset,$attachments;

// Define data holder array
$part_array = array();

// Define some vars
$htmlmsg = '';
$plainmsg = '';
$charset = '';
$attachments = '';

// Get message structure
$structure = imap_fetchstructure($connection,$uid, FT_UID);

if (!$structure->parts) { // not multipart

$this->getPart($connection,$uid,$structure,0); // no part-number, so pass 0

} else { // multipart: iterate through each part

foreach ($structure->parts as $partno0 => $part) {


if (!empty($htmlmsg)) $part_array['html'] = $htmlmsg;
if (!empty($plainmsg)) $part_array['plain'] = $plainmsg;
if (!empty($charset)) $part_array['charset'] = $charset;
if (!empty($attachments)) $part_array['attachments'] = $attachments;

// Return data array
return $part_array;


/* Makes up & decodes the different 'parts' of the message body */

protected function getPart($connection,$uid,$p,$partno) {

// $partno = '1', '2', '2.1', '2.1.3', etc if multipart, 0 if not multipart
global $htmlmsg,$plainmsg,$charset,$attachments;

$data = ($partno)?
imap_fetchbody($connection,$uid,$partno, FT_UID): // multipart
imap_body($connection,$uid, FT_UID); // not multipart
// Any part may be encoded, even plain text messages, so check everything.
if ($p->encoding==4)
$data = quoted_printable_decode($data);
elseif ($p->encoding==3)
$data = base64_decode($data);
// no need to decode 7-bit, 8-bit, or binary

// get all parameters, like charset, filenames of attachments, etc.
$params = array();
if ($p->parameters)
foreach ($p->parameters as $x)
$params[ strtolower( $x->attribute ) ] = $x->value;
if ($p->dparameters)
foreach ($p->dparameters as $x)
$params[ strtolower( $x->attribute ) ] = $x->value;

// Any part with a filename is an attachment,
// so an attached text file (type 0) is not mistaken as the message.
if ($params['filename'] || $params['name']) {
// filename may be given as 'Filename' or 'Name' or both
$filename = ($params['filename'])? $params['filename'] : $params['name'];
// filename may be encoded, so see imap_mime_header_decode()
$attachments[$filename] = $data; // this is a problem if two files have same name

elseif ($p->type==0 && $data) {
// Messages may be split in different parts because of inline attachments,
// so append parts together with blank row.
if (strtolower($p->subtype)=='plain')
$plainmsg .= trim($data) ."\n\n";
$htmlmsg .= $data ."<br><br>";
$charset = $params['charset']; // assume all parts are same charset

// Many bounce notifications embed the original message as type 2,
// but AOL uses type 1 (multipart), which is not handled here.
// There are no PHP functions to parse embedded messages,
// so this just appends the raw source to the main message.
elseif ($p->type==2 && $data) {
$plainmsg .= trim($data) ."\n\n";

if ($p->parts) {
foreach ($p->parts as $partno0=>$p2)
$this->getPart($connection,$uid,$p2,$partno.'.'.($partno0+1)); // 1.2, 1.2.1, etc.


I've echoed out the $plainmsg variable at the bottom of the getParts() method & it's empty... anyone know why this would work fine on my two servers & not my clients? Some kind of PHP setting that is affecting something?

Thanks a lot!

11-09-2010, 09:50 PM
IMAP functions not installed on your clients machine? Far as I recall, they're reliant on external dependencies during compilation. You have checked everything is okay in that regard?

11-09-2010, 10:26 PM
Yeah. He has IMAP installed.... got all the headers fine such as subject, date, to, from etc..... the body is the problem.

If it helps.... here is a saved copy of his PHP info I took home with me.. http://www.condorstudios.com/work/steve/start.htm

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum