Another great use for email piping is parsing bounce emails. This is very useful if you have a mailing list and need to remove any invalid or old email addresses. Sending emails to invalid addresses can land you on domain blacklists for possible spamming so it is definitely beneficial to trim any mailing lists you send out to.
Set Up Your Bounce Address
Choose a bounce email address for parsing your bounce emails. I usually choose something like email@example.com then set up the email address to pipe to a PHP script.
Setting the Return-Path Header
On any emails you want returned to your firstname.lastname@example.org address you will need to set the Return-Path header on the email. The email header should look like this:
This tells the recipient’s mail server to send any return (bounce) email to the Return-Path address instead of the address in the From header.
Parse the Bounce Email
For this example we’ll take advantage of a Bounce Handler Class to process the bounce email and determine why exactly the email was returned to us.
Pretty straight forward. We read in the email and pass it to the bounce handler. It will tell us if it failed or not. If it fails we can process that address however we deem fit (remove from mailing, etc.). See the rfc1893.error.codes.php file in the Bounce Handler Class to look at the different error types for the email to bounce. Most failed statuses will be the results of an invalid email address.