Please include your work to support the answer…

Cindy’s comment on my last post reminded me that I did not show any of the routines I tried while trying to figure out how to get the full raw email from the built in PHP imap_ routines.

So, here it is… It is obviously scrap work, but some one might find it useful to have all in one place. Just save this as a php file on your server (some place not shared since you’ll have your email password in it in raw text) and then change the mail server, username and password references (3 of them) to your own. As is, this routine will display a VERY simple webmail reader. It displays a list of messages in your box and the contents of 1 message.

It should give you the groundworks for building something more complex.

However, what it does not include is any part of the TRUE header which is what I pointed out here that I really need.

BTW this routine is OBVIOUSLY NOT safe html. If you display an email that has an embedded code that does nasty stuff, this testing routine will not protect you from it. It is JUST test code. Use it wisely.

[php]
This is just an example of how I research a topic.
‘);
print(‘Therefore it is not fully documented or clean.
It contains code fragments of my own making and those found on the web, who knows where.

‘);
print(‘There are THREE places where you must put in your email server, email username and email password.
‘);
print(‘Otherwise, you will get very limited results.

‘);

function list_emails(){
$MAILSERVER=”{imap.1and1.com:143}”;
// or $MAILSERVER=”{pop.1and1.com:110}”;
// or $MAILSERVER=”{localhost:143}”;
$PHP_AUTH_USER = “YourAddrHere@thecodecave.com”;
$PHP_AUTH_PW = “YOUR_PW_HERE”;

$mbox=imap_open($MAILSERVER,
$PHP_AUTH_USER,
$PHP_AUTH_PW);

print(‘


‘);
print(‘
THIS SECTION REPORTS ON THE FULL MAIL BOX

‘);

echo “Number of Total Emails: ”
.imap_num_msg($mbox);
echo ” Number of Recent Emails: ”
.imap_num_recent($mbox).”

“;
$headers=imap_headers($mbox);
for($x=0; $x < count($headers); $x++) { $idx=($x+1); echo $idx."
$headers[$x]

“;
}
imap_close($mbox);
}

function view_message_num($num){
$MAILSERVER=”{imap.1and1.com:143}”;
// or $MAILSERVER=”{pop.1and1.com:110}”;
// or $MAILSERVER=”{localhost:143}”;
$PHP_AUTH_USER = “YourAddrHere@thecodecave.com”;
$PHP_AUTH_PW = “YOUR_PW_HERE”;

$mbox=imap_open($MAILSERVER,
$PHP_AUTH_USER,
$PHP_AUTH_PW);
$header=imap_headerinfo($mbox,
$num,
180, 180);
print(‘


‘);
print(‘
This is a print_r of the header contents.

‘);
print_r($header);
print(‘


‘);
print(‘
This is a print_r of only the header->from contents.
I wanted to ensure the print_r contained all the info from the subarrays. This text should also be found above.

‘);
print_r($header->from);
print(‘


‘);
print(‘
This is the header info parsed out

‘);
$from = $header->from;
$udate=$header->udate;
$date=Date(“F j, Y, g:i a”, $udate);
echo “Message Number: “. $num.”
“;
$subject= $header->fetchsubject;
if (is_array($from)){
while(list($key, $val) = each($from)) {
echo “From Address: “;
echo $fromaddr=sprintf(“%s@%s”,
$from[0]->mailbox,
$from[0]->host).

“;
echo “Personal : “.
$from[0]->personal.

“;
echo “Adl : “.
$from[0]->adl.

“;
echo “Mailbox : “.
$from[0]->mailbox.

“;
echo “Host : “.
$from[0]->host.

“;
echo “Subject : “.
$subject.

“;
echo “Date : “.
$date.

“;
echo “To Address : “.
$header->toaddress.



“;
}
}

print(‘


‘);
print(‘
This is a print_r of the results of a fetchstructure

‘);
$mid = $num;
$struct = imap_fetchstructure($mbox, $mid);
print_r($struct);
$parts = $struct->parts;
$i = 0;

if (!$parts)
{
/* Simple message, only 1 piece */
$attachment = array(); /* No attachments */
$content = imap_body($mbox, $mid);
}
else
{
/* Complicated message, multiple parts */

$endwhile = false;

$stack = array(); /* Stack while parsing message */
$content = “”; /* Content of message */
$attachment = array(); /* Attachments */

while (!$endwhile)
{
if (!$parts[$i])
{
if (count($stack) > 0)
{
$parts = $stack[count($stack)-1][“p”];
$i = $stack[count($stack)-1][“i”] + 1;
array_pop($stack);
}
else
{
$endwhile = true;
}
}

if (!$endwhile)
{
/* Create message part first (example ‘1.2.3’) */
$partstring = “”;
foreach ($stack as $s)
{
$partstring .= ($s[“i”]+1) . “.”;
}
$partstring .= ($i+1);

if (strtoupper($parts[$i]->disposition) == “ATTACHMENT”) { /* Attachment */
$attachment[] = array(“filename” => $parts[$i]->parameters[0]->value,
“filedata” => imap_fetchbody($mbox, $mid, $partstring));
}
elseif (strtoupper($parts[$i]->subtype) == “PLAIN”)
{ /* Message */
$content .= imap_fetchbody($mbox, $mid, $partstring);
}
}

if ($parts[$i]->parts)
{
$stack[] = array(“p” => $parts, “i” => $i);
$parts = $parts[$i]->parts;
$i = 0;
}
else
{
$i++;
}
} /* while */
} /* complicated message */

print(‘


‘);
print(‘
This is a print_r of the results of the entire content of the email turned into an array as offered by RJ
NOTE: THIS MIGHT NOT BE SAFE HTML

‘);
print_r($content);

print(‘


‘);
print(‘
This is a print_r of the body in text format. This in theory should be included above, but this is quote protected.

‘);
$Body_text = quoted_printable_decode($content);
print_r($Body_text);

imap_close($mbox);
}

