|
Posted by chris_fieldhouse@hotmail.com on 10/10/84 11:39
Hi,
I have a script for processing emails,
The script finds email sent to a particular alias, grabs the body text
of the email and stores it into a database.
Problem is that certain character like '_' sometimes get stored as =5F,
and some email clients seem to add in =0D encoded characters.
I store the text in an Mysql database, and depending on the message
type I also set a flag to indicate whether its a plain text message, or
a MIME message with HTML coding.
Its alwasy the HTML messages that have the extra characters.
here is the code segment I modified from php.net to break down the
email structure and grab just the body text (not interested in storring
any attachments).
Any help would be appreciated.
# some borrowed code to get the email contents and attachments,
$MIME = FALSE;
$debug = "";
$struct = imap_fetchstructure($conn, $msg);
$parts = $struct->parts;
$i = 0;
# messages are either simple, only text, or complex with attachments.
if (!$parts) { /* Simple message, only 1 piece */
$content = imap_body($conn, $msg);
} else { /* Complicated message, multiple parts */
# complex message: multi-part - dump attachments (do not forward to
ANK).
$endwhile = false;
$stack = array(); /* Stack while parsing message */
$content = ""; /* Content of message */
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);
$debug .= strtoupper($parts[$i]->subtype) . "\n";
# only grab the plain text message - everything else will get dumped!
if (strtoupper($parts[$i]->subtype) == "PLAIN" && $MIME ==
FALSE) { /* Message */
$content = imap_fetchbody($conn, $msg, $partstring);
}
if (strtoupper($parts[$i]->subtype) == "HTML" ) { /*
Message */
$content = imap_fetchbody($conn, $msg, $partstring);
$MIME = TRUE;
}
}
if ($parts[$i]->parts) {
$stack[] = array("p" => $parts, "i" => $i);
$parts = $parts[$i]->parts;
$i = 0;
} else {
$i++;
}
} /* while */
} /* complicated message */
Navigation:
[Reply to this message]
|