突然的 PHP 错误“警告:imap_open() [function.imap-open]:无法打开流”


Sudden PHP Error "Warning: imap_open() [function.imap-open]: Couldn't open stream"

好的,这段代码''运行良好',然后我开始使用它,以便让其他人连接到他们的电子邮件,并且由于各种拼写错误等,在此过程中遇到了一些打开流错误。

自从这样做以来,突然间我根本无法连接到我的电子邮件?不久前,我使用了完全相同的连接代码,然后浏览了我的收件箱。

我总是收到"警告:imap_open() [function.imap-open]:无法打开流"错误。

这很奇怪,因为我使用与以前完全相同的代码,但是自从遇到错误以来,我现在无法连接任何代码。回应也需要很长时间。

这是代码:

$mailbox = imap_open('{mail.artisancodesmith.com:143/notls}INBOX', 'admin@artisancodesmith.com', 'PASSWORD');
if ($mailbox) {
    $response = "MAIL MENU:<br>
        inbox: View your inbox.<br>
        compose: Compose an e-mail.<br>
        setup: Set your e-mail account's settings.";
    $next = "iorcmail";
}

注意:PHP 页面正在连接到同一服务器上的电子邮件。

更新:如果我将"mail.artisancodesmith.com"替换为"本地主机",它会再次工作!我最好使用我的实际 IMAP 主机 - 我想我会看看它是否会在将来的某个时候再次工作。感谢所有帮助过的人。:)

请使用

以下代码成功连接,

$hostname = "{imap.gmail.com:993/imap/ssl/novalidate-cert}";
$mailbox = imap_open($hostname, 'admin@artisancodesmith.com', 'PASSWORD');
if ($mailbox) 
{
    // do work....
}

我遇到了这个问题,这是我解决它的方法;

  • 登录到您的 gmail 帐户,在"设置"->"转发"和"POP/IMAP"下,>启用 IMAP。
  • 转到 https://www.google.com/settings/security/lesssecureapps 并选择"打开"
  • 转到:https://accounts.google.com/b/0/DisplayUnlockCaptcha 并启用访问权限。

在此之后,上面的代码对我有用....

我已经尝试过了,对我来说效果很好

$inbox = imap_open("{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox", 'username', 'password')
             or die('Cannot connect to Gmail: ' . imap_last_error());
$emails = imap_search($inbox,'All');
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);
        $header =  imap_header($inbox, $email_number); 
        echo "<h1>data</h1>";
        echo "<pre>";print_r($message);
        echo "<h1>Message</h1>";
        echo "<pre>";print_r($message);
        echo "<h1>header</h1>";
        echo "<pre>";print_r($message);
        $overview[0]->seen;
        $overview[0]->subject;
        $overview[0]->from;
        $overview[0]->date;
    }
} 
/* close the connection */
imap_close($inbox);

也许你在代理后面? 如果是这样,我想你需要对它进行身份验证......

resource imap_open(
    string $mailbox , 
    string $username , 
    string $password [, 
    int $options = 0 [, 
    int $n_retries = 0 [,  
    array $params = NULL ]]] 
)