Reading POP3 emails with PHP and IMAP issues - php

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.

Related

Can't connect to gmail-imap.l.google.com,993: Connection refused

<?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

Read email with IMAP and PHP - Connection Refused

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.

Unable to connect to Gmail, using php imap

I'm trying to use imap with php to retrieve mails from gmail using the following code:
<?php
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'myaccount#gmail.com';
$password = 'mypassword';
$inbox = imap_open($hostname,$username,$password)
or die('Cannot connect to Gmail: ' . imap_last_error());
?>
However, imap_last_error throws me the following error:
"Unable to create TCP socket: Address family not supported by protocol"
I have imap enabled on my gmail account. imap 2007e with ssl (and OpenSSL) and kerberos seems to be enabled on the webserver. I don't have control on the domains ports, but support told me "Outgoing IMAP connections can be made, this is not blocked.".
I see I'm not the only one having trouble connecting to gmail using IMAP. However, I didn't find anyone who encountered this error.

Connecting to Gmail IMAP PHP "Couldn't open stream"

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).

PHP imap gmail not connecting [Edited!!]x2

I have some php code that I'm trying to use to connect to gmail using imap. Here's the code:
$hostname = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';
$tmp_username = 'username';
$tmp_password = 'password';
$inbox = imap_open($hostname, $username, $password) or die(imap_last_error());
And I get this error output everytime i try to connect:
Warning: imap_open() [function.imap-open]: Couldn't open stream {imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX in /var/www/PHP/EmailScript.php on line 14
Login aborted
I dont understand what could be wrong!! I've heard of people having SSL errors but this doesnt seem to be one of those. Please please please help me!!!!!
Edit: When trying to connect through telnet-ssl to imap.gmail.com i get the following output:
Trying 74.125.155.109...
Connected to gmail-imap.l.google.com.
Escape character is '^]'.
And nothing else happens, it just sticks there
I was just about to ask if you had a firewall up when you edited your question.
Your router or firewall may be configured to block port 993. You will have to open it to allow communication on that port. If the machine has a system administrator, you'll have to ask them. Otherwise, check the firewall software and/or router.
Try the host
{imap.gmail.com:993/imap/ssl} without setting the folder INBOX.
Your code should be:
$hostname = '{imap.gmail.com:993/imap/ssl}';
$tmp_username = 'username';
$tmp_password = 'password';
$inbox = imap_open($hostname, $username, $password) or die(imap_last_error());
If the problem persist check if IMAP function is enabled or disabled in your server.

Categories