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!

Advertisements

One thought on “Parsing email for relevant headers

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