php imap - 获取正文并制作纯文本

标签 php mysql email

我正在使用 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/

相关文章:

php - 两个 PHP 实例(在 Apache 中)可以共享同一个 PDO 持久连接吗?

php - 如何有效地添加带有 GET 参数?或者 PHP 中的 & ?

php - Laravel 5如何检查用户是否存在于我的另一个表中

php - MySQL多对多选择

ios - 应用内截图并附加到电子邮件 IOS

ruby-on-rails - 验证电子邮件未通过Heroku/Mailgun发送

php - Codeigniter - join() 只返回一个 "first table"

mysql - 查找和处理重复用户

mysql - Google 云 SQL - CPU 为 100%

Java Mail - 一起搜索和排序减少数量的电子邮件