php - imap_fetchbody消息已编码

时间:2013-12-30 10:54:10

标签: php email imap

我正在构建一个网络应用,用户可以在其中看到发送到其帐户的电子邮件。我有一些关于邮件编码的问题。

作为测试,我刚收到一个网站的注册电子邮件。邮件包含HTML。

它应该是这样的:

Thanks for signing up to Virally, we're excited to have you using our product!  
  Start Capturing Leads with Virally!  
  Click here to make your first campaign >>  
  You'll need a digital reward to create a campaign, like an eBook, Whitepaper, Video, free book chapter, Podcast etc. (We can help with this if you don't have anything of your own, just ask)  
  Please hit reply to this email if you'd like some help getting started.   
Thanks,  
 Liam Gooding  liam@virallyapp.com  Virally Co-founder    

这就是它在我的收件箱中的样子;虽然,在我的网站上它看起来像这样:

VGhhbmtzIGZvciBzaWduaW5nIHVwIHRvIFZpcmFsbHksIHdlJ3JlIGV4Y2l0ZWQgdG8gaGF2ZSB5
b3UgdXNpbmcgb3VyIHByb2R1Y3QhwqAgCiAgU3RhcnQgQ2FwdHVyaW5nIExlYWRzIHdpdGggVmly
YWxseSEgIAogIENsaWNrIGhlcmUgdG8gbWFrZSB5b3VyIGZpcnN0IGNhbXBhaWduICZndDsmZ3Q7
ICAKICBZb3UnbGwgbmVlZCBhIGRpZ2l0YWwgcmV3YXJkIHRvIGNyZWF0ZSBhIGNhbXBhaWduLCBs
aWtlIGFuIGVCb29rLCBXaGl0ZXBhcGVyLCBWaWRlbywgZnJlZSBib29rIGNoYXB0ZXIsIFBvZGNh
c3QgZXRjLiAoV2UgY2FuIGhlbHAgd2l0aCB0aGlzIGlmIHlvdSBkb24ndCBoYXZlIGFueXRoaW5n
IG9mIHlvdXIgb3duLCBqdXN0IGFzaykgIAogIFBsZWFzZSBoaXQgcmVwbHkgdG8gdGhpcyBlbWFp
bCBpZiB5b3UnZCBsaWtlIHNvbWUgaGVscCBnZXR0aW5nIHN0YXJ0ZWQuICAgClRoYW5rcywgIAog
TGlhbSBHb29kaW5nICBsaWFtQHZpcmFsbHlhcHAuY29tICBWaXJhbGx5IENvLWZvdW5kZXIgICAg

这是我的PHP代码:

$openmail = imap_open($dns,$email,$password ) or die("Cannot Connect ".imap_last_error());

if ($openmail) {
  echo  "<div class='noti success'>You have ".imap_num_msg($openmail). " messages in your inbox</div>\n\r";
  $tot=imap_num_msg($openmail);
  for($i=$tot;$i>0;$i--) {

    $header = imap_header($openmail,$i);
    echo "<br>";
    echo $header->Subject." (".$header->Date.")";
    $body = imap_fetchbody($openmail, $i,'1');

    echo "<div class='faq-tile'>$body</div>";
  }
  imap_close($openmail);
}

$body变量是打印出邮件正文的变量。

1 个答案:

答案 0 :(得分:2)

在这个例子中,您从imap_fetchbody获得的内容是base64:

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

if (preg_match('/^([a-zA-Z0-9]{76} )+[a-zA-Z0-9]{76}$/', $body)) {
    $body = base64_decode($body);
}

echo "<div class='faq-tile'>$body</div>";

您还应该阅读Can't fetch plain text mails content using IMAP