how do i track the bounced emails?

KoolKabin picture KoolKabin · Mar 23, 2011 · Viewed 12.1k times · Source

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.

Answer

mihai6744 picture mihai6744 · Sep 10, 2015

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', '[email protected]', '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.