douyu1656 2019-04-28 08:40
浏览 116
已采纳

imap_mail_move()没有处理特殊字符(äüö......)

I am using imap_mail_move() to move emails from one folder to another. This works pretty well, but not if it comes to special characters in the folder name. I am sure I need to encode the name, but all test where not succesful.

Anybody that has a nice idea? Thanks in advance.

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;
    }

    [...]
}

Calling the function in the script

[...]
$uid = 1234;

$folderTarget1 = "INBOX.00_Korrespondenz";
$this->doMoveEmail($uid, $folderTarget1);

$folderTarget2 = "INBOX.01_Anmeldevorgang.011_Bestätigungslink";
$this->doMoveEmail($uid, $folderTarget2);
[...]

The execution of the first call (folderTarget1) is working pretty well.

The execution of the secound call (folderTarget2) is creating an error:

[TRYCREATE] Mailbox doesn't exist: INBOX.01_Anmeldevorgang.011_Bestätigungslink (0.001 + 0.000 secs).

Remark 1:

if I call imap_list(), the name of the folder is shown as

"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"

Remark 2

I checked each possible encoding, with the following script, but none of them matchs the value that is returned by 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条回答 默认 最新

  • dongnong3019 2019-04-28 14:46
    关注

    I created a workaround, which helps me to work with UTF8-values and to translate it to the original (raw) IMAP folder name.

        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() . "
    ");
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图