如何读取内容类型头并将其转换为UTF-8,当Gmail IMAP使用UTF8而Outlook使用ISO-8859-7时?

10

因此,我使用imap从Gmail和Outlook收取电子邮件。

Gmail的编码方式如下=?UTF-8?B?UmU6IM69zq3OvyDOtc68zrHOuc67IG5ldyBlbWFpbA==?=, 而Outlook的编码方式如下=?iso-8859-7?B?UmU6IOXr6+ft6er8IHN1YmplY3Q=?=

不幸的是,我尚未找到任何解决方案,可以帮助我将其转换为可读文本。 相反,我正在尝试以下方法:

mb_convert_encoding($body, "UTF-8", "UTF-8"); 

mb_convert_encoding($body, "UTF-8", "iso-8859-7");

但我正在努力寻找解决此问题的方案。

这是我打开我的帐户IMAP的方法(其中包含大量的gmail和outlook消息)

$hostname = '{imappro.zoho.com:993/imap/ssl}INBOX';
$username = 'email@email.com';
$password = 'password';


/* try to connect */
$inbox = imap_open($hostname,$username ,$password) or die('Cannot connect to Zoho: ' . imap_last_error());

/* grab emails */
$emails = imap_search($inbox,'UNSEEN');

需要帮助吗?


那些不是正文编码,而是标题编码。您需要读取内容类型标题或解析结构响应。 - Max
@Max,你能否给我推荐一份指南或者其他资料吗? - EnexoOnoma
请查看修改后的内容,让我知道是否解决了您的问题。 - Altimus Prime
5个回答

5

很遗憾,我还没有找到任何解决方案,可以帮助我将此转换为可读文本。

解决方案 您的字符串是base64编码的。

=?UTF-8?B?UmU6IM69zq3OvyDOtc68zrHOuc67IG5ldyBlbWFpbA==?=

echo base64_decode('UmU6IM69zq3OvyDOtc68zrHOuc67IG5ldyBlbWFpbA==');

打印出"Re: νέο εμαιλ new email"

=?iso-8859-7?B?UmU6IOXr6+ft6er8IHN1YmplY3Q=?=

会输出什么内容?

echo base64_decode('UmU6IOXr6+ft6er8IHN1YmplY3Q=');

打印出 "Re: subject"。
答案是使用 base64_decode 与您当前的解决方案相结合。
识别 base64 编码文本的方法是,它由字母 a-z、A-Z、数字0-9以及两个其他字符(通常为+和/)组成,并且通常用=右填充。
编辑:
抱歉,我已经忘记了问题是将其从 iso-8859-7 转换为 UTF-8 并使其可见。
<?php
$str = base64_decode('UmU6IPP03evt+SDs3u317OE=');
$str = mb_convert_encoding($str,'UTF-8','iso-8859-7');
echo $str;
?>

结果是“Re:发送消息”。

但是这个怎么样?echo base64_decode('ZP3OUC66Z4ZOU86XZ4IGZP3OUC66ZR/OU86XZPDOTM63Z4I='); 它返回 d��P.�g�NSΗg�d��P.�e�SΗd��Lηg� - EnexoOnoma
并非所有的数据都是文本,也不是所有的文本数据都是每个字符一个字节。这是你遇到的例子中完整的字符串吗,还是只是一部分?另外,在你的回复中它之前的字符编码是什么? - Altimus Prime
这是一个类似的例子:=?iso-8859-7?B?UmU6IPP03evt+SDs3u317OE=?= - EnexoOnoma
如果您在浏览器中查看它,您需要设置字符集标题,例如 header('Content-Type: text/html; charset=iso-8859-7'); - Altimus Prime
我之前做了一个错误的笔记,认为它是阿拉伯语,但实际上我认为它是希腊语。 - Altimus Prime
我有点认为,既然你已经知道如何使用mb_convert_encoding,那么base64就是你所需要知道的全部了。 - Altimus Prime

