IMAP not opening when trying to fetch Hotmail emails in CakePHP - php

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.

Related

Accessing a mail with Gmail with php and changing the folder

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.

How to get unread messages from server webmail in php

I'm integrating webmail inbox access process. See my code is below. It does not get unread message it will bring all messages. How can i get only unread messages Kindly advice me.
$emails = imap_search($openmail, 'UNSEEN');
see above line it does not working.
$authhost="{example.com:110/pop3}INBOX";
$username="xxx#example.com";
$password="wxwxwxw";
ini_set('max_execution_time', 0);
/* try to connect */
$inbox = imap_open($authhost,$username,$password) or die('Cannot connect to Mail Server: ' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox, 'UNSEEN');
// echo count($emails);exit;
/* 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);
/* echo "<pre>";
print_r($overview);exit; */
$obj_thang = imap_headerinfo($inbox, $email_number);
// print_r($overview);exit;
$message = imap_body($inbox,$email_number,2);
/* output the email header information */
$output.= $obj_thang->subject;
//$output.= $obj_thang->fromaddress."<br/>";
$output.= $obj_thang->reply_toaddress."<br/>";
}
echo $output;
}
You are connecting to POP3, not IMAP. Pop3 does not support server side search, nor a concept of unseen vs seen messages.

retrive the emails from my own server email address like manoj#manoj.com

Is it possible to fetch the emails from my own domain mails ? i want to fetch the inbox please help for this , I am using IMAP right now but it is giving me the ssl errors like
Certificate failure for MAIL.enlighten-energy.net: Server name does not match certificate: /OU=Domain Control Validated/OU=PositiveSSL Wildcard/CN=*.justhost.com
function fetch_gmail_inbox()
{
$res=array();
/* connect to gmail */
$hostname = '{imap.enlighten-energy.net:143/imap}';
$username = 'abc#enlighten-energy.net';
$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,'UNSEEN');
/* if emails are returned, cycle through each... */
if($emails) {
/* 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 = quoted_printable_decode(imap_fetchbody($inbox,$email_number,1));
$structure = imap_fetchstructure($inbox,$email_number);
if($structure->parts[0]->encoding == 3 ||$structure->encoding == 3 )
{
$message=imap_base64($message);
}
if($structure->parts[0]->encoding == 4 ||$structure->encoding == 4)
{
$message = imap_qprint($message);
}
$message2= quoted_printable_decode(imap_fetchbody($inbox,$email_number,0));
$date=explode(':',$message2);
$date2= date('d-m-Y h:i:s',strtotime($date[8].':00:00'));
if($overview[0]->subject=="USR:Site01_Comms Complete")
{
$res['date']=$date2;
$res['body']=$message;
}else
{
echo "not a correct mail";
}
}
return $res;
}
/* close the connection */
imap_close($inbox);
}
but it is not working for me any suggestion will appreciable .thanks in advance
Certificate failure for MAIL.enlighten-energy.net: Server name does not match certificate: /OU=Domain Control Validated/OU=PositiveSSL Wildcard/CN=*.justhost.com
The error message is actually quite clear if you understand SSL/TLS:
You access imap.enlighten-energy.net (which is a cname to mail.enlighten-energy.net)
But the certificate of the server is issued for *.justhost.com
Since *.justhost.com does not match imap.enlighten-energy.net it will not trust the certificate, because if it would just trust any certificate then the connection would be open to man-in-the-middle attacks which can defeat the encryption.
In summary: if you want to use your own domain name for the IMAP server you have to setup this server with a certificate for your own domain. If this is a shared server between multiple hosts and you don't have access to the configuration of this server, then you cannot do this.
Found the solution , i have to use
$hostname = '{mail.enlighten-energy.net:143/imap/novalidate-cert}INBOX';
instead of this
$hostname = '{imap.enlighten-energy.net:143/imap}';
like , here is the complete solution
function fetch_gmail_inbox()
{
$res=array();
/* connect to gmail */
$hostname = '{mail.enlighten-energy.net:143/imap/novalidate-cert}INBOX';
$username = Yii::app()->getModule('user')->get_config('datalogger_email');
$password = Yii::app()->getModule('user')->get_config('datalogger_email_pwd');
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox,'UNSEEN');
/* if emails are returned, cycle through each... */
if($emails) {
/* 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 = quoted_printable_decode(imap_fetchbody($inbox,$email_number,1));
$structure = imap_fetchstructure($inbox,$email_number);
if($structure->parts[0]->encoding == 3 ||$structure->encoding == 3 )
{
$message=imap_base64($message);
}
if($structure->parts[0]->encoding == 4 ||$structure->encoding == 4)
{
$message = imap_qprint($message);
}
$message2= quoted_printable_decode(imap_fetchbody($inbox,$email_number,0));
$date=explode(':',$message2);
$date2= date('d-m-Y h:i:s',strtotime($date[8].':00:00'));
if($overview[0]->subject=="USR:Site01_Comms Complete")
{
$res['date']=$date2;
$res['body']=$message;
}
}
return $res;
}
/* close the connection */
imap_close($inbox);
}

