ok, let me elaborate with some code that i have written.
I am using imap function in php to open a particular mailbox (inbox) and fetch the mails. Particularly i want to fetch all bounced mails. So following is the code:
$hostname="{124.163.948.918:110/pop3/novalidate-cert}INBOX";
$username = 'dfgd';
$password = 'dgdooopoo';
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Graphserver: ' . imap_last_error());
$emails = imap_search($inbox,'SUBJECT "Undelivered Mail Returned to Sender" ');
rsort($emails);
foreach($emails as $email_number) {
$overview = imap_fetch_overview($inbox,$email_number,1);
$message = imap_fetchbody($inbox,$email_number,1);
$find= "To: "; // look for the 'To' part
$foundatloc= strpos($message,$find);
if ($foundatloc > 1){ // test if a mail adres was found
$findend = "\n";
$foundendloc = strpos($message,$findend,$foundatloc); // find the location of EOL
$foundendloc = $foundendloc -($foundatloc + 5);
$adres = substr($message,$foundatloc + 5,$foundendloc);
$adres = str_replace("<","",$adres); // strip <, > and spaces
$adres = str_replace(">","",$adres);
$adres = trim($adres);
$arr=explode(' ',$adres);
print($arr[1]);
//$arr[1] contains the bounced address !!!
}
else{
$listemails = extract_emails_from($message);
print(implode("\n", $listemails));
}
print('<br>');
}
//There is a function that sends the mail
//After that i call the above code to catch bounced mails
Now, the problem is that i do not get the most recent bounced mail, i get it only the next time i run this script. So i am finding some way to refresh the inbox before fetching the mails.
I hope my problem is clear now.
pls help....
