How to open all emails in a utf8 encode? - php

Below is my code that I open "Zoho" to get unread emails.
The problem is that the encoding from emails coming from Gmail and Outlook are different.
Gmail is utf-8 and Outlook (live) is iso-8869-7.
How to handle this?
$hostname = '{imappro.zoho.com:993/imap/ssl}INBOX';
$username = 'email#email.com';
$password = 'password';
/* try to connect */
$inbox = imap_open($hostname,$username ,$password) or die('Cannot connect to Zoho: ' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox,'UNSEEN');

Related

IMAP - in_reply_to always blank

I want to pair a reply with the original message.
I am connecting to a gmail account like so:
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'somebody#gmail.com'
$password = 'password';
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
$emails = imap_search($inbox,'UNSEEN');
$max_emails = 3;
if($emails) {
rsort($emails);
foreach($emails as $email_number) {
$overview = imap_fetch_overview($inbox,$email_number,0);
if($count++ >= $max_emails) break;
}
}
When an email in the inbox is a reply, the [in_reply_to] field appears within imap_fetch_overview, so it knows it's a reply, however this field is always blank.
How can I pair a reply to the sent message?
Ignore this...
I was displaying results like so:
print_r($overview);
When doing this instead:
var_dump($overview);
The information is there.
Not sure why that is...

How to receive gmail using imap in server?

I want to receive the mails from gmail using imap. I have used the code $connection_result = $imap->connect('{imap.gmail.com:993/imap/ssl}INBOX', 'xxxxxxx#gmail.com', 'xxxxxx');
It works in the local xampp but if host in my server, its giving error as
Cannot connect to Mail: Can't connect to gmail-imap.l.google.com,993: Network is unreachable
How to use in server. Anyone can help?
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'example#gmail.com';
$password = 'example';
$inbox = imap_open($hostname,$username ,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
$emails = imap_search($inbox,'ALL');
if($emails) {
$output = '';
rsort($emails);
foreach($emails as $email_number) {
$overview = imap_fetch_overview($inbox,$email_number,0);
$output.= 'Name: '.$overview[0]->from.'</br>';
$output.= 'Message ID: '.$overview[0]->message_id.'</br>';
$output.= 'Subject: '.$overview[0]->subject.'</br>';
$output.= 'UID: '.$overview[0]->uid.'</br>';
$output.= '<hr/>';
}
echo $output;
}
imap_close($inbox);

Issues reading mailbox wien Gmail and yahoo emails received - PHP

I'm reading e-mails from a mailbox on my domain. It's all ok, i'm getting overview "from" and "subject", then body content... all in UTF-8 except Gmail and yahoo mails.
I did this function:
function newmail($username, $password){
/* connect to server */
$server = "mail.domain.com";
$port = "993";
$type = "imap";
$secure = "ssl";
$options = "novalidate-cert";
$hostname = "{".$server."/".$type."/".$options."}INBOX";
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to inbox: '.$username.' on '.$server.' ' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox,'ALL');
/* if emails are returned, cycle through each... */
if($emails) {
$subjects = '';
$senders = '';
$bodys = '';
$dates = '';
/* 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 = iconv("ISO-8859-1", "UTF-8", quoted_printable_decode(imap_fetchbody($inbox, $email_number, 1)));
$senders .= iconv("ISO-8859-1", "UTF-8", quoted_printable_decode($overview[0]->from))." ## ";
$subjects .= iconv("ISO-8859-1", "UTF-8", quoted_printable_decode($overview[0]->subject))." ## ";
$bodys .= $message." ## ";
$dates .= $overview[0]->date." ## ";
}
}
/* close the connection */
imap_close($inbox);
//return different parts to explode
$emails = array(
'sender' => $senders,
'subject' => $subjects,
'body' => $bodys,
'date' => $dates
);
return $emails;
}
I'm doing this way cause i want to make something when read the body depending of subject etc.
The issue happen when i send an email from a gmail or yahoo account (i've not tried with outlook, only with 3/4 different domain mailing servers, gmail and yahoo).
It makes appear some glitches like:
=?UTF-8?Q?Joël_Bo?=
i think i'm parsing well to UTF-8, otherwise it hadn't been working on other emails. The glitch i pasted...i copied it from overview ("from") part, and i have more glitch on body too.
Any idea?
PD: i dont want to use php pear, zend or others, i want to make it with pure php. Don't mean to insert third party scripts on my one-
Thanks!

open email by php using imap giving me this error Message: imap_open() [function.imap-open]: Couldn't open stream {imap.gmail.com:993/imap/ssl}INBOX

I am trying to open the email messages by php using imap method but it give me an error Couldn't open stream {imap.gmail.com:993/imap/ssl}INBOX and this is my code
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'nohamedtemraz92#gmail.com';
$password = '0129799169mohamed';
/* 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);
$output.= 'Name: '.$overview[0]->from.'</br>';
$output.= 'Email: '.$overview[0]->message_id.'</br>';
}
echo $output;
}
/* close the connection */
imap_close($inbox);
Try this:
$hostname = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';
$inbox = imap_open($hostname,$username ,$password) or die('Cannot connect to Gmail: ' imap_last_error());
{imap.gmail.com:993/imap/ssl}INBOX
working for me but you have to enable 'Access for less secure apps' on account security or you can try live email account

gmail not responding while connecting through imap?

I wrote a simple 3 line code for connecting to gmail through imap-php.
i am using 000webhost for running it.
but when i try to open it, it keeps on loading and after some times shows "Problem loading page" or "No data recieved", as if gmail servers are not responding!!
Any suggestions whats the problem?
$hostname = "{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox";
$username = 'abc#gmail.com';
$password = "abc123";
$inbox = imap_open($hostname,$username,$password)or die('Cannot connect to Gmail: ' . imap_last_error());
$server_name = "imap.gmail.com";
$account_name = "email#mydomain.com";
$account_pwd = "my_account_password";
$inbox = imap_open("{".$server_name.":993/imap/ssl/novalidate-cert}INBOX",
$account_name, $account_pwd) OR error_imap_mail("Unable To Open Imap
Connection","Imap Failure",imap_errors());
function error_imap_mail($subject,$messagetext,$errors) {
global $report_email;
$mailfrom = "support#mydomain.com";
$mailsubject = "$subject";
$mailbody = "$messagetext\n\n";
if (count($errors) >0) {
if (substr($errors[0],0,8) <> "[IN-USE]") {
array_reverse($errors);
foreach ($errors as $key=>$value) {
$mailbody .= "\nErr No.".$key." Error:".$value;
}
foreach ($report_email as $value) {
mail($value, $mailsubject, $mailbody, "From: $mailfrom");
}
}#if (substr($err_array[0],0,8) <> "[IN-USE]") {
}#if (count($errors) >0) {
exit;
}
Get your script to send you the errors if it fails to connect.
Have you turned IMAP on in the account setting in Gmail?
Try this:
$hostname = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
$username = 'abc#gmail.com';
$password = "abc123";
$inbox = imap_open($hostname,$username,$password)or die('Cannot connect to Gmail: ' . imap_errors());
Also make sure you enable IMAP for you account via GMail settings.

Categories