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
-
print(‘Therefore it is not fully documented or clean. <br/>It contains code fragments of my own making and those found on the web, who knows where. <br/><br/>’);
-
print(‘There are THREE places where you must put in your email server, email username and email password. <br/>’);
-
-
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);
-
-
-
echo “Number of Total Emails: “
-
.imap_num_msg($mbox);
-
echo “ Number of Recent Emails: “
-
.imap_num_recent($mbox).“<BR><BR>”;
-
$headers=imap_headers($mbox);
-
for($x=0; $x <count($headers); $x++) {
-
$idx=($x+1);
-
$headers[$x]
-
</a><br>”;
-
}
-
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(‘<br/><strong>This is a print_r of only the header->from contents.<br/>I wanted to ensure the print_r contained all the info from the subarrays. This text should also be found above.</strong><br/><br/>’);
-
$from = $header->from;
-
$udate=$header->udate;
-
$subject= $header->fetchsubject;
-
echo “From Address: “;
-
$from[0]->mailbox,
-
$from[0]->host).
-
“<BR>”;
-
echo “Personal : “.
-
$from[0]->personal.
-
“<br>”;
-
echo “Adl : “.
-
$from[0]->adl.
-
“<br>”;
-
echo “Mailbox : “.
-
$from[0]->mailbox.
-
“<br>”;
-
echo “Host : “.
-
$from[0]->host.
-
“<br>”;
-
echo “Subject : “.
-
$subject.
-
“<br>”;
-
echo “Date : “.
-
$date.
-
“<br>”;
-
echo “To Address : “.
-
$header->toaddress.
-
“<br><BR><BR>”;
-
}
-
}
-
-
$mid = $num;
-
$struct = imap_fetchstructure($mbox, $mid);
-
$parts = $struct->parts;
-
$i = 0;
-
-
if (!$parts)
-
{
-
/* Simple message, only 1 piece */
-
$content = imap_body($mbox, $mid);
-
}
-
else
-
{
-
/* Complicated message, multiple parts */
-
-
$endwhile = false;
-
-
$content = “”; /* Content of message */
-
-
while (!$endwhile)
-
{
-
if (!$parts[$i])
-
{
-
{
-
}
-
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);
-
-
“filedata” => imap_fetchbody($mbox, $mid, $partstring));
-
}
-
{ /* Message */
-
$content .= imap_fetchbody($mbox, $mid, $partstring);
-
}
-
}
-
-
if ($parts[$i]->parts)
-
{
-
$parts = $parts[$i]->parts;
-
$i = 0;
-
}
-
else
-
{
-
$i++;
-
}
-
} /* while */
-
} /* complicated message */
-
-
print(‘<br/><strong>This is a print_r of the results of the entire content of the email turned into an array as offered by RJ<br/>NOTE: THIS MIGHT NOT BE SAFE HTML</strong><br/><br/>’);
-
-
-
print(‘<br/><strong>This is a print_r of the body in text format. This in theory should be included above, but this is quote protected.</strong><br/><br/>’);
-
-
imap_close($mbox);
-
}
-
-
/*********************************************************************************/
-
/* Calling routines */
-
/*********************************************************************************/
-
list_emails();
-
$num =1;
-
}
-
view_message_num($num);
-
?>
-
<hr/>
-
<br/>
-
<hr/>
-
-
-
<?php
-
function parsepart($p,$i){
-
//where to write file attachments to:
-
$filestore = ‘.’;
-
-
//fetch part
-
$part=imap_fetchbody($link,$msgid,$i);
-
//if type is not text
-
if ($p->type!=0){
-
//DECODE PART
-
//decode if base64
-
//decode if quoted printable
-
//no need to decode binary or 8bit!
-
-
//get filename of attachment if present
-
$filename=”;
-
// if there are any dparameters present in this part
-
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
-
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!=”){
-
}
-
//end if type!=0
-
}
-
-
//if part is text
-
else if($p->type==0){
-
//decode text
-
//if QUOTED-PRINTABLE
-
//if base 64
-
-
//OPTIONAL PROCESSING e.g. nl2br for plain text
-
//if plain text
-
-
//if HTML
-
}
-
-
//if subparts… recurse into function and parse them too!
-
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);
-
//see if there are any parts
-
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
-
//OPTIONAL PROCESSING
-
-
}
-
print(‘<br/><strong>This is a print_r of another method of getting the complete parts of the email. This is the parts array.</strong><br/><br/>’);
-
?>


At this point, in light of RFC3501 s6.4.5, I'd be trying imap_fetchbody() with a variety of part numbers, such as "0", "HEADER" and "HEADER.FIELDS Received-SPF".
IMAP is a crazy protocol. IMAP servers are even crazier.
Comment by Libertus (Paul Mitchell) — July 2, 2006 @ 4:03 am