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.
Related
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.
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.
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);
}
So I have the basic problem of a mailing list and I am trying to find all the undeliverable emails and turn them off. I am using php imap function and I have been going through all the fetch and structure functions and although they do give you the information is there anything that concisely will say this is a bad email based on the error code. Currently my best option is using imap_fetchbody with option 2 and doing a find for 550 or 5.4.4 or 5.1.1 etc and then grabbing the email. I wanted to know if there was a function that tells you bad good or at least just has the above codes 550, 5.4.4 etc in a concise array format.
set_time_limit(0);
$hostname = "{localhost:110/pop3/novalidate-cert}INBOX";
$username = 'xxxx';
$password = "xxxx";
$inbox = imap_open($hostname, $username, $password) or die("Cannot connect to pop: " . imap_last_error());
$emails = imap_search($inbox, 'ALL');
if($emails){
$output = '';
rsort($emails);
foreach($emails as $key1 => $email_number){
if($key1 < 10){
$imap_fetchstructure = imap_fetchstructure($inbox, $email_number);
$imap_fetchbody = imap_fetchbody($inbox, $email_number, 1);
$imap_fetch_overview = imap_fetch_overview($inbox, $email_number);
$imap_bodystruct = imap_bodystruct($inbox, $email_number, 1);`
} else{
break;
}
}
}
imap_close($inbox);
I connect to my gmail account via this code
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'myacount#gmail.com';
$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);
$message=strip_tags($message);
/* output the email header information */
$emai=split("<",$overview[0]->from );
echo 'Lus ou pas : '.($overview[0]->seen ? 'lue' : 'Non Lue').'<br>';
echo 'subject : '.$overview[0]->subject.'<br> ';
//echo 'from : '.$overview[0]->from.'<br>';
echo 'from : '.$emai[0].' + '.$emai[1].'<br>';
echo 'date : '.$overview[0]->date.'<br>';
/* output the email body */
echo 'Message '.rawurlencode(utf8_decode(rawurldecode($message))).'<br>';
break;
}
echo "<br><br><br><hr><br><br>";
}
/* close the connection */
imap_close($inbox);
in my account I have one email her body have
http://www.test.com?id=4
but when I execute my script I have her in my browser
Lus ou pas : Non Lue
subject : test
from : =?ISO-8859-1?Q?fai=E7al_name?= + myacount#hotmail.com>
date : Mon, 24 Sep 2012 13:39:11 +0000
Message %0D%0A%0D%0A%0D%0A%3D0A%3D%0D%0A%3D0A%3D%0D%0A%3D0A%3D%0D%0Ahttp%3A%2F%2Fwww.test.com%3Fid%3D3D4%20%09%09%20%09%20%20%20%09%09%20%20%20%09%09%20%09%20%20%20%3D%0D%0A%09%09%20%20%0D%0A%3D%0D%0A
how can I get the link exactly.
Ps : English Is not my mother tongue, sorry for any mistakes.
<?php echo 'Message : '.quoted_printable_decode($message).'<br/>';
Try this:
$s = urldecode('%0D%0A%0D%0A%0D%0A%3D0A%3D%0D%0A%3D0A%3D%0D%0A%3D0A%3D%0D%0Ahttp%3A%2F%2Fwww.test.com%3Fid%3D3D4%20%09%09%20%09%20%20%20%09%09%20%20%20%09%09%20%09%20%20%20%3D%0D%0A%09%09%20%20%0D%0A%3D%0D%0A');
echo $s;
It will output =0A= =0A= =0A= http://www.test.com?id=3D4 = = to the screen.
If have no idea why your message is urlencoded in the first place. But it works in you email client, because the email client will urldecode the message to.