Skip to content

Commit

Permalink
fix cc header parse
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Kolomiiets committed Jan 24, 2017
1 parent d0ca083 commit c9fd228
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions Infrastructure/Message/Zend/AbstractMessageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,7 @@ protected function processMessageId($headers)
public function processFrom($headers)
{
try {
$from = $headers->toArray()['From'];

preg_match('/^((?P<name>.*?)<(?P<namedEmail>[^>]+)>|(?P<email>.+))/', $from, $matches);

if (array_key_exists('email', $matches)) {
$email = explode(',', $matches['email'])[0];
$name = explode('@', $email)[0];
} else {
$name = $matches['name'];
$email = $matches['namedEmail'];
}

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new \RuntimeException(sprintf('This %s email address is considered invalid.'), $email);
}
list($name, $email) = $this->parseFrom($headers);

return new MessageSender($email, $name);
} catch (\RuntimeException $e) {
Expand All @@ -80,6 +66,31 @@ public function processFrom($headers)
}
}

/**
* @param \Zend\Mail\Headers $headers
* @return array
*/
protected function parseFrom($headers)
{
$from = $headers->toArray()['From'];

preg_match('/^((?P<name>.*?)<(?P<namedEmail>[^>]+)>|(?P<email>.+))/', $from, $matches);

if (array_key_exists('email', $matches)) {
$email = explode(',', $matches['email'])[0];
$name = explode('@', $email)[0];
} else {
$name = $matches['name'];
$email = $matches['namedEmail'];
}

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new \RuntimeException(sprintf('This %s email address is considered invalid.'), $email);
}

return [$name, $email];
}

/**
* @param \Zend\Mail\Headers $headers
* @return string
Expand All @@ -102,7 +113,7 @@ public function processTo($headers)
*/
public function processRecipients($headers)
{
$processAddressTypes = ['to', 'cc', 'from'];
$processAddressTypes = ['to', 'cc'];
$recipients = [];

foreach ($processAddressTypes as $type) {
Expand All @@ -118,6 +129,9 @@ public function processRecipients($headers)
}
}

list($name, $email) = $this->parseFrom($headers);
$recipients[] = new MessageRecipient($email, $name);

return $recipients;
}

Expand Down

0 comments on commit c9fd228

Please sign in to comment.