IMAP incoming mail server setup in PHP - php

I have searched and read so many questions regarding this. I am saving incoming emails in to a MySQL database.
I am using pipe to script method and I have already set up those things and the file reads correctly when I receive an email.
The error I receive is:
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to server: ' . imap_last_error());
I am confused with $hostname variable, I have tried many times and I failed.
Normally my server is localhost for MySQL. I tried with that, and my server uses SSL. I used this localhost:993/imap/ssl}INBOX, but none are working. What is the correct server for that variable?
imap.mydomain.org:993 ???
Please help me to resolve this problem. I am fed up with this.

Going by what you have said, have you tried:
{localhost:993/imap/ssl}INBOX
or
{imap.mydomain.org:993/imap/ssl}INBOX
If that doesn't work, could you try providing an error output?

The true error message is Certificate failure, as mentioned by OP.
To solve the issue, change the function to:
$mbox = imap_open('{imap.maydomain.org:993/novalidate-cert}INBOX', 'user#domain.com', 'userpass') or die('Cannot connect to server: ' . imap_last_error());
to bypass validating self-signed certificate.

Related

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.

Connect to Gmail with PHP & IMAP

I have enabled OpenSSL and IMAP functions from my php.ini file and phpinfo() confirms it.
By using the code below i can connect to a Hotmail account but not to a Gmail account.(of course i change the $connect_to to point to Hotmail.)
$connect_to = '{imap.gmail.com:993/imap/ssl}INBOX';
$connection = imap_open($connect_to, $user, $password)
or die("Can't connect to '$connect_to': " . imap_last_error());
imap_close($connection);
The returned error is
Warning: imap_open(): Couldn't open stream {imap.gmail.com:993/imap/ssl}INBOX in /opt/lampp/htdocs/webmail_client_practise/index.php on line 6
Can't connect to '{imap.gmail.com:993/imap/ssl}INBOX': Certificate failure for imap.gmail.com: unable to get local issuer certificate: /C=US/O=GeoTrust Inc./CN=GeoTrust Global CA
Notice: Unknown: Certificate failure for imap.gmail.com: unable to get local issuer certificate: /C=US/O=GeoTrust Inc./CN=GeoTrust Global CA (errflg=2) in Unknown on line 0
Unfortunately i cannot find a complete tutorial of how to use the IMAP functions.
Any ideas, solutions or suggestions will be helpful.
UPDATE:
After searching for my problem on the Internet, i did not found a solution that solves my problem completely. Although if
1) I use the insecure $connect_to = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';
2)And allow gmail to be accessed by less secured apps,
solves my problem for now.
Please check this https://support.google.com/accounts/answer/6010255?hl=en
If you get blocked, this can be because google sees a login from a new device.
Go to your google account and check
https://myaccount.google.com/device-activity
and let them know the 'Unknown device is ok to use'.
First check your date and time settings on your server are correct.
Alternatively try:
$connect_to = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX',
This will stop it from trying to validate certificate.
I will probably get downvotes from people telling me this is bad practice as this leaves you open to Man in the Middle attacks. Which is true.

imap_open(): Couldn't open stream in Yii controller

I have really strange issue with imap_open function that it connects to some servers but fails with some servers. in the Yii controller I have
$inbox = imap_open("{".$model->incoming_hostname .":" .$model->incoming_port
."/ssl/novalidate-cert}",$model->e_mail_username,$model->e_mail_password);
Now when I submit parameters to connect to Gmail account I get the
imap_open("{imap.gmail.com:993/ssl/novalidate-cert}", "username", "password") and the inbox is opened fine, the same seems to work for yahoo. But for some reason I get error when connecting to outlook.com or icloud.com e-mail.
imap_open("{imap-mail.outlook.com:993/ssl/novalidate-cert}", "username", "password") gets error imap_open(): Couldn't open stream
What is really strange is that when I run the separate php script I made manually in shell for testing with the same parameters for connecting to outlook.com e-mail account it works fine. Also when I access this script through apache via web browser it also works. So this is not the problem of apache executing php code. But why does it fail in the Yii?
$hostname = '{imap-mail.outlook.com:993/ssl/novalidate-cert}';
$username = 'username';
$password = 'password';
/* try to connect */
$inbox = imap_open($hostname,$username,$password);
Any suggestions how to track this issue? In Yii I cannot see what could be the problem by calling imap_last_error() because Yii immediately displays error page for the warning
PHP warning
imap_open(): Couldn't open stream
{:993/ssl/novalidate-cert}
/var/www/..../controllers/RegisterController.php(102)
try this as hostname
$hostname='{imap-mail.outlook.com:993/imap/ssl}INBOX';
and also you pre check your username

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

Reading POP3 emails with PHP and IMAP issues

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.

Categories