I am trying to fetch email using imap function which return very old email. Following script i am trying
$imap = #imap_open("{mail.******.com:143/novalidate-cert}", $sEmail, $sPwd);
$message_count = #imap_num_msg($imap);
for ($m = 1; $m <= $message_count; ++$m){
$header = #imap_rfc822_parse_headers(imap_fetchheader($imap, $m));
$email[$m]['from'] = $header->from[0]->mailbox.'#'.$header->from[0]->host;
}
Currently above function return email in ascending order, so that's why very old emails (2015's Email) are coming first. So is there any way to get recent email first.
Related
I am using IMAP to read my last 3 unseen emails.
I am using php. The email provider is fastmail.
I use the following approach.
I get the emails where $d is yesterday.
$s = 'SUBJECT "New email from " SINCE "'.$d.'" UNSEEN';
$emails = imap_search($this->conn,$s);
$emails is an array of email ids.
I loop over them like so:
foreach ($emails as $index => $i) {
$message = imap_body($this->conn, $i) . '<br>';
$overview = imap_fetch_overview($this->conn, $i, 0);
$structure = imap_fetchstructure($this->conn, $i);
if (!empty($overview[0])) {
$subject = $overview[0]->subject;
}
//from this point i get some html and i just process it.
$mes = imap_body($this->conn, $i);
}
How can i make the whole process faster? Any ideas?
I am not an expert in IMAP.
Is there any way that i can just get the text body, and not the rest of the email?
Thanks in advance!
I'm currently building a php scripts which fetches the email from a server using imap functions and stores the details in the database.
My problem is I dont know how to identify new mails from old mails that already exists.
and how to get reply mails sent to the mail
use the UID message to determine the last message, you have to store the last UID in the table
$uidsArray = imap_sort($imapConnection, SORTARRIVAL, 1, SE_UID);
if ($uidsArray) {
// read UID last message, XEmailUID - table(mailbox, lastuid mailbox)
$lastUIDObject = new XEmailUID();
$lastUIDObject->setImap($mailbox->getId().'/'.$mailboxRef);
if (!$lastUIDObject->select()) {
$lastUIDObject->insert();
}
$uidMax = 0;
foreach ($uidsArray as $uid) {
if ($uid < $lastUIDObject->getUid()) {
continue;
}
if ($uid >= $uidMax) {
$uidMax = $uid;
}
// your function
$this->_readIMAPMessage(
$imapConnection,
$uid,
$mailboxRef
);
}
if ($uidMax > 0) {
$lastUIDObject->setUid($uidMax);
$lastUIDObject->update();
}
}
I need to read an email and then send a value in that email into another page. I've successful to read email and detect subject and date of that email. After read that incoming email, I need to send that value into another php page whoch in that page I can do background process.
Here's the code to read email
include "connect.php";
$mailbox = imap_open("{mail.xxxxx.com:143/novalidate-cert}INBOX", "username", "password"); //Note 1
$count = imap_num_msg($mailbox);
$headers = imap_headerinfo($mailbox, $count);
$sub = $headers->subject;
$time = $headers->date;
$pieces = explode("://",$sub);
$check = mysql_query("SELECT * FROM email WHERE web='$pieces[1]' AND date='$time'");
$data = mysql_fetch_array($check);
if($time != $date['date']) {
//code to send parameter here
echo "enter";
} else {
echo "no";
}
I need to send an email subject and email date into another php page. What ways can be used ? Is that with javascript, ajax or what ?
Thanks for your help,
I want to connect to an IMAP-server and find all E-Mails that were sent to abc#server.tld. I tried:
$mbox = imap_open("{imap.server.tld/norsh}", "imap#server.tld", "5ecure3");
$result = imap_search($mbox, "TO \"abc#server.tld\"", SE_UID);
but this also listed e-Mails that were sent e.g. to 123abc#server.tld. Is it somehow possible to do a search for exact matches?
Short answer: you can't. I didn't find anything in RFC 2060 - Internet Message Access Protocol - Version 4rev1 saying that it can be done.
But, there is a workaround. First fetch all emails that contain abc#server.tld, then iterate through the results and select only the exact matches.
$searchEmail = "abc#server.tld";
$emails = imap_search($mbox, "TO $searchEmail");
$exactMatches = array();
foreach ($emails as $email) {
// get email headers
$info = imap_headerinfo($mbox, $email);
// fetch all emails in the TO: header
$toAddresses = array();
foreach ($info->to as $to) {
$toAddresses[] = $to->mailbox . '#' . $to->host;
}
// is there a match?
if (in_array($searchEmail, $toAddresses)) {
$exactMatches[] = $email;
}
}
Now you have all emails matching abc#server.tld in $exactMatches
I'm trying to figure out how to get the latest 3 emails (SEEN and UNSEEN) using imap and php. It need to be ressource-efficient since the mailbox as 1 000 emails inside. Getting all header may need too much ressources I think.
I just need the sender, the subject and the date...
Any idea? Thanks for any syggestion/help/explaination/hint...
I did it like that:
$mbox = imap_open("{imap.myconnection.com:993/imap/ssl}INBOX", "username", "password");
// get information about the current mailbox (INBOX in this case)
$mboxCheck = imap_check($mbox);
// get the total amount of messages
$totalMessages = $mboxCheck->Nmsgs;
// select how many messages you want to see
$showMessages = 5;
// get those messages
$result = array_reverse(imap_fetch_overview($mbox,($totalMessages-$showMessages+1).":".$totalMessages));
// iterate trough those messages
foreach ($result as $mail) {
print_r($mail);
// if you want the mail body as well, do it like that. Note: the '1.1' is the section, if a email is a multi-part message in MIME format, you'll get plain text with 1.1
$mailBody = imap_fetchbody($mbox, $mail->msgno, '1.1');
// but if the email is not a multi-part message, you get the plain text in '1'
if(trim($mailBody)=="") {
$mailBody = imap_fetchbody($mbox, $mail->msgno, '1');
}
// just an example output to view it - this fit for me very nice
echo nl2br(htmlentities(quoted_printable_decode($mailBody)));
}
imap_close($mbox);
PHP-Ref IMAP: http://php.net/manual/en/ref.imap.php
Regards
Dominic
What about
imap_search($res, 'RECENT');
?
http://php.net/manual/en/function.imap-search.php
$msgnos = imap_search($mbox, "UNSEEN", SE_UID);
$i=0;
foreach($msgnos as $msgUID) {
$msgNo = imap_msgno($mbox, $msgUID);
$head = imap_headerinfo($mbox, $msgNo);
$mail[$i][] = $msgUID;
$mail[$i][] = $head->Recent;
$mail[$i][] = $head->Unseen;
$mail[$i][] = $head->from[0]->mailbox."#".$head->from[0]->host;
$mail[$i][] = utf8_decode(imap_utf8($head->subject));
$mail[$i][] = $head->udate;
}
return $mail;
imap_close($mbox);
Will do the job.