Connecting to Gmail IMAP PHP “Couldn't open stream”

前端 未结 7 1790
别跟我提以往
别跟我提以往 2021-01-11 12:12

There are lots of people having similar issues but no one is answering their questions. I have IMAP enabled in PHP, Using all the correct information. I don\'t see where I\'

相关标签:
7条回答
  • 2021-01-11 12:34

    I had the same error and found a different solution. I have added debug info into host:

    "{imap.gmail.com:993/debug/imap/ssl/novalidate-cert}INBOX"; 
    

    When I read php error log, I found

    Unknown: [ALERT] Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure) (errflg=1) in Unknown on line 0
    

    Open link, and follow instructions. Search for

    Your app might not support the latest security standards. Try changing a few settings to allow less secure apps access to your account.

    Click on link and enable less secure app access.

    Then it works for me.

    0 讨论(0)
  • 2021-01-11 12:40

    I think Gmail's IMAP can only be accessed on port 993.

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

    0 讨论(0)
  • 2021-01-11 12:46

    You can try the following code by giving a notls argument and connecting the server like follows, if SSL is not applied.

    $hostname = '{imap.YOUR_DOMAIN.com:143/imap/notls}INBOX'; 
    $username = 'YOUR_USERNAME';
    $password = 'YOUR_PASSWORD';
    
    $inbox = imap_open($hostname, $username, $password) or die('Cannot connect to Gmail: ' . imap_last_error());
    

    If it will not throw any error, this means that you are succesfully connected to server then. I hope this helps.

    0 讨论(0)
  • 2021-01-11 12:53

    You can setup 2 step authentication and then assign an APP password to use in your requests (just replace your password with the one provided for the app, your normal password doesn't change.).

    This will help your script run from any host without google blocking it (due to a change in login location).

    0 讨论(0)
  • 2021-01-11 12:54

    You need port 993, the SSL IMAP port.

    Port 995 is the SSL POP3 port.

    0 讨论(0)
  • 2021-01-11 12:55

    I had the same issue and I fixed it by unlocking the Google captcha.

    Go to the following URL while signed in to the Google account : https://accounts.google.com/DisplayUnlockCaptcha

    And click on the button.

    Of course, you already had activated the access for less secure apps in the Security tab of your Google account settings ;).

    It should work now !

    0 讨论(0)
提交回复
热议问题