imap_open show message body as plain text

php

Solution

try `$msg = imap_fetchbody($inbox,$email_number,1.1);` instead of `$msg = imap_fetchbody($inbox,$email_number,1.2);`

found some more information on this topic on http://php.net/manual/en/function.imap-fetchbody.php#89002:

imap-fetchbody() will decode attached email messages inline with the rest of the email parts, however the way it works when handling attached email messages is inconsistent with the main email message.

With an email message that only has a text body and does not have any mime attachments, imap-fetchbody() will return the following for each requested part number:

- (empty) - Entire message

- 0 Message header

- 1 - Body text

With an email message that is a multi-part message in MIME format, and contains the message text in plain text and HTML, and has a file.ext attachment, imap-fetchbody() will return something like the following for each requested part number:

- (empty) - Entire message

- 0 Message header

- 1 MULTIPART/ALTERNATIVE

- 1.1 - TEXT/PLAIN

- 1.2 - TEXT/HTML 2 - file.ext

Now if you attach the above email to an email with the message text in plain text and HTML, imap_fetchbody() will use this type of part number system:

- (empty) - Entire message

- 0 - Message header

- 1 - MULTIPART/ALTERNATIVE

- 1.1 - TEXT/PLAIN

- 1.2 - TEXT/HTML

- 2 - MESSAGE/RFC822 (entire attached message)

- 2.0 - Attached message header

- 2.1 - TEXT/PLAIN

- 2.2 - TEXT/HTML

- 2.3 - file.ext

Problem

I am using imap_open in PHP but the message body is showing like: ``` <html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:w=\"urn:schemas-microsoft-com:office:word\" xmlns:m=\"http://schemas.microsoft.com/office/2004/12/omml\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=us-ascii\"> <meta name=\"Generator\" content=\"Microsoft Word 14 (filtered medium)\"> <!--[if !mso]><style>v\\:* {behavior:url(#default#VML);} o\\:* {behavior:url(#default#VML);} w\\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} </style><![endif]--><style><!-- /* Font Definitions */ @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4;} @font-face {font-family:Tahoma; panose-1:2 11 6 4 3 5 4 4 2 4;} @font-face {font-family:Verdana; panose-1:2 11 6 4 3 5 4 4 2 4;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {margin:0cm; margin-bottom:.0001pt; font-size:12.0pt; font-family:\"Times New Roman\",\"serif\";} a:link, span.MsoHyperlink {mso-style-priority:99; color:blue; text-decoration:underline;} a:visited, span.MsoHyperlinkFollowed {mso-style-priority:99; color:purple; text-decoration:underline;} p.MsoAcetate, li.MsoAcetate, div.MsoAcetate {mso-style-priority:99; mso-style-link:\"Balloon Text Char\"; margin:0cm; margin-bottom:.0001pt; font-size:8.0pt; font-family:\"Tahoma\",\"sans-serif\";} span.EmailStyle18 {mso-style-type:personal-reply; font-family:\"Calibri\",\"sans-serif\"; color:#1F497D;} span.BalloonTextChar {mso-style-name:\"Balloon Text Char\"; mso-style-priority:99; mso-style-link:\"Balloon Text\"; font-family:\"Tahoma\",\"sans-serif\";} .MsoChpDefault {mso-style-type:export-only; font-size:10.0pt;} @page WordSection1 {size:612.0pt 792.0pt; margin:72.0pt 72.0pt 72.0pt 72.0pt;} div.WordSection1 {page:WordSection1;} --></style> ``` ...this continues here is the code i am using. ``` //try to connect $inbox = imap_open($hostname,$username,$password) or die('Cannot connect: ' . imap_last_error()); //grab emails $emails = imap_search($inbox,'ALL'); //if emails are returned, cycle through each... if($emails) { //put the newest emails on top rsort($emails); //for every email... foreach($emails as $email_number) { //get information specific to this email $header=imap_headerinfo($inbox,$email_number); $structure = imap_fetchstructure($inbox,$email_number); $from = $header->from[0]->mailbox."@".$header->from[0]->host; $toaddress = array_shift($header->to); $toaddress->mailbox."@".$toaddress->host; $replyto=$header->reply_to[0]->mailbox."@".$header->reply_to[0]->host; $datetime=date("Y-m-d H:i:s",$header->udate); $subject=$header->subject; //get message body $message = imap_fetchbody($inbox, $email_number, 1.2); if(base64_decode($message, true)) { //message body if base64 encoded $message = base64_decode($message); } else { //message body is not base64 encoded $message = $message = imap_fetchbody($inbox, $email_number, 1); } $message = strip_tags($message); } } ``` I have added the if statements to see if the message body is base64 encoded and if it is to decode it but it still displays as above sometimes. Other times it is showing as plain text but with huge line breaks. Is there a way i make the whole message body plain text?

Original source