Ed Koren
10-19-2003, 02:55 AM
Hi there,
I was wondering if anyone could have a look at a small script I wrote that I want to use to keep a mailing list up-to-date. The aim is to periodically execute it as a cron job, picking out the returned mail messages and deleting the original recipient from the mailing list. I am certainly not an advanced PHP programmer and therefore I am not sure if the script is reliable - testing it has been successful though.
// Retrieve headers from mailbox
$headers = @imap_headers($m_connect)
or die("Couldn't retrieve headers");
// Get number of messages
$num_emails = sizeof($headers);
// Loop through headers to locate Returned Mail messages
for($i = 1; $i < $num_emails+1; $i++) {
$mailHeader = @imap_headerinfo($m_connect, $i);
$from = $mailHeader->from[0];
if ($from->mailbox == "MAILER-DAEMON") {
// If returned mail message, get body of message
$b = imap_body($m_connect, $i);
// Remove occurrences of own domain from body
$b = ereg_replace("@mydomain.com","",$b);
// Check if it's a permanent error - not sure if this is a reliable way
if (ereg("permanent", $b) | ereg("fatal", $b)) {
// Fish for failed address
ereg('[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]*', $b, $regs);
$addr = $regs[0];
// Delete address from db
$sql = "DELETE * FROM mailinglist WHERE ml_addr = '$addr'";
$result = mysql_query($sql, $db);
// Mark for deletion
imap_delete ($m_connect, $i);
}
}
// Delete all messages marked for deletion
imap_expunge ($m_connect);
}
imap_close($m_connect);
Any suggestions to improve it would be very much appreciated.
Ed
I was wondering if anyone could have a look at a small script I wrote that I want to use to keep a mailing list up-to-date. The aim is to periodically execute it as a cron job, picking out the returned mail messages and deleting the original recipient from the mailing list. I am certainly not an advanced PHP programmer and therefore I am not sure if the script is reliable - testing it has been successful though.
// Retrieve headers from mailbox
$headers = @imap_headers($m_connect)
or die("Couldn't retrieve headers");
// Get number of messages
$num_emails = sizeof($headers);
// Loop through headers to locate Returned Mail messages
for($i = 1; $i < $num_emails+1; $i++) {
$mailHeader = @imap_headerinfo($m_connect, $i);
$from = $mailHeader->from[0];
if ($from->mailbox == "MAILER-DAEMON") {
// If returned mail message, get body of message
$b = imap_body($m_connect, $i);
// Remove occurrences of own domain from body
$b = ereg_replace("@mydomain.com","",$b);
// Check if it's a permanent error - not sure if this is a reliable way
if (ereg("permanent", $b) | ereg("fatal", $b)) {
// Fish for failed address
ereg('[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]*', $b, $regs);
$addr = $regs[0];
// Delete address from db
$sql = "DELETE * FROM mailinglist WHERE ml_addr = '$addr'";
$result = mysql_query($sql, $db);
// Mark for deletion
imap_delete ($m_connect, $i);
}
}
// Delete all messages marked for deletion
imap_expunge ($m_connect);
}
imap_close($m_connect);
Any suggestions to improve it would be very much appreciated.
Ed