How to fetch Gmail on PHP IMAP?

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

Sudden PHP Error "Warning: imap_open() [function.imap-open]: Couldn't open stream"

Okay, this code 'was' working perfectly and then I started playing around with it in order to let others connect to their e-mails and as you do ran into a few open stream errors along the way due to various typos and such.
Since doing this, all of a sudden I can't connect to my e-mail at all? A short while back I was using the exact same connection code and then browsing my inbox.
I always get the "Warning: imap_open() [function.imap-open]: Couldn't open stream" error.
This is strange as I'm using the exact same code as before, but since bumping into errors I can't connect whatsoever now. It also takes ages to respond.
Here is the code:
$mailbox = imap_open('{mail.artisancodesmith.com:143/notls}INBOX', 'admin#artisancodesmith.com', 'PASSWORD');
if ($mailbox) {
$response = "MAIL MENU:<br>
inbox: View your inbox.<br>
compose: Compose an e-mail.<br>
setup: Set your e-mail account's settings.";
$next = "iorcmail";
}
NOTE: The PHP page is connecting to the e-mail on the same server.
UPDATE:
If I replace "mail.artisancodesmith.com" with "localhost" it works again!
I would preferably like to use my actual IMAP host - I'll see if it works again some time in the future I guess.
Thanks to all who helped. :)
Please use the below code to connect successfully,
$hostname = "{imap.gmail.com:993/imap/ssl/novalidate-cert}";
$mailbox = imap_open($hostname, 'admin#artisancodesmith.com', 'PASSWORD');
if ($mailbox)
{
// do work....
}
I ran into this problem and here is how I solved it;
Login to your gmail account, Under Settings -> Forwarding and POP/IMAP -> Enable Imap.
Go to https://www.google.com/settings/security/lesssecureapps and select "Turn On"
Go to: https://accounts.google.com/b/0/DisplayUnlockCaptcha and enable access.
after this, above code works for me....
I have tried this an works perfectly fine for me
$inbox = imap_open("{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox", 'username', 'password')
or die('Cannot connect to Gmail: ' . imap_last_error());
$emails = imap_search($inbox,'All');
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);
$header = imap_header($inbox, $email_number);
echo "<h1>data</h1>";
echo "<pre>";print_r($message);
echo "<h1>Message</h1>";
echo "<pre>";print_r($message);
echo "<h1>header</h1>";
echo "<pre>";print_r($message);
$overview[0]->seen;
$overview[0]->subject;
$overview[0]->from;
$overview[0]->date;
}
}
/* close the connection */
imap_close($inbox);
maybe are you behind a proxy? if so, I guess you need to auth against it...
resource imap_open(
string $mailbox ,
string $username ,
string $password [,
int $options = 0 [,
int $n_retries = 0 [,
array $params = NULL ]]]
)

Categories