I want to read a mail's inbox using PHP IMAP.
On phpinfo() i see both OpenSSL and IMAP enabled.
When trying to connect:
/* connect to inbox */
$hostname = "{imap-mail.outlook.com:993/imap/tls}INBOX";
$username = 'mail#...';
$password = 'password';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Error: ' . imap_last_error());
It will always throw an error:
Can't connect to imap.glbdns2.microsoft.com,993: Connection refused (errflg=2)
On the outlook settings it says:
Why does this happen and how can I fix it?
I had almost the same problem with my email server. It was not accepting request from other third party servers which was using it. After sometimes, it came down to the increased banning rules I had set for the server. The fail2ban was now set to permanently ban offending IPs which tried to unsuccessfully login for more than three times. I was lucky since I managed this email server myself so I un-blacklisted the IP and it works fine. Firewall problem is the most likely cause of this. Try the following:
check your IP if it is blacklisted by any blacklist houses (the requesting server IP);
a site like https://mxtoolbox.com/ can help;
if you have another server (like VPS container), try running the code from it;
if all fails, you may have to contact the: imap-mail.outlook.com people for help.
Related
<?php
require "vendor/autoload.php";
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'mail#gmail.com';
$password = 'password';
$inbox = imap_open($hostname,$username ,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
He constantly writes that access is denied. The feeling of being closed by Google security. Where to allow access for no message:
Can't connect to gmail-imap.l.google.com, 993: Connection refused
Website https://pomidor.cc
Something on your network -- possibly a firewall -- is blocking your connection to the Google mail server. Contact your system administrator or hosting provider for details.
From the information you provided, it looks like a problem related with a missing certificate on your end. Here are a couple of questions with workarounds/solutions [1][2].
You may also consider to start using the Gmail API which allows you to do several actions in a simple way. You can enable the IMAP setting [3] and set the quickstart so you can start making requests from there [4].
[1] Connect to Gmail with PHP & IMAP
[2] Certificate error using IMAP in PHP
[3] https://developers.google.com/gmail/api/v1/reference/users/settings/updateImap
[4] https://developers.google.com/gmail/api/quickstart/php
I had the same problem and what solved it for me was 2 things:
I used my own password instead of the app password which you can
generate here
Instead of {imap.gmail.com:993/imap/ssl}INBOX i used
{localhost:993/imap/ssl/novalidate-cert}INBOX
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?
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.
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
I am using Apache on Ubuntu 14.02 and running php on the same.I have connected to a remote mssql server hence no need of mysql .
I want to know how to connect to an email id registered with gmail in order to read the incoming emails .
I have tried the below , but it shows a blank screen while running it from my localhost :
<?php
error_reporting('E_ALL');
$mailboxPath = "{imap.gmail.com:993/imap/ssl}INBOX";
$username = "my_email#gmail.com";
$password = "mypassword";
$imap = imap_open($mailboxPath, $username, $password);
print_r($imap);
?>
I am inserting my actual gmail username and password while running the program . I want to know if i am missing something .
Many Thanks
Here is the solution:
Login to your gmail account, enable imap.
Let the access here first: https://www.google.com/settings/security/lesssecureapps
Go to: https://accounts.google.com/b/0/DisplayUnlockCaptcha
and enable access.
That's it.
I had the exact same problem when I try to get emails with imap on php.
I was running the exact same script that I know it works with my other gmail.
The solution is above and the error message was:
ERROR: Can not authenticate to IMAP server: [ALERT] Please log in via your web browser
Have you activated the imap.so extension in your php.ini file ? extension=imap.so it's a basical cause of issues on localhost server
Try this (you need to turn on your Outbound relay):
Log into your account at google.com/a/yourdomain.com
Click the Settings tab and then select Email in the left column.
In the Outbound relay section, select Allow users to send mail through an external SMTP when configuring a "from" address hosted outside your domain.
Click Save changes.
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'm going wrong.
Here's my code:
$hostname = '{imap.gmail.com:995/imap/ssl/novalidate-cert}';
$username = 'emailaddress#gmail.com'; $password = 'password';
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
print_r(imap_errors());
Not returning any errors other than:
Warning: imap_open() [function.imap-open]: Couldn't open stream
{imap.gmail.com:995/imap/ssl/novalidate-cert} in
/home/a8066360/public_html/test/imap.php on line 6
Cannot connect to Gmail: Can't connect to gmail-imap.l.google.com, 995:
Connection timed out
I've noticed that if I change the single quotes to `
shell_exec() has been disabled for security reasons...
Please help!!!
You need port 993, the SSL IMAP port.
Port 995 is the SSL POP3 port.
I think Gmail's IMAP can only be accessed on port 993.
$hostname = "{imap.gmail.com:993/imap/ssl/novalidate-cert}";
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.
You have to enable the
Google Enable Less Secure App option from Gmail account get connected.
And a requirement for this, you have to first disable your 2-step-verification from here:
google account -> settings -> security
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.
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 !
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).
Problem
I want to connect to my mail box on my domain name. Unfortunately I couldn't succeed so far.
/* mail information */
$hostname = '{mail.domain.com:110/pop3}INBOX';
$user = 'mail#domain.com';
$pass = 'mypassword';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Mail: ' . imap_last_error());
Error Output
Cannot connect to Mail: Certificate failure for mail.domain.com: Self-signed certificate or untrusted authority: C=US, S=Someprovince, L=Sometown, O=none, OU=none, CN=localhost, E=webaster#localhost
I also tried following;
1. approach
$hostname = '{mail.domain.com:110}INBOX';
After quite long time of waiting;
Cannot connect to Mail: [CLOSED] IMAP connection broken (server response)
2. approach
$hostname = '{mail.domain.com:110/pop3/novalidate-cert}INBOX';
Almost immediate response;
Cannot connect to Mail: Login aborted
At this point, I tried what I could find on PHP.net, Google and SOF without any success.
What could be the problem and what could be the solution? I don't think imap connection is hard to connect.
You can use /novalidate-cert in hostname parameter to bypass certificate validations in case you use self-signed certificates. See http://php.net/manual/de/function.imap-open.php for further information.
Well I've never used IMAP and POP in PHP, but it's evident to me that you're trying to connect via IMAP on a POP3 port. IMAP listens on port 143.