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