我正在使用 PHP imap 函数从 POP3 邮箱获取电子邮件并将数据插入 MySQL 数据库。
这是 PHP 代码:
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect: ' . imap_last_error());
$emails = imap_search($inbox,'ALL');
if($emails)
{
$output = '';
rsort($emails);
foreach($emails as $email_number)
{
$header=imap_headerinfo($inbox,$email_number);
$from = $header->from[0]->mailbox . "@" . $header->from[0]->host;
$toaddress=$header->toaddress;
$replyto=$header->reply_to[0]->mailbox."@".$header->reply_to[0]->host;
$datetime=date("Y-m-d H:i:s",$header->udate);
$subject=$header->subject;
//remove the " from the $toaddress
$toaddress = str_replace('"','',$toaddress);
echo '<strong>To:</strong> '.$toaddress.'<br>';
echo '<strong>From:</strong> '.$from.'<br>';
echo '<strong>Subject:</strong> '.$subject.'<br>';
//get message body
$message = (imap_fetchbody($inbox,$email_number,1.1));
if($message == '')
{
$message = (imap_fetchbody($inbox,$email_number,1));
}
}
它工作正常,但是在正文中的一些电子邮件中,我在单词之间得到 =
,或者在单词之间得到 =20
。有时,即使发送时并非空白,电子邮件也只是空白。
只有来自某些电子邮件时才会发生这种情况。
我怎样才能绕过这个问题,让电子邮件完全是纯文本?
最佳答案
发生这种情况是因为电子邮件通常是 Quoted-printable编码。 =
是一个软换行符,=20
是一个空格。我想,你可以使用 quoted_printable_decode()在消息上,以便正确显示。关于空白电子邮件,我不知道,我需要更多详细信息。
基本上:
//get message body
$message = quoted_printable_decode(imap_fetchbody($inbox,$email_number,1.1));
关于php imap - 获取正文并制作纯文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19092161/