为了账号安全,请及时绑定邮箱和手机立即绑定

imap_mail_move()无法使用特殊字符(äüö…)

imap_mail_move()无法使用特殊字符(äüö…)

PHP
Qyouu 2021-05-10 13:22:56
我正在使用imap_mail_move()将电子邮件从一个文件夹移动到另一个文件夹。这种方法效果很好,但是如果文件夹名称中包含特殊字符,则效果不佳。我确定我需要对名称进行编码,但是所有测试都无法成功进行。有人有个好主意吗?提前致谢。class EmailReader {    [...]    function doMoveEmail($uid, $targetFolder) {        $targetFolder = imap_utf8_to_mutf7($targetFolder);        $return = imap_mail_move($this->conn, $uid, $targetFolder, CP_UID);        if (!$return) {            $this->printValue(imap_errors());           die("stop");        }        return $return;    }    [...]}在脚本中调用函数[...]$uid = 1234;$folderTarget1 = "INBOX.00_Korrespondenz";$this->doMoveEmail($uid, $folderTarget1);$folderTarget2 = "INBOX.01_Anmeldevorgang.011_Bestätigungslink";$this->doMoveEmail($uid, $folderTarget2);[...]第一个调用(folderTarget1)的执行效果很好。secound调用(folderTarget2)的执行产生了一个错误:[TRYCREATE] Mailbox doesn't exist: INBOX.01_Anmeldevorgang.011_Bestätigungslink (0.001 + 0.000 secs).备注1:如果我调用imap_list(),该文件夹的名称将显示为"INBOX.01_Anmeldevorgang.011_Besta&Awg-tigungslink" (=$val)using: $new = mb_convert_encoding($val,'UTF-8','UTF7-IMAP')echo $new; // gives --> "INBOX.01_Anmeldevorgang.011_Bestätigungslink"but:$new2 = mb_convert_encoding($new,'UTF7-IMAP', 'UTF-8')echo $new2; // gives --> "INBOX.01_Anmeldevorgang.011_Best&AOQ-tigungslink"备注2我使用以下脚本检查了每种可能的编码,但没有一个与imap_list()返回的值匹配。// looking for "INBOX.01_Anmeldevorgang.011_Besta&Awg-tigungslink" given by imap_list().$targetFolder = "INBOX.01_Anmeldevorgang.011_Bestätigungslink";foreach(mb_list_encodings() as $chr){  echo mb_convert_encoding($targetFolder, $chr, 'UTF-8')." : ".$chr."<br>";}
查看完整描述

2 回答

?
侃侃尔雅

TA贡献1801条经验 获得超16个赞

我创建了一个变通办法,它可以帮助我使用UTF8值并将其转换为原始(原始)IMAP文件夹名称。


    function getFolderList() {

        $folders = imap_list($this->conn, "{".$this->server."}", "*");

        if (is_array($folders)) {


            // Remove Server details of each element of array

            $folders = array_map(function($val) { return str_replace("{".$this->server."}","",$val); }, $folders);


            // Sort array

            asort($folders);


            // Renumber the list

            $folders = array_values($folders);


            // add UTF-8 encoded value to array

            // this is needed as the original value is so wiered, that it is not possible to encode it

            // with a function on the fly. This additional utf-8 value is needed to map the utf-8 value

            // to the original value. The original value is still needed to do some operations like e.g.:

            //  - imap_mail_move()

            //  - imap_reopen()

            // ==> the trick is to use normalizer_normalize()

            $return = array();

            foreach ($folders as $key => $folder) {

                $return[$key]['original'] = $folder;

                $return[$key]['utf8']     = normalizer_normalize(mb_convert_encoding($folder,'UTF-8','UTF7-IMAP'));

            }



            return $return;

        } else {

            die("IMAP_Folder-List failed: " . imap_last_error() . "\n");

        }

    }


查看完整回答
反对 回复 2021-05-21
  • 2 回答
  • 0 关注
  • 185 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信