How to get latest Unread email from primary tab using gmail IMAP - php

HI Have requirement of creating ticket if any new mail come to gmail account.
but problem with current implementation is even its a promotional email or socail email its creating a ticket.
is there is some way we can sort email based on category?
is there a way we can identify its a looped mail not new email
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'support#xyz.com';
$password = 'ywowahvxitmesssskvly';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
$today = \Carbon\Carbon::now()->format('j-M-Y');
/* grab emails */
$emails = imap_search($inbox,'UNANSWERED');
dd($emails);
/* if emails are returned, cycle through each... */
if($emails) {
/* put the newest emails on top */
rsort($emails);
/* for every email... */
$emailStatus = "";
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 = ((imap_fetchbody($inbox, $email_number, 2)));
$message = quoted_printable_decode(imap_fetchbody($inbox, $email_number, 1.1));
if ($message == '') {
$message = quoted_printable_decode(imap_fetchbody($inbox, $email_number, 2));
}
/* output the email header information */
$header = imap_headerinfo ( $inbox, $email_number);
$emailStatus = $overview[0]->seen;
$emailSubject = $overview[0]->subject;
$emailFromName = $header->from[0]->personal;
$emailFromEmail = $header->from[0]->mailbox .'#'.$header->from[0]->host ;
$emailDate = $overview[0]->date;
$emailBody = base64_encode($message);
$emailMessageId = $overview[0]->message_id;
try{
if(!$emailStatus)
{
$ticket = new Ticket();
$ticket->title = $emailSubject??'';
$ticket->content = base64_encode($message)??'';
$ticket->author_name = $emailFromName??'';
$ticket->author_email = $emailFromEmail??'';
$ticket->status_id = 1;
$ticket->category_id = 1;
$ticket->priority_id = 1;
$ticket->assigned_to_group_id = 1;
$ticket->is_created_from_mailbox = 1;
$ticket->message_id = $emailMessageId;
// dd($ticket);
$ticket->save();
}
imap_setflag_full($inbox, $email_number, "\\Seen \\Flagged");
}catch(\Exception $e)
{
// dd($e->getMessage());
\Log::error("error form gamil ===" .$e->getMessage());
}
}
}
/* close the connection */
imap_close($inbox);

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 email attachments using php?

I'm trying to retrieve email and attachments in cpanel email. Emails are being retrieved correctly, however I cannot retrieve the attachments. What am I doing wrong?
My source code:
$emailAddress = "email#email.com" ; // Full email address
$emailPassword = "password"; // Email password
$domainURL = 'email.com'; // Your websites domain
$useHTTPS = true;
$inbox = imap_open('{'.$domainURL.':143/notls}INBOX',$emailAddress,$emailPassword) or die('Cannot connect to domain:' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox,'UNSEEN');
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 = imap_fetchbody($inbox,$email_number,2);
$header = imap_headerinfo($inbox, $email_number, 1);
/* output the email header information */
$subject_other= $overview[0]->subject;
$sender_name_other= $overview[0]->from;
$date_other=$overview[0]->date;
$msg_to_other=$overview[0]->to;
$msg_from_other = $header->from[0]->mailbox . "#" . $header->from[0]->host;
$msg_msg_other = $message;
//store to database email data
mysqli_query($con,"insert into other(msg_to, msg_from, sender_name, subject, message, msg_date_time) values('$msg_to_other', '$msg_from_other', '$sender_name_other', '$subject_other', '$msg_msg_other', '$date_other')");
}
}
/* close the connection */
imap_close($inbox);
// insert into other email in database
foreach($emails as $email_number) {
$structure = imap_fetchstructure($inbox,$email_number);
}
http://php.net/manual/en/function.imap-fetchstructure.php

IMAP body displaying raw data

I'm using php imap functiona to get mail content.
Following is my code:
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = $_REQUEST['email'];
$password = $_REQUEST['pwd'];
$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);
$i = 0;
foreach($emails as $email_number)
{
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_qprint(imap_body($inbox, $email_number));
}
}
Displaying html data as it is in my mail body along with some raw data. How can I remove this.
--94eb2c03a0feeaad70054df6411a
Content-Type: text/plain; charset=UTF-8; format=fowed; delsp=yes
Content-Transfer-Encoding: base64
PT09IEZpbmFuY2UgLSAxIG5ldyByZXN1bHQgZm9yIFthbGliYWJhXSA9PT0NCg0KDQo8aHR0cHM6
Ly93d3cuZ29vZ2xlLmNvbS91cmw/cmN0PWomc2E9dCZ1cmw9JmN0PWdhJmNkPUNBRVlBQ29UTnpN
ek1EUTBPREE0TVRnNU9EWTJPVFl5TkRJYVptVXpOR0psT0Rnd00yTmpPREF6WXpwamIyMDZaVzQ2
VlZNJnVzZz1BRlFqQ05GMXNIbGsyejlRSXgyOGp0dmZuR1BfVWxraWZBPg0KDQoNCj09PSBOZXdz
IC0gMTAgbmV3IHJlc3VsdHMgZm9yIFthbGliYWJhXSA9PT0NCg0KQWxpYmFiYSdzIFN0cm9uZyBH
cm93dGggUmF0ZSBJcyBVbmRlcmFwcHJlY2lhdGVkDQpTZWVraW5nIEFscGhhDQpBbGliYWJhJ3Mg
aGlzdG9yaWNhbCBncm93dGggcmF0ZSBpcyBhbiBhdmVyYWdlIG9mIDUwJSBmb3IgdGhlIGxhc3Qg
Zml2ZQ0KeWVhcnMsIHdoaWNoIGlzIG5vdGhpbmcgc2hvcnQgb2Yg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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!

Getting e-mail ID of sender while fetching mails from Gmail

I am fetching email from gmail where I can fetch body, name of the sender etc. I have to get the email-id of the sender, which I am not able to get. I have tried with some variables names like fromaddress after fetchting headerinfo by imap_header but didn't work.
Can I get some help regarding this ?
function connect_mail(){
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = '*****#gmail.com';
$password = '*****';
$inbox = imap_open($hostname,$username,$password) or die(t('Cannot connect to Gmail: ' . imap_last_error()));
$emails = imap_search($inbox,'ALL');
$Msgcount = count($emails);
for ($x = 1; $x <= $Msgcount; $x++)
{
$overview = imap_fetch_overview($inbox, $x);
$title = $overview[0]->subject;
echo "Subject of the Mail : ".$title."</br>";
$from = $overview[0]->from;
echo "Name of the sender : ".$from."</br>";
//Now I have to get mail ID of senders & print it, but how?
}
}
I have tried my luck with several methods but got failed each time...
Thanks in Advance :)
I have done it..Hurray!
Here is the code:-
function connect_mail(){
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = '*****#gmail.com';
$password = '*****';
$inbox = imap_open($hostname,$username,$password) or die(t('Cannot connect to Gmail: ' . imap_last_error()));
$emails = imap_search($inbox,'ALL');
$Msgcount = count($emails);
for ($x = 1; $x <= $Msgcount; $x++)
{
$overview = imap_fetch_overview($inbox, $x);
$title = $overview[0]->subject;
echo "Subject of the Mail : ".$title."</br>";
$from = $overview[0]->from;
echo "Name of the sender : ".$from."</br>";
$header = imap_headerinfo($inbox, $x);
$fromaddress = $header->from[0]->mailbox . "#" . $header->from[0]->host;
echo "From E-Mail Address : ".$fromaddress.;
}
}
Thanks :)

Categories