A simple test to detect a permanent bounce

There are n chances for an email to get bounced back after being rejected and discussing it all here is out of scope of this post, and are broadly categorized into permanent (hard) and temporary (soft) bounces. Jotting down some reasons for a temporary bounces:

  • A server is unavailable or down -Network failure
  • The server is overloaded
  • Recipient mailbox is over-quota
  • Too long e-mail message
  • Vacation response is on

The reasons for permanent failures are still many, and those who are desirous, can check here.
Only the permanent bounces needs to get parsed, and actions taken upon, while temporary needs to be neglected. Since there are another n number of cases for a bounce to occur, there are n varieties of bounce emails too. We were searching for how to filter only the permanent bounces out.
We hit with this exim4-doc here and found that, exim4 adds an X-Failed-Recipients: header to permanent bounces. Sticking to that, filtering out the permanent bounces is a little regex code.

/**
  * Filter a given boucne for hard or soft
  *
  * @param string $email
  * @return array $headers
  */
protected static function checkForPermanentBounce( $email ) {
	$emailLines = explode( "\n", $email );
	foreach ( $emailLines as $emailLine ) {
		if ( preg_match( "/^X-Failed-Recipients: (.*)/", $emailLine, $failureMatch ) ) {
			$headers[ 'permanentFailure' ] = $failureMatch;
		}
		if ( trim( $emailLine ) == "" ) {
			// Empty line denotes that the header part is finished
			break;
		}
	}

	return $headers;
}

Now, on the calling function, you can just check:

$permanentFailure = $emailHeaders[ 'permanentFailure'];
if ( $permanentFailure !== null ) {
       //code to do processing!
}

Yay! Thats it.
PS: We sticked to exim, as mchnery uses exim. There are still chances of a bounce from an internal server, and we are working on how to fix that too. Thanks

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s