open email by php using imap giving me this error

2019-09-06 12:01发布

问题:

I am trying to open the email messages by php using imap method but it give me an error Couldn't open stream {imap.gmail.com:993/imap/ssl}INBOX and this is my code

$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'nohamedtemraz92@gmail.com';
$password = '0129799169mohamed';

/* try to connect */
$inbox = imap_open($hostname,$username ,$password) or die('Cannot connect to Gmail: '        imap_last_error());

    /* grab emails */
    $emails = imap_search($inbox,'ALL');

    /* if emails are returned, cycle through each... */
    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);


            $output.= 'Name:  '.$overview[0]->from.'</br>';
                $output.= 'Email:  '.$overview[0]->message_id.'</br>';



        }

        echo $output;
    } 

 /* close the connection */
imap_close($inbox);

回答1:

Try this:

$hostname = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';
$inbox = imap_open($hostname,$username ,$password) or die('Cannot connect to Gmail: '        imap_last_error());


回答2:

{imap.gmail.com:993/imap/ssl}INBOX

working for me but you have to enable 'Access for less secure apps' on account security or you can try live email account