Php accessing the email inbox for email address - php

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

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

Storing Email from IMAP(Unique Email ID)?

How do i store a UNIQUE ID from that email in my database.
I tried $overview = imap_fetch_overview($inbox,$email_number,0); and i received a bunch of numbers but the problem is the number will change when one of the email is deleted.
How do I store it properly? MD5 the message or something?
Basically i'm trying to receive email on my personal web application where i can manage and access my own email. It calls gmail using imap.
Anyway where i can check and store only new emails?
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'xxxx#gmail.com';
$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');
dd($emails);
/* 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);
The UID exists for this purpose, it should remain unchanged unless the server gives a different UIDVALIDITY value.
https://www.rfc-editor.org/rfc/rfc3501#section-2.3.1.1
The UID is part of the IMAP standard so it should be correctly implemented by all IMAP servers. If you are only working against gmail, then you might want to look into another value though, since the same message can be seen under different labels, and then the UID might be different for that logically identical message. I don't know the gmail API though.

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

imap_open Copy email to folder and then Delete the email

I am looping through emails using the PHP function imap_open
Once the loop on each email has finished, how can i move or copy it to a specific folder then delete it?
Here is my code:
//try to connect
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect: ' . 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) {
//run queries etc here
}
}
At the moment I am just deleting the email using:
//delete the email
//this deletes the email
imap_delete($inbox,1);
//this bit really really deletes the email
imap_expunge($inbox);

Categories