Parsing email for relevant headers

Often people love to show off their regex skills, but makes a lot of confusion to people like me trying to understand whats being grepped out.
Problem :
* Extract the To, Subject and Date from an email, received by POST or whatever.
Solution :

/**
  * Extract the required headers from the received email
  *
  * @param $email
  * @return string
  */
protected function getHeaders( $email ) {
	$emailLines = explode( "\n", $email );
	foreach ( $emailLines as $emailLine ) {
		if ( preg_match( "/^To: (.*)/", $emailLine, $toMatch ) ) {
			$headers[ 'to' ] = $toMatch[1];
		}
		if ( preg_match( "/^Subject: (.*)/", $emailLine, $subjectMatch ) ) {
			$headers[ 'subject' ] = $subjectMatch[1];
		}
		if ( preg_match( "/^Date: (.*)/", $emailLine, $dateMatch ) ) {
			$headers[ 'date' ] = $dateMatch[1];
		}
		if ( trim( $emailLine ) == "" ) {
			// Empty line denotes that the header part is finished
			break;
		}
	}
	return $headers;
}

.
Now the headers can be used in the calling function by

$to = $emailHeaders[ 'to' ];
$subject = $emailHeaders[ 'subject' ];
$emailDate = $emailHeaders[ 'date' ];

Used here (https://github.com/wikimedia/mediawiki-extensions-BounceHandler).
PS: Please note that Date timestamp will be of the type RFC 2822. (Tue, 17 Jun 2014 05:53:13 GMT ). Happy Hacking again!

Altering e-mail ‘Return-Path’ with exim4 and PHP mailer in Wikitech Labs

While implementing VERP, I always found that the ‘Return-Path’ address of any send email — was getting rewritten to root@wikimedia.org. This was frustrating as I had already changed the $header['Return-Path'] many times in the UserMailer.php and the result was just this one.
Problem:-
* Rewrite custom generated Return-Path to email-headers while sending
* Implement VERP addressing to handle bounces
Solution:-
*The PHP part:
+ Make sure the UserMailer.php ( or anyother file that sends the mail ) have custom header configurations such as:

$headers[ ‘Return-Path’ ] = ‘return-to-here@domain’ ;
mail( $to, $sub, $body, $headers );
*Now, the exim4 part:
+ Open your sudo nano /etc/exim4/exim4.conf and add the following line to the main configuration area:
( optional )

# Return Path
return_path_remove = false 

+ Remove the line errors_to from the desired router (( here wiki_mail router )

wiki_mail:
driver = manualroute
condition = ${if eqi{$header_X-Mailer:}{MediaWiki mailer}}
errors_to = ${if def:h_return-path: {${address:$h_return-path:}} fail}
headers_remove = return-path
transport = remote_smtp
route_list = *     

Yay! all done. Now, $ sudo /etc/init.d/exim4 restart
Now test it by sending a mail to some remote id, using Special:EmailUser
PS: There is a shorter version, with just removing the errors_to from the wiki_mail router.