Reputation: 1804
Here is my PHP Code for grabbing gmail emails and echoing them to a webpage:
<?php
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = '[email protected]';
$password = 'password';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox,'ALL');
/* if emails are returned, cycle through each... */
if($emails) {
/* begin output var */
$output = '';
/* put the newest emails on top */
rsort($emails);
/* for every email... */
foreach($emails as $email_number) {
/* get information specific to this email */
$overview = imap_fetch_overview($inbox, $email_number, 0);
$message = imap_fetchbody($inbox, $email_number, 1);
$DateFormatted = str_replace("-0500", "", $overview[0] -> date);
/* output the email header information */
$output .= $overview[0] -> subject ;
$output .= $DateFormatted ;
//$bodyFormatted = preg_replace("/This e-mail(.*)/","",$message);
//$bodyFormatted = preg_replace("/Ce courriel(.*)/","",$bodyFormatted);
/* output the email body */
$output .= $message;
}
echo $output;
}
/* close the connection */
imap_close($inbox);
?>
The problem I am having is that I keep getting weird headers blurted out along with the message body and I dont know why. I cant use str replace or Regex because the numerical part of the headers is generated different each time so I really need a reliable way of doing this. I am out of ideas and I need help. Also getting some random "=" signs
Output:
SiteA Connectivity Issues (resolved)Tue, 6 Dec 2011 13:59:59 **------_=_NextPart_001_01CCB449.406E7C50 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: quoted-printable =20** Please be advised www.siteA.com is no longer experiencing issues. The site is up and fully operational once again. =20 IMPACT: www.siteA.com was experiencing connectivity issues.=20 =20 UPDATE: Connectivity issues have been resolved. Root cause analysis and reporting will be performed tomorrow during business hours. **=20 ------_=_NextPart_001_01CCB449.406E7C50 Content-Type: text/html; charset="US-ASCII" Content-Transfer-Encoding: quoted-printable**
Please be advised www.siteA.com is no longer = experiencing issues. The site is up and fully operational once = again.
IMPACT:
www.siteA.com was experiencing connectivity issues.
UPDATE:
Connectivity issues have been = resolved. Root cause analysis and reporting will be performed tomorrow during = business hours.
**------_=_NextPart_001_01CCB449.406E7C50--**
Upvotes: 0
Views: 298
Reputation: 88657
What you are seeing are not headers, as such, they are MIME boundaries for a multipart message. Your message contains the same data in two formats - HTML and plain text. This is very common for email bodies, you will need access to the Content-Type:
header of the email to parse it correctly. Read this.
This may help you parse it and deal with it correctly, as may this.
Upvotes: 1