473,394 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

email processing script, need help trying to catch =0D and =5f encoded characters

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 */

Feb 8 '06 #1
6 1812
d
<ch**************@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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.


[snippy snip snip]

http://en.wikipedia.org/wiki/Quoted-printable

It's a type of MIME encoding called "quoted printable". Read up about it -
it's really quite simple to encode/decode.

dave
Feb 8 '06 #2
Ah,

So I should do this check
if (strtoupper($parts[$i]->encoding) == "QUOTED-PRINTABLE")
and if true, then use
imap_qprint function to convert it to 8-Bit.

Does that seem like a reasonable approach?

Thanks for your help.

Feb 8 '06 #3

<ch**************@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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.


You could try using addslashes($content) just before writing the string to
the DB. Use stripslashes() when you pull the string back out.

There are also strip_tags() and htmlspecialchars() functions that strip out
or convert HTML and PHP tags. You might find one of those useful.

HTH

------------------------------------
Mike S
Copywriting for the IT Professional
www.redmaplesoftware.com
Feb 8 '06 #4
I currently do the addslashes before saving to the database.

Doing a simple echo of the $contents in the php script right after the
imap_fetchbody shows me that the Quoted-Printable are already present,
so it now becomes a problem of recognising that they are in there and
converting them accordingly.

Chris.

Feb 8 '06 #5
Dave,

Just putting this code in does the trick:
if (strtoupper($parts[$i]->encoding) == "4")
$content = imap_qprint($content);

All the Quoted Printable characters are converted.

Thanks.

Chris

Feb 8 '06 #6
d
<ch**************@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Dave,

Just putting this code in does the trick:
if (strtoupper($parts[$i]->encoding) == "4")
$content = imap_qprint($content);

All the Quoted Printable characters are converted.

Thanks.

Chris


Nice!

dave
Feb 9 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: kyrbi | last post by:
hi, using flash and php script I try to send mail from my website (linux hosting) The script is working but if I enable the marked lines (//....) the script doesn't want to send mail ..... ...
7
by: BenignVanilla | last post by:
My ISP provides a web based email client, but it is not brandable and the features are not that extensive. I'd like to build my own. Has anyone done this, or is anyone aware of any tools out there...
117
by: Steevo | last post by:
Any suggestions as to the best programs for cloaking email addresses? Many thanks -- Steevo
4
by: chris_fieldhouse | last post by:
Hi, I'm almost done with a php driven email filter and automated forwarder, I've tested it out with various emails and ironed out plain text and html. But this final item has me stumped. ...
40
by: apprentice | last post by:
Hello, I'm writing an class library that I imagine people from different countries might be interested in using, so I'm considering what needs to be provided to support foreign languages,...
4
by: Christoph Haas | last post by:
Hello, everyone... I'm trying to send an email to people with non-ASCII characters in their names. A recpient's address may look like: "Jörg Nørgens" <joerg@nowhere> My example code: ...
3
by: Laangen_LU | last post by:
Dear Group, my first post to this group, so if I'm on the wrong group, my apologies. I'm trying to send out an email in Chinese lanuage using the mail() function in PHP. Subject and...
7
by: erikcw | last post by:
Hi all, I'm trying to extract zip file (containing an xml file) from an email so I can process it. But I'm running up against some brick walls. I've been googling and reading all afternoon, and...
16
by: Ed Bitzer | last post by:
Trying to send groups of email with program using System.Net.Mail. I do not clear MailMessage but repeatedly loop changing only the Bcc entries. Works fine if all addresses are valid. As a simple...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.