De­cod­ing the fail­ing re­cip­i­ent from the bounce email

OpenSource For You - - HOW TO ADMIN -

Now that the mail is suc­cess­fully in the PHP script, our task will be to ex­tract the fail­ing re­cip­i­ent from the en­coded email head­ers. Thanks to exim con­fig­u­ra­tions like en­velope_­to_add in the pipe trans­port (above), the VERP ad­dress gets pasted to the To header of the bounce email, and that’s the place to look for the fail­ing re­cip­i­ent.

Some common regex func­tions to ex­tract the head­ers are: func­tion ex­trac­tHead­ers( $email ) { $bounceHead­ers = ar­ray(); $line­Breaks = ex­plode( "\n", $email ); fore­ach ( $line­Breaks as $line­Break ) {

if ( preg_­match( "/^To: (.*)/", $line­Break , $toMatch )

){

$bounceHead­ers[ 'to' ] = $toMatch[1]; } if ( preg_­match( "/^Sub­ject: (.*)/", $line­Break , $sub­jec­tMatch ) ) {

$bounceHead­ers[ 'sub­ject' ] = $sub­jec­tMatch[1]; } if ( preg_­match( "/^Date: (.*)/", $line­Break , $dateMatch ) ) {

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

// Empty line de­notes that the header part is

fin­ished

break;

} } re­turn $bounceHead­ers;

}

After ex­tract­ing the head­ers, we need to de­code the orig­i­nal-failed-re­cip­i­ent email ID from the VERP hashed $bounceHeader[‘ to’], which in­volves more or less the re­verse of what we did ear­lier. This would help us val­i­date the bounced email too. /** *Con­sid­er­ing the re­cieved $hed­ers[ ‘to’ ] is of the form * bounces-{ to_ad­dress }-{ de­liv­ery_­times­tamp }-{ en­code ( to_ad­dress-de­liv­ery & time­stamp ), * se­cretKey }@ some­where.com

*/ $hashedTo = $head­ers[ ‘to’ ]’;

$to = self::ex­trac­tToAd­dress( $hashedTo ); func­tion ex­trac­tToAd­dress( $hashedTo ) {

$timeNow = time();

// This will help us get the ad­dress part of ad­[email protected] do­main

preg_­match( '~(.*?)@~', $hashedTo, $hashedSlice );

// This will help us cut the ad­dress part with the sym­bol ‘ - ‘ $hashedAd­dressPart = ex­plode( '-', $hashedSlice1] ); // Now we have the pre­fix in $hashedAd­dressPart[ 0 - 2 ] and the hash in $hashedAd­dressPart[3]

$ver­pPre­fix = $hashedAd­dressPart [0]. '-'. $hashedAd­dressPart 1]. '-'. hashedAd­dressPart [2]; // Ex­tract­ing the bounce time. $bounceTime = $hashedAd­dressPart[ 2 ];

// Valid time for a bounce to hap­pen. The val­ues can be sub­tracted to find out the time in be­tween and even used to set an ac­cept time, say 3 days. if ( $bounceTime < $timeNow ) {

if ( hash_h­mac( $hashAl­go­rithm, $ver­pPre­fix , $hashSe­cretKey ) === $hashedAd­dressPart[3] ) {

// Bounce is valid, as

the com­par­isons re­turn true.

$to = string_re­place(

‘.’, ‘@’, $ver­pPre­fix[1] );

re­turn $to;

} } }

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.