imap_open Copy email to folder and then Delete the email - php

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

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

Php accessing the email inbox for email address

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

IMAP Live/Constant Sync like in Gmail

In Gmail, new emails are displayed instantaneously without page refresh. I can retrieve my gmail emails using PHP and IMAP. How could I implement or add this instantaneous/constant sync without page refresh? Here is the basic code:
http://pastebin.com/ivfbMSZk
$inbox = imap_open($hostname,$username,$password,NULL,1) or die('Cannot connect to Gmail: ' . print_r(imap_errors()));
$emails = imap_search($inbox,'ALL');
if($emails) {
$output = '';
rsort($emails);
foreach($emails as $email_number) {
$message = imap_fetchbody($inbox,$email_number,1);
$output.= $message;
}
echo $output . "\n";
}
You would need something like the comet model, there are questions like this one rewarding this subject, on this, php works for light traffic only.

how do i track the bounced emails?

I would like to track the bounced emails of that has been sent from my server. I read few stuffs and found that the bounced emails are stored in mailbox and can be detected by reading the mailbox files directly.
check for bounced mails with php
Now i would like to get some idea how do i read the mailbox files of my server? Do i need to run a php script file manually after sending the mail to record the bounced email to my database? Do i need to parse the email contents to findout which email has been bounced?
I am targeting the stuff for my php server with pop email access.
Here is how I connect to the incoming mail server at one.com
$inbox = imap_open('{imap.one.com:993/imap/ssl/novalidate-cert}INBOX', 'your#address.com', 'xxxxxxxx') or die('Cannot connect: ' . print_r(imap_errors(), true));
/* grab emails */
$emails = imap_search($inbox,'ALL');
/* 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) {
$message = imap_fetchbody($inbox,$email_number,2);
$pieces = explode(" ", $message);
foreach($pieces as $piece){
$findme = '#';
//$findme2 = '.com';
$pos = strpos($piece, $findme);
if ($pos !== false) {
echo $piece;
}
}
}
}
The email address that bounced is in the body of the message and I echo it to the browser.

Categories