2
这里
   /* connect to gmail */
    $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
    $username = 'davidwalshblog@gmail.com';
    $password = 'davidwalsh';

    /* 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,2);

            /* output the email header information */
            $output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
            $output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
            $output.= '<span class="from">'.$overview[0]->from.'</span>';
            $output.= '<span class="date">on '.$overview[0]->date.'</span>';
            $output.= '</div>';

            /* output the email body */
            $output.= '<div class="body">'.$message.'</div>';
        }

        echo $output;
    } 

    /* close the connection */
    imap_close($inbox);

阅读和解码请点击这里
<?php
$hostname = '{********:993/imap/ssl}INBOX';
$username = '*********';
$password = '******';

$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to server: ' . imap_last_error());

$emails = imap_search($inbox,'ALL');

if($emails) {
    $output = '';
    rsort($emails);

    foreach($emails as $email_number) {
        $overview = imap_fetch_overview($inbox,$email_number,0);
        $structure = imap_fetchstructure($inbox, $email_number);

        if(isset($structure->parts) && is_array($structure->parts) && isset($structure->parts[1])) {
            $part = $structure->parts[1];
            $message = imap_fetchbody($inbox,$email_number,2);

            if($part->encoding == 3) {
                $message = imap_base64($message);
            } else if($part->encoding == 1) {
                $message = imap_8bit($message);
            } else {
                $message = imap_qprint($message);
            }
        }

        $output.= '<div class="toggle'.($overview[0]->seen ? 'read' : 'unread').'">';
        $output.= '<span class="from">From: '.utf8_decode(imap_utf8($overview[0]->from)).'</span>';
        $output.= '<span class="date">on '.utf8_decode(imap_utf8($overview[0]->date)).'</span>';
        $output.= '<br /><span class="subject">Subject('.$part->encoding.'): '.utf8_decode(imap_utf8($overview[0]->subject)).'</span> ';
        $output.= '</div>';

        $output.= '<div class="body">'.$message.'</div><hr />';
    }

    echo $output;
}

imap_close($inbox);
?>

在这里查看关于电子邮件结构和提取功能的优秀教程。here

0

我有一个任务,需要从特定的邮箱接收信件,解析它们并索引某些内容。

我想要一些微服务来提供数据。

  1. 下载所需内容
  2. 将接收到的数据转换为可读格式
  3. 处理内容

因此,我决定使用现成的工具。

  1. 用于获取电子邮件的脚本 - imap2maildir
  2. 用于处理消息的Unix客户端mu
  3. dos2unix 转换器

接下来,我编写了一个小的bash脚本,将其放置在cron中。

#!/bin/bash
python /var/mail_dump/imap2maildir/imap2maildir -c /var/mail_dump/imap2maildir/deploy.conf
mu index --maildir=/var/mail_dump/dumps/new
#clean old data
rm -rf /var/mail_dump/extract/*

#search match messages
mu find jivo --fields="l" --nocolor | xargs $1 cp -t /var/mail_dump/extract
#converting
dos2unix -f /var/mail_dump/extract/*

#reassembly of messages in html
cd /var/mail_dump/extract/
for i in /var/mail_dump/extract/*
do
  mu extract --parts=0 --overwrite "$i"
  rm "$i"
done

完成! 我有一个服务,不断地接收电子邮件并准备它们进行处理。 PHP可以使用准备好的数据而无需考虑低级逻辑的实现。


0
如果您想解码标题元素,可以使用PHP函数imap_mime_header_decode()
此外,您需要一些MIME解析器类来解码多部分消息。

0
要获取标题,您需要将流($inbox)传递给imap_headers()。响应中可以获得许多值,完整列表:imap_headerinfo 对于实际的消息,可以使用imap_body()读取纯文本,传递流和您想要的消息编号(在搜索后的$emails中)。获取HTML / 多部分电子邮件有点棘手。首先,您需要imap_fetchstructure()来识别消息的各个部分,然后使用imap_fetchbody()获取您感兴趣的部分。
一旦您从imap_fetchbody()获得结果,如果仍然需要调整编码,则可以在此时完成。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接