PHP IMAP 交换问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5815183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:34:44  来源:igfitidea点击:

PHP IMAP Exchange Issue

phpexchange-serverimap

提问by Afrosimon

So I have a little problem with a PHP script I'm currently writing. To start off, let me say the script is supposed to connect to an IMAP mailbox, search for some emails and download their attachments. All of this is already coded and is working with my own gmail account. The problem arise when I try and connect to an exchange server. Short code excerpt :

所以我目前正在编写的 PHP 脚本有一个小问题。首先,让我说脚本应该连接到 IMAP 邮箱,搜索一些电子邮件并下载它们的附件。所有这些都已经编码并且正在使用我自己的 gmail 帐户。当我尝试连接到交换服务器时出现问题。短代码摘录:

$mbox = imap_open($host, $login, $password);
echo '<br/>' . imap_last_error() . '<br/>';
$emails = imap_search($mbox, 'FROM "[email protected]"', SE_UID);

I have tried two main $host "version" (with and without SSL) :

我尝试了两个主要的 $host “版本”(有和没有 SSL):

1 - {server:993/imap/ssl/novalidate-cert}INBOX 2 - {server:143/imap/novalidate-cert}INBOX

1 - {server:993/imap/ssl/novalidate-cert}收件箱 2 - {server:143/imap/novalidate-cert}收件箱

The novalidate-cert deal with a certificate error. I also tried the "notsl" parameters, for both of these, without any noticeable outcome. The error I get is this lovely message, absolutely not cryptic in any way, shape or form :

novalidate-cert 处理证书错误。我还尝试了“notsl”参数,对于这两个参数,没有任何明显的结果。我得到的错误是这个可爱的消息,绝对不是以任何方式、形状或形式神秘:

[CLOSED] IMAP connection broken (server response)

Additionally, I also receive these notices :

此外,我还收到以下通知:

Notice: Unknown: Unknown GSSAPI failure: An invalid name was supplied (errflg=1) in Unknown on line 0 
Notice: Unknown: GSSAPI mechanism status: Hostname cannot be canonicalized (errflg=1) in Unknown on line 0 
Notice: Unknown: Retrying PLAIN authentication after AUTHENTICATE failed. (errflg=1) in Unknown on line 0 
Notice: Unknown: Retrying PLAIN authentication after AUTHENTICATE failed. (errflg=1) in Unknown on line 0 
Notice: Unknown: Can not authenticate to IMAP server: AUTHENTICATE failed. (errflg=2) in Unknown on line 0 
Notice: Unknown: [CLOSED] IMAP connection broken (server response) (errflg=1) in Unknown on line 0

The first two especially puzzle me... I did try this script on another server, to make sure the issue was not related to my local network. After a lot of googling around, I only got this : http://www.phpfreaks.com/forums/index.php?topic=190628.0which seems like a somewhat cumbersome fix.

前两个特别让我困惑......我确实在另一台服务器上尝试过这个脚本,以确保问题与我的本地网络无关。经过大量的谷歌搜索,我只得到了这个:http://www.phpfreaks.com/forums/index.php?topic=190628.0这似乎是一个有点麻烦的修复。

Any ideas?

有任何想法吗?

回答by Dave Lasley

I'm having this same issue, it looks like the errors are being generated because an Exchange server advertises authentication protocols that it does not support (http://vision.eng.shu.ac.uk/mmvlwiki/index.php/Exchange). It also seems like this issue is isolated to linux servers as I have no issues with the exact same code on a Windows box. This has been a longstanding issue and PHP was recently patched (v 5.3.2) to allow you to disable certain authentication protocols (http://php.net/manual/en/function.imap-open.php). The below code works intermittently for me:

我遇到了同样的问题,看起来错误是因为 Exchange 服务器宣传它不支持的身份验证协议(http://vision.eng.shu.ac.uk/mmvlwiki/index.php/Exchange)。这个问题似乎也与 linux 服务器隔离,因为我在 Windows 机器上使用完全相同的代码没有问题。这是一个长期存在的问题,最近对 PHP 进行了修补(v 5.3.2)以允许您禁用某些身份验证协议(http://php.net/manual/en/function.imap-open.php)。下面的代码间歇性地为我工作:

$this->inbox = imap_open("{server:993/imap/ssl/novalidate-cert}$inbox", 
                           $username, $password, NULL, 1, 
                           array('DISABLE_AUTHENTICATOR' => 'PLAIN')) or 
                   die(var_dump(imap_errors()));

This also works intermittently:

这也间歇性地工作:

$this->inbox = imap_open("{server:993/imap/ssl/novalidate-cert}$inbox", 
                           $username, $password, NULL, 1, 
                           array('DISABLE_AUTHENTICATOR' => 'GSSAPI')) or 
                   die(var_dump(imap_errors()));

SO I ghetto rigged this it does seem to work...although it has the potential for an endless loop/DOS attack on my company's exchange server but /care

所以我在贫民区操纵了它它似乎确实有效......尽管它有可能对我公司的交换服务器进行无限循环/DOS攻击,但是 /care

Hopefully there is a better solution, but this should help:

希望有更好的解决方案,但这应该会有所帮助:

$tryCnt = 0;

while(!is_resource($this->inbox)){

    $this->inbox = imap_open("{server.com:993/imap/ssl/novalidate-cert}$inbox", 
                               $username, $password, NULL, 1, 
                               array('DISABLE_AUTHENTICATOR' => 'GSSAPI'));
    $tryCnt ++;

    if(!is_resource($this->inbox)){

        $this->inbox = imap_open("{server.com:993/imap/ssl/novalidate-cert}$inbox", 
                                   $username, $password, NULL, 1, 
                                   array('DISABLE_AUTHENTICATOR' => 'PLAIN'));
        $tryCnt ++;

    }

    if($tryCnt > 20){

        echo "Cannot Connect To Exchange Server:<BR>";
        die(var_dump(imap_errors()));

    }    
}

回答by user1077774

I have a PHP script that connects to an OWA email server and brings back the content of an email using the imap_open PHP function. Using that content, it then creates a page in a MindTouch instance.

我有一个 PHP 脚本,它连接到 OWA 电子邮件服务器并使用 imap_open PHP 函数带回电子邮件的内容。然后使用该内容在 MindTouch 实例中创建一个页面。

All of this code works correctly, but the script was reporting the GSSAPI failure errors shown above. In my web results page, success was (correctly) reported, but the page also displayed the GSSAPI error messages.

所有这些代码都可以正常工作,但脚本报告了上面显示的 GSSAPI 失败错误。在我的网页结果页面中,成功报告(正确),但该页面还显示 GSSAPI 错误消息。

What I discovered in my code was that I was turning on error_reporting(E_ALL). When I changed the reporting level, the error message went away.

我在代码中发现我正在打开error_reporting(E_ALL). 当我更改报告级别时,错误消息消失了。

I know that the error is still there, and I don't know why. But, since all of my code is working correctly, I just wanted the error message to go away, because it was confusing my users.

我知道错误仍然存​​在,但我不知道为什么。但是,由于我的所有代码都正常工作,我只是希望错误消息消失,因为它让我的用户感到困惑。

Changing the reporting level to a lower one took care of that.

将报告级别更改为较低级别即可解决此问题。