php imap回应奇怪的标题..我可以解决这个问题吗?

时间:2011-12-07 17:12:20

标签: php regex header imap gmail-imap

这是我的PHP代码,用于抓取Gmail电子邮件并将其回显到网页:

<?php


                            /* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'username@gmail.com';
$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);

                                ?>

我遇到的问题是我不断地将奇怪的标题与消息体一起脱口而出,我不知道为什么。我不能使用str替换或正则表达式,因为标题的数字部分每次生成不同,所以我真的需要一个可靠的方法来做到这一点。我没有想法,我需要帮助。  还得到一些随机的“=”符号

输出:

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

1 个答案:

答案 0 :(得分:1)

您看到的不是标题,因此它们是多部分邮件的MIME边界。您的邮件包含两种格式的相同数据 - HTML和纯文本。这对于电子邮件正文非常常见,您需要访问电子邮件的Content-Type:标题才能正确解析它。阅读this

This可以帮助您解析它并正确处理它,this可能会。