OpenSource For You

Decoding the failing recipient from the bounce email

-

Now that the mail is successful­ly in the PHP script, our task will be to extract the failing recipient from the encoded email headers. Thanks to exim configurat­ions like envelope_to_add in the pipe transport (above), the VERP address gets pasted to the To header of the bounce email, and that’s the place to look for the failing recipient.

Some common regex functions to extract the headers are: function extractHea­ders( $email ) { $bounceHead­ers = array(); $lineBreaks = explode( "\n", $email ); foreach ( $lineBreaks as $lineBreak ) {

if ( preg_match( "/^To: (.*)/", $lineBreak , $toMatch )

){

$bounceHead­ers[ 'to' ] = $toMatch[1]; } if ( preg_match( "/^Subject: (.*)/", $lineBreak , $subjectMat­ch ) ) {

$bounceHead­ers[ 'subject' ] = $subjectMat­ch[1]; } if ( preg_match( "/^Date: (.*)/", $lineBreak , $dateMatch ) ) {

$bounceHead­ers[ 'date' ] = $dateMatch[1]; } if ( trim( $lineBreak ) == "" ) {

// Empty line denotes that the header part is

finished

break;

} } return $bounceHead­ers;

}

After extracting the headers, we need to decode the original-failed-recipient email ID from the VERP hashed $bounceHead­er[‘ to’], which involves more or less the reverse of what we did earlier. This would help us validate the bounced email too. /** *Considerin­g the recieved $heders[ ‘to’ ] is of the form * bounces-{ to_address }-{ delivery_timestamp }-{ encode ( to_address-delivery & timestamp ), * secretKey }@ somewhere.com

*/ $hashedTo = $headers[ ‘to’ ]’;

$to = self::extractToA­ddress( $hashedTo ); function extractToA­ddress( $hashedTo ) {

$timeNow = time();

// This will help us get the address part of address@ domain

preg_match( '~(.*?)@~', $hashedTo, $hashedSlic­e );

// This will help us cut the address part with the symbol ‘ - ‘ $hashedAddr­essPart = explode( '-', $hashedSlic­e1] ); // Now we have the prefix in $hashedAddr­essPart[ 0 - 2 ] and the hash in $hashedAddr­essPart[3]

$verpPrefix = $hashedAddr­essPart [0]. '-'. $hashedAddr­essPart 1]. '-'. hashedAddr­essPart [2]; // Extracting the bounce time. $bounceTime = $hashedAddr­essPart[ 2 ];

// Valid time for a bounce to happen. The values can be subtracted to find out the time in between and even used to set an accept time, say 3 days. if ( $bounceTime < $timeNow ) {

if ( hash_hmac( $hashAlgori­thm, $verpPrefix , $hashSecret­Key ) === $hashedAddr­essPart[3] ) {

// Bounce is valid, as

the comparison­s return true.

$to = string_replace(

‘.’, ‘@’, $verpPrefix[1] );

return $to;

} } }

Newspapers in English

Newspapers from India