This question already has answers here:
Certificate error using IMAP in PHP
(3 answers)
Closed 9 years ago.
I need to connect to a Gmail account via IMAP using PHP. I have some working code, but when attempting to establish a connection, I receive the following error:
Warning: imap_open() [function.imap-open]: Couldn't open stream {imap.gmail.com:993/ssl}[Gmail]/All Mail in /home/demoosiz/public_html/goqlue/email.php on line 31
Cannot connect to Gmail: Certificate failure for imap.gmail.com: unable to get local issuer certificate: /C=US/O=Google Inc/CN=Google Internet Authority
my code is below
<?php
/* connect to gmail */
$hostname = '{imap.gmail.com:993/ssl}[Gmail]/All Mail';
$username = 'hidden';
$password = 'hidden';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox,'ALL');
/* if emails are returned, cycle through each... */
if($emails) {
/* begin output var */
$output = '';
/* put the newest emails on top */
rsort($emails);
/* for every email... */
foreach($emails as $email_number) {
/* get information specific to this email */
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,2);
/* output the email header information */
$output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
$output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
$output.= '<span class="from">'.$overview[0]->from.'</span>';
$output.= '<span class="date">on '.$overview[0]->date.'</span>';
$output.= '</div>';
/* output the email body */
$output.= '<div class="body">'.$message.'</div>';
}
echo $output;
}
/* close the connection */
imap_close($inbox);
?>
This seems to be a certificate issue.
I don't tell you what the certificate issue is nor how to solve it.
However, you can leave your gmail account open to attacks by ignoring that issue disabling the certificate validation:
$hostname = '{imap.gmail.com:993/ssl/novalidate-cert}[Gmail]/All Mail';
Cannot connect to Gmail: Certificate failure for imap.gmail.com: unable to get local issuer certificate: /C=US/O=Google Inc/CN=Google Internet Authority
The system you execute the code on is missing that certificate. There is not much more that can be said about this by the information you provided with your question. Consult the system documentation of the operating system you have how to install certificates and how to aquire the needed certificate.
See as well:
Certificate error using IMAP in PHP
Related
When i trying to get messages from email via imap - i have this error:
Can not authenticate to IMAP server: [AUTHENTICATIONFAILED] Invalid credentials (Failure)
OS: debian 7
Where is trouble?
PHP code:
<?php
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'email#gmail.com';
$password = 'pass';
/* try to connect */
$inbox = imap_open($hostname,$username,$password, NULL, 1, array('DISABLE_AUTHENTICATOR' => 'GSSAPI')) or die('Cannot connect to Gmail: ' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox,'ALL');
/* if emails are returned, cycle through each... */
if($emails) {
/* begin output var */
$output = '';
/* put the newest emails on top */
rsort($emails);
/* for every email... */
foreach($emails as $email_number) {
/* get information specific to this email */
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,2);
/* output the email header information */
$output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
$output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
$output.= '<span class="from">'.$overview[0]->from.'</span>';
$output.= '<span class="date">on '.$overview[0]->date.'</span>';
$output.= '</div>';
/* output the email body */
$output.= '<div class="body">'.$message.'</div>';
}
echo $output;
}
/* close the connection */
imap_close($inbox);
If you are using G Suite, you have to contact your G Suite administrator to enable not secure access for users apps in Google Admin panel, URL like https://admin.google.com/{YOUR_DOMAIN_NAME}/AdminHome?hl=en#ServiceSettings/notab=1&service=securitysetting&subtab=lesssecureappsaccess
or go this way (for G Suite admin)
https://admin.google.com
Security settings
Basic settings (first link)
Less secure apps -> Go to settings for less secure apps
Enforce access to less secure apps for all users (Not Recommended)
Save
PROFIT (can take effect in 1-2 mins, wait for some time please)
if you are not using G Suite, check below url and turn on "less secure apps"
https://myaccount.google.com/lesssecureapps
Hello so i'm trying to retrieve emails from a Gmail account through IMAP ( IMAP enabled and access to less secured app On ) on a PHP page but when i try to connect to my account with imap_open , everything crash. Here is my code :
<?php
/* connect to gmail */ $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$username = 'xxxx#gmail.com';
$password = 'xxxxxxxxxxxxxxxx';
echo "test";
echo extension_loaded ( "imap" );
/* try to connect */
$inbox = imap_open($hostname,$username,$password);
echo "test";
/* if emails are returned, cycle through each... */
if($emails) {
/* begin output var */
$output = '';
/* put the newest emails on top */
rsort($emails);
/* for every email... */
foreach($emails as $email_number) {
/* get information specific to this email */
echo "test";
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number, 1.2);
/* output the email header information */
$output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
$output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
$output.= '<span class="from">'.$overview[0]->from.'</span>';
$output.= '<span class="date">on '.$overview[0]->date.'</span>';
$output.= '</div>';
/* output the email body */
$output.= '<div class="body">'.$message.'</div>'; }
echo $output; }
/* close the connection */ imap_close($inbox); ?>
Thanks for your time
Your PHP is not compiled with IMAP
or
This is generally a possible effect of IMAP support missing in your php.ini
Please enable it here is a thread that has fixed such issues in different OS and scenario
Fatal error: Call to undefined function imap_open() in PHP
PHP 7 and 7.1 has it inbuilt but whereas PHP 5.X or PHP 7.2 may have some issues if IMAP is not enabled
Fix for WHM : (as this is not covered in the above link)
WHM -> EasyApache 4 -> PHP extensions "php*-php-imap" (eg php72-php-imap in case of PHP 7.2)
If still does not work contact your server admin of the hosting provider they will enable it in PHP config
To troubleshoot You need to enable the Error reporting first as in the comments and this link: How do I get PHP errors to display?
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
You can troubleshoot based on results of error reporting
I access to gmail using php to make action on mail. I put these mails into a folder using gmail directly.
So here is my code to get it :
$hostname = '{imap.gmail.com:993/ssl}test';
$username = 'myaddress#gmail.com';
$password = 'mypassword';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox,'ALL');
//$emails = imap_search($inbox,'RECENT');
print_r($emails);
/* if emails are returned, cycle through each... */
if($emails) {
/* begin output var */
$output = '';
/* put the newest emails on top */
rsort($emails);
$emailCount = 1;
foreach($emails as $email_number) {
//echo 'email n∞' . $emailCount;
/* get information specific to this email */
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,2);
$structure = imap_fetchstructure($inbox,$email_number);
if (!$overview[0]->seen) {
print_r($overview);
$emailCount++;
}
}
// echo $output;
}
/* close the connection */
imap_close($inbox);
No I'd like to move these mail into an other folder, for exemple test2
here is what I do :
imap_mail_move($inbox, $email_number, 'test2') or die('Error');
but I get an error :
Notice: Unknown: [TRYCREATE] No folder test2 (Failure) [THROTTLED] (errflg=2) in Unknown on line 0
I have tried these different kind of solution :
With a subfolder of test named test3
imap_mail_move($inbox, $email_number, 'test3') or die('Error');
same error.
Also tried :
imap_mail_move($inbox, $email_number, '[Gmail]/test3') or die('Error');
Same error :-(
If anyone has a solution that would be cool !
Thanks,
Alex
So after several tries here is what I have found that works :
imap_mail_move($inbox, $email_number, 'INBOX') or die('Error');
imap_mail_move($inbox, $email_number, 'test2') or die('Error');
But that means to move to the inbox then to the second folder. Problem : the message is still in the inbox after these.
Still looking for an other solution.
With the below code i am trying to fetch emails from Hotmail.
On my system localhost this code is working fine but on my hosting server this code is not working.
IMAP is enable on hosting server.
PHP Warning: imap_open(): Couldn't open stream {imap-mail.outlook.com:993/imap/ssl}INBOX
CakePHP Controller code:
$username = 'myusername#hotmail.com';
$password = 'myusernamepassword';
set_time_limit(3000);
/* try to connect */
$inbox = imap_open('{imap-mail.outlook.com:993/imap/ssl}INBOX',$username,$password) or die('Cannot connect to Mail Server: ' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox,'UNSEEN');
/* if emails are returned, cycle through each... */
if($emails) {
/* begin output var */
$output = '';
/* put the newest emails on top */
rsort($emails);
/* for every email... */
$count = 1;
foreach($emails as $email_number) {
$head = imap_header($inbox, $email_number);
/* get information specific to this email */
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_body($inbox,$email_number,2);
$subject = $this->decode_imap_text($overview[0]->subject);
}
// echo $output;
}
/* close the connection */
imap_close($inbox);
If it is working correctly on localhost then there may be port 993 OR imap protocol is blocked on server by firewall etc. (High Probability for this error)
if you have access to hosting server then check for firewall. otherwise contact hosting administrator or look for another hosting.
Edit: Get more detailed error by imap_last_error, imap_errors AND imap_alerts functions.
I try this hosts:
{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX
{imap.gmail.com:993/imap/ssl}INBOX
{imap.gmail.com:993/ssl/novalidate-cert}Current Batch
{imap.gmail.com:993/ssl/novalidate-cert}
{imap.gmail.com:995/ssl/serivce=pop3}INBOX
None of them worked. What is Gmail host or what IMAP settings?
Thank you!
The imap.gmail.com:993/imap/ssl}INBOX should be the right one. How are you using this in your php code? Do you just want to receive your email? Because than it will look something like this:
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'youremail#gmail.com';
$password = 'password';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
Setting are as follows:
$mail = new Imap([
'host' => 'imap.gmail.com',
'user' => 'username#gmail.com',
'password' => 'password',
'port' => 993,
'ssl' => 'SSL',
]);
Then go to https://myaccount.google.com/security#connectedapps and check 'Allow less secure apps'.
Here is complete code example which connects and fetches emails from gmail :
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'gmail_email_address';
$password = 'password';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox,'ALL');
/* if emails are returned, cycle through each... */
if($emails) {
/* begin output var */
$output = '';
/* put the newest emails on top */
rsort($emails);
/* for every email... */
foreach($emails as $email_number) {
/* get information specific to this email */
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,2);
/* output the email header information */
$output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
$output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
$output.= '<span class="from">'.$overview[0]->from.'</span>';
$output.= '<span class="date">on '.$overview[0]->date.'</span>';
$output.= '</div>';
/* output the email body */
$output.= '<div class="body">'.$message.'</div>';
}
echo $output;
}
imap_close($inbox);
Enter correct email id and password.
You might wanna replace
$emails = imap_search($inbox,'ALL');
with something like this to narrow the results
$emails = imap_search($inbox,'SINCE "19 October 2018"');
or you might get a timeout error.
You might have to enable 2 step verification on google account and use app specific passowrd if you get error like Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure)
There are several problems with PHP IMAP and connecting to gmail.
First make sure you have enabled IMAP in your gmail settings (default is off!).
Second SSL IMAP on PHP has problems with several versions.
To get it to work choose : {imap.gmail.com:993/ssl/novalidate-cert}
Go into your Google Account (not gmail this time), go to security and allow access from less secure methods (Allow less secure apps).
Then it works.
Hopefully there will be a fix one day