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
Related
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);
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...
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!
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
I needed to get the Email address from which Im getting/receiving emails in my Inbox! what should I do for this, I at this time have the following code
<?php
$mbox = imap_open("{imap.gmail.com:993/imap/ssl}INBOX", "address#gmail.com", "passw0rd")
or die("can't connect: " . imap_last_error());
$status = imap_status($mbox, "{imap.gmail.com:993/imap/ssl}INBOX", SA_MESSAGES);
if ($status) {
echo $status->messages;
}
?>
<?php
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'davidwalshblog#gmail.com';
$password = 'davidwalsh';
/* 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);
?>
link :Update code from
EDIT
subject - the messages subject
from - who sent it
to - recipient
date - when was it sent
message_id - Message-ID
references - is a reference to this message id
in_reply_to - is a reply to this message id
size - size in bytes
uid - UID the message has in the mailbox
msgno - message sequence number in the mailbox
recent - this message is flagged as recent
flagged - this message is flagged
answered - this message is flagged as answered
deleted - this message is flagged for deletion
seen - this message is flagged as already read
draft - this message is flagged as being a draft