fetching gmail inbox with imap - php

i got a code from here to download gmail inbox:
http://davidwalsh.name/gmail-php-imap
use these 2 hostnames
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox
but getting this error.
Warning: imap_open() [function.imap-open]: Couldn't open stream {imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox in /home/medicalh/public_html/testmail.php on line 9
Cannot connect to Gmail: Can't connect to gmail-imap.l.google.com,993: Connection timed out
i have searched on stackoverflow but did't got solution
thanks

change you host name to this one and try
$hostname= '{imap.gmail.com:993/ssl/novalidate-cert}';
and double check the username and password

Open the php.ini file and remove ; before ;extension=php_imap.dll
and restart your server.
Your code will work...

You can download my Gmail class at my website: . I have also included a test file for you to test the class file.
If it does not work, I believe it could be your server's problem. You should contact your system administrator.

One of the issues with gmail IMAP SSL authentication is related to Google's account security.Once you get the login error once, sign out of all your google accounts. Then, visit this link:
https://accounts.google.com/DisplayUnlockCaptcha
Log in with the account you're attempting to access via imap.
Follow the steps and you'll then be able to login in to gmail with php imap.
It's visually shown here:
http://jeffreifman.com/filtered-open-source-imap-mail-filtering-software-for-php/configuring-gmail/
source: http://php.net/manual/en/function.imap-open.php

Related

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 = '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

Open shared exchange email account with imap_open() PHP

In exchange environment I have PHP application setup to open and IMAP stream and parse emails. I have no problem connecting to my personal email account with the following:
imap_open("{exchange.server/ssl}INBOX", "username", "password");
Now I have been given access to a group email account and I'm wondering if its possible to specify other accounts with PHP's IMAP functions. I know when I setup outlook with a new profile I add the group email then my credentials with no problems.
I've also see telnet commands that support this in How can I access a shared Exchange mailbox with IMAP (over telnet)?
TELNET: ? LOGIN domain/username/mailboxname password
Using this logic I have tried:
imap_open("{exchange.server/ssl}INBOX", "username/sharedmailbox", "$password");
This gives an error I've also tried to add the domain with no luck.
Turns out I was very close with what I posted in the question however I was including #example.com for sharedmailbox#example.com in the imap_open(). Once I removed that I had no problem connecting with the following.
imap_open("{exchange.server/ssl}INBOX", "domain/username/sharedmailbox", "$password");

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.

Access emails using imap

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.

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

Categories