You are here: email processing script, need help trying to catch =0D and =5f encoded characters « All PHP « IT news, forums, messages
email processing script, need help trying to catch =0D and =5f encoded characters

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация