猿问

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

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

}


Qyouu
浏览 186回答 2
2回答

侃侃尔雅

我创建了一个变通办法,它可以帮助我使用UTF8值并将其转换为原始(原始)IMAP文件夹名称。&nbsp; &nbsp; function getFolderList() {&nbsp; &nbsp; &nbsp; &nbsp; $folders = imap_list($this->conn, "{".$this->server."}", "*");&nbsp; &nbsp; &nbsp; &nbsp; if (is_array($folders)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Remove Server details of each element of array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $folders = array_map(function($val) { return str_replace("{".$this->server."}","",$val); }, $folders);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Sort array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; asort($folders);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Renumber the list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $folders = array_values($folders);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // add UTF-8 encoded value to array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // this is needed as the original value is so wiered, that it is not possible to encode it&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // with a function on the fly. This additional utf-8 value is needed to map the utf-8 value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // to the original value. The original value is still needed to do some operations like e.g.:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; - imap_mail_move()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; - imap_reopen()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ==> the trick is to use normalizer_normalize()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $return = array();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($folders as $key => $folder) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $return[$key]['original'] = $folder;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $return[$key]['utf8']&nbsp; &nbsp; &nbsp;= normalizer_normalize(mb_convert_encoding($folder,'UTF-8','UTF7-IMAP'));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $return;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; die("IMAP_Folder-List failed: " . imap_last_error() . "\n");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答