We help IT Professionals succeed at work.

How do I strip or decode the header and footer from messages like this one?

jvutechnc used Ask the Experts™
The message is "test body".  My php script is connecting to an IMAP server and inserting the message into a database.  However, it's inserting what appears to be the MIME header and footer also.  I just want the message.  Thank you.    I'm not an expert in PHP, so any code examples you can provide would be great. e.g. mime decoder

------=_Part_61734_626075708.1278340795961 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable test body=20 John Vu=20 Help Desk Analyst=20 JanPak Inc.=20 office 704.892.0219=20 cell 704.621.0639=20 jvu@janpak.com=20 janpak.com =C2=A0 =E2=80=A2 green.janpak.com=20 Distinctive, Responsible Solutions=20 ------=_Part_61734_626075708.1278340795961 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable <= div style=3D'font-family: Times New Roman; font-size: 12pt; color: #000000'= >test body

John Vu
= Help Desk Analyst

JanPak Inc.
office 704.892.0219
cell 704.6= 21.0639

janpak.com =E2=80=A2 green.janpa= k.com
Distinctive, Responsible Solutions

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®


oh wait..I see that my PHP script is fetching the header information before inserting into the database. I probably should try to fetch the body only with something along the lines of:

imap_body() returns the body of the message, numbered msg_number in the current mailbox.
imap_body() will only return a verbatim copy of the message body. To extract single parts of a multipart MIME-encoded message you have to use imap_fetchstructure() to analyze its structure and imap_fetchbody() to extract a copy of a single body component.
where u go buddy... try this i think this is what your getting at.


Tells you how to remove the encruption/decode. it.
Most Valuable Expert 2011
Top Expert 2016

If you want to post the script you're using we may be able to help a little more.  But if all you needed was the imap_body() function, you may be home free by now ;-)


I found that I could use the following to fetch the body.  Thank you!

$body = (imap_fetchbody($inbox, $i, '1'));


Perhaps you can help me with the following.  The email gets inserted into mysql with code denoting line breaks or spaces.    =20    

What's going on?

Here's an example of the message that goes into mysql.   I've also included the script that takes the email from an IMAP server and insert the message into mysql.  The apache and mysql server is on Windows 2003.
test test test=20

John Smith=20
Programmer/web developer=20

Acme Inc.=20
705 Griffith St. Suite 300=20
Davidson, NC 28036=20

janpak.com =E2=80=A2=C2=A0=C2=A0 green.janpak.com=20
Distinctive, Responsible Solutions=20
	error_reporting(E_ALL ^ E_NOTICE);

function translate_object($obj){
 return $obj->mailbox.'@'.$obj->host;


/* connect to server */
	$hostname = '{}INBOX';
	$username = 'xxxx';
	$password = 'xxxx';

/* try to connect */
	$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to server: ' . imap_last_error());

/* grab emails */
	$emails = imap_search($inbox,'ALL');

	$headers = imap_headers($inbox);
	$numEmails = sizeof($headers);

	for($i = 1; $i < $numEmails+1; $i++)


$mailHeader = @imap_headerinfo($inbox, $i);

$from = $mailHeader->reply_toaddress;

$subject = ($mailHeader->subject);

$body = (imap_fetchbody($inbox, $i, '1'));

$date = $mailHeader->date;

$rows = "SELECT * FROM requests";
$result = mysql_query($rows);

$query = "INSERT INTO requests (`date`, `from`, `subject`, `message`) VALUES ('".date("DdM H:i")."','$from', '$subject', '".mysql_real_escape_string($body)."')";

$res = mysql_query($query);

/* close the connection */


Open in new window

Most Valuable Expert 2011
Top Expert 2016
Try printing out the $body with var_dump() to see what is in there.  You might want to just translate the =20 strings into PHP_EOL characters.