-1

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 = 'myemail@gmail.com';
$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?

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
zzz
  • 23
  • 1
  • 6
  • 1
    the second error is telling you your username and password are invalid, that's all. – ADyson Jul 25 '18 at 09:16
  • yes but my first error is with my own gmail account. – zzz Jul 25 '18 at 09:19
  • If you enter the correct values for Username, and password, it should work perfectly – Joppe De Cuyper Jul 25 '18 at 09:19
  • Have you enabled IMAP in your gmail settings? – Joppe De Cuyper Jul 25 '18 at 09:20
  • i want to know if their is any test gmail account available because i want to test this function. So may be their is something wrong with my email. I tried many other emails but still same error. – zzz Jul 25 '18 at 09:21
  • Yes i did enable imap – zzz Jul 25 '18 at 09:21
  • "is any test gmail account available"...you can create as many gmail accounts as you like, they're free. Test away. I have no idea who "david walsh" is as mentioned in your question, but why are you trying to use _his_ credentials? Surely, whoever he is, he didn't give you his gmail password? – ADyson Jul 25 '18 at 09:22

2 Answers2

1

You are probably looking for gmail setup for something called "Less Secure Apps": https://support.google.com/accounts/answer/6010255?hl=en

that's why it requires you to log in with a browser.

After you set this up correctly, and enable less secure apps, this should work nicely, just checked with my own account.

EDIT: is safe apps is alredy enabled, you may need to create App Password: https://support.google.com/mail/answer/185833?hl=en and change your credentials to:

$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'email@gmail.com';
$password = 'app password here, not your gmail password';

EDIT2: App Password functionality require to have 2-Step verification enabled

Jan Myszkier
  • 2,714
  • 1
  • 16
  • 23
  • 1
    Please read the OP's question again: _"I also enabled less secure apps"_ – M. Eriksson Jul 25 '18 at 09:21
  • 1
    Read the question: `I also enabled less secure apps` – eisbehr Jul 25 '18 at 09:21
  • yes i went their and its coming that The setting that you are looking for is not available for your account. – zzz Jul 25 '18 at 09:34
  • you'll have to enable 2step verification for app passwords to work. try setting that up first. if that doesn't work, it may be someone disabled that for your company account. see https://support.google.com/a/answer/6260879?hl=en – Jan Myszkier Jul 25 '18 at 09:37
  • the link I posted is about setting up admin.google.com, it doesn't have anything to do with the hosting. But if you need I can help as much as I can, feel free to find me on skype. – Jan Myszkier Jul 25 '18 at 09:53
  • it's same as SO username: janmyszkier – Jan Myszkier Jul 25 '18 at 09:57
1

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.

Jarod S
  • 36
  • 3