/*********************************************************************************/
/* Calling routines */
/*********************************************************************************/
list_emails();
if (!isset($num)) {
$num =1;
}
view_message_num($num);
?>



type!=0){
//DECODE PART
//decode if base64
if ($p->encoding==3)$part=base64_decode($part);
//decode if quoted printable
if ($p->encoding==4)$part=quoted_printable_decode($part);
//no need to decode binary or 8bit!

//get filename of attachment if present
$filename=”;
// if there are any dparameters present in this part
if (count($p->dparameters)>0){
foreach ($p->dparameters as $dparam){
if ((strtoupper($dparam->attribute)==’NAME’) ||(strtoupper($dparam->attribute)==’FILENAME’)) $filename=$dparam->value;
}
}
//if no filename found
if ($filename==”){
// if there are any parameters present in this part
if (count($p->parameters)>0){
foreach ($p->parameters as $param){
if ((strtoupper($param->attribute)==’NAME’) ||(strtoupper($param->attribute)==’FILENAME’)) $filename=$param->value;
}
}
}
//write to disk and set partsarray variable
if ($filename!=”){
$partsarray[$i][attachment] = array(‘filename’=>$filename,’binary’=>$part);
$fp=fopen($filestore.$filename,”w+”);
fwrite($fp,$part);
fclose($fp);
}
//end if type!=0
}

//if part is text
else if($p->type==0){
//decode text
//if QUOTED-PRINTABLE
if ($p->encoding==4) $part=quoted_printable_decode($part);
//if base 64
if ($p->encoding==3) $part=base64_decode($part);

//OPTIONAL PROCESSING e.g. nl2br for plain text
//if plain text

if (strtoupper($p->subtype)==’PLAIN’)1;
//if HTML
else if (strtoupper($p->subtype)==’HTML’)1;
$partsarray[$i][text] = array(‘type’=>$p->subtype,’string’=>$part);
}

//if subparts… recurse into function and parse them too!
if (count($p->parts)>0){
foreach ($p->parts as $pno=>$parr){
parsepart($parr,($i.’.’.($pno+1)));
}
}
return;
}

//open resource
$MAILSERVER=”{imap.1and1.com:143}”;
// or $MAILSERVER=”{pop.1and1.com:110}”;
// or $MAILSERVER=”{localhost:143}”;
$PHP_AUTH_USER = “YourAddrHere@thecodecave.com”;
$PHP_AUTH_PW = “YOUR_PW_HERE”;

$link=imap_open($MAILSERVER,
$PHP_AUTH_USER,
$PHP_AUTH_PW);

$msgid = 2;
//fetch structure of message
$s=imap_fetchstructure($link, $msgid);
print_r($s);
die();
//see if there are any parts
if (count($s->parts)>0){
foreach ($s->parts as $partno=>$partarr){
//parse parts of email
parsepart($partarr,$partno + 1);
}
}

//for not multipart messages
else{
//get body of message
$text=imap_body($link,$msgid);
//decode if quoted-printable
if ($s->encoding==4) $text=quoted_printable_decode($text);
//OPTIONAL PROCESSING
if (strtoupper($s->subtype)==’PLAIN’) $text=$text;
if (strtoupper($s->subtype)==’HTML’) $text=$text;

$partsarray[‘not multipart’][text]=array(‘type’=>$s->subtype,’string’=>$text);
}
print(‘


‘);
print(‘
This is a print_r of another method of getting the complete parts of the email. This is the parts array.

‘);
print_r($partsarray);
?>
[/php]

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *