Details
-
Bug
-
Status: Open
-
Minor
-
Resolution: Unresolved
-
None
Description
The function ezcMailTools::parseEmailAddress is used to parse a string containing the email address as found in a header and parse it into a standard structure. The function as currently written accepts addresses in the following forms:
user@domain
<user@domain>
Real Name <user@domain>
but does not accept the legal (but depreciated) format (I am using the tool to read archives in mbox format and have a lot of data using this format)
user@domain (Real Name)
(i.e. using the comment function to define the real name).
This is made worse by the fact that not only does the routine not parse out Real Name, but totally doesn't detect this as an email address.
This format might have been missed as it comes about not as a direct production of the grammar but because comments are treated as a form of white space which is a delimiter.
The below code is a revised version of the function that adds parsing for this form. I have expanded the parsing in the RE to include productions for the "real name" part, and placing them into named subexpressions (so code is less brittle to later expression changes).
public static function parseEmailAddress( $address, $encoding = "mime" ) { // we don't care about the "group" part of the address since this is not used anywhere $matches = array(); $chars = '[a-zA-Z0-9!#\$\%\&\'\*\+\-\/=\?\^_`{\|}~\.]+'; $pattern = '/^\s*(?<name>.*?)<?\"?(?<local>' . $chars . ')\"?@(?<domain>' . $chars . ')>?\s*(?:\((?<name2>.*)\))?\s*$/'; $res = preg_match( $pattern, $address, $matches); if( $res != 1) { return null; } $mail = $matches['local'] . '@' . $matches['domain']; if(isset($matches['name2']) && !empty($matches['name2'])) { $name = $matches['name2']; } else { $name = $matches['name']; } $name = trim( $name, ' '); if ( $encoding == 'mime' ) { // the name may contain interesting character encoding. We need to convert it. $name = ezcMailTools::mimeDecode( $name ); } else { $name = ezcMailCharsetConverter::convertToUTF8( $name, $encoding ); } $address = new ezcMailAddress( $mail, $name, 'utf-8' ); return $address; }