Sudden PHP Error “Warning: imap_open() [function.i

2019-02-25 03:41发布

问题:

Okay, this code 'was' working perfectly and then I started playing around with it in order to let others connect to their e-mails and as you do ran into a few open stream errors along the way due to various typos and such.

Since doing this, all of a sudden I can't connect to my e-mail at all? A short while back I was using the exact same connection code and then browsing my inbox.

I always get the "Warning: imap_open() [function.imap-open]: Couldn't open stream" error.

This is strange as I'm using the exact same code as before, but since bumping into errors I can't connect whatsoever now. It also takes ages to respond.

Here is the code:

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

NOTE: The PHP page is connecting to the e-mail on the same server.

UPDATE: If I replace "mail.artisancodesmith.com" with "localhost" it works again! I would preferably like to use my actual IMAP host - I'll see if it works again some time in the future I guess. Thanks to all who helped. :)

回答1:

Please use the below code to connect successfully,

$hostname = "{imap.gmail.com:993/imap/ssl/novalidate-cert}";

$mailbox = imap_open($hostname, 'admin@artisancodesmith.com', 'PASSWORD');

if ($mailbox) 
{
    // do work....
}


回答2:

I ran into this problem and here is how I solved it;

  • Login to your gmail account, Under Settings -> Forwarding and POP/IMAP -> Enable Imap.
  • Go to https://www.google.com/settings/security/lesssecureapps and select "Turn On"
  • Go to: https://accounts.google.com/b/0/DisplayUnlockCaptcha and enable access.

after this, above code works for me....



回答3:

I have tried this an works perfectly fine for me

$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);


回答4:

maybe are you behind a proxy? if so, I guess you need to auth against it...



回答5:

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


标签: php imap