Search code examples
phpemailgmail-imapserve

imap php not working


I am trying to run imap_open() in on my server but its giving me this error.

Warning: imap_open(): Couldn't open stream {imap.gmail.com:993/imap/ssl}INBOX in /home/bestinsaudi2/public_html/mailbox.php on line 55 Cannot connect to Gmail: Can not authenticate to IMAP server: [ALERT] Please log in via your web browser: https://support.google.com/mail/acco

I have enabled imap in gmail account i am using .I also enabled less secure apps. I copied code from david walsh . His credentials is giving this error.

Warning: imap_open(): Couldn't open stream {imap.gmail.com:993/imap/ssl}INBOX in /home/bestinsaudi2/public_html/mailbox.php on line 55 Cannot connect to Gmail: Can not authenticate to IMAP server: [AUTHENTICATIONFAILED] Invalid credentials (Failure)

I am writing down the code i am using with my own credentials.

$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = '[email protected]';
$password = 'mypassword';

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

here is one more thing i am not using wamp or xamp server i am using ipower. Somewhere i found to run command like $ composer require secit-pl/imap-bundle And its confusing .How to and where to run this and is it necessary with my code? Also do i have to download any library for this ? or should some additional step?


Solution

  • If less secure apps are enabled, and the app password is not working or not desired, it might be that google has found the IP address that your imap is using to be suspicious and blocked it.

    Log into that account from a browser and check for an email from that has the subject "Critical security alert". The date on the email should be around the same time that imap stopped working. If you find such an email, it should spell out that a sign-in attempt was blocked, and have a big button that says "Check activity". When you click the button, it will ask if the sign in that it blocked was actually from you. Indicate that it was, and you should be able to sign in with imap again from that IP.

    If you or someone else has already indicated that it was or was not them, there is an option to change your answer.

    When google decides to do this, it only sends a single email. So if it's been a few days, that email might be buried in the inbox. It does not get resent for each blocked sign in from that IP.