Skip to content
Permalink
Browse files

Message: added support for quoted "display-name" <email> [Closes #64]

  • Loading branch information...
dg committed Jun 3, 2019
1 parent f60b1fb commit 89f8fd05c97530ac89da47acddeb399de4235758
Showing with 46 additions and 3 deletions.
  1. +6 −3 src/Mail/Message.php
  2. +40 −0 tests/Mail/Mail.email.parse.phpt
@@ -142,10 +142,13 @@ public function addBcc(string $email, string $name = null)
private function formatEmail(string $email, string $name = null): array
{
if (!$name && preg_match('#^(.+) +<(.*)>\z#', $email, $matches)) {
return [$matches[2] => $matches[1]];
} else {
return [$email => $name];
[, $name, $email] = $matches;
$tmp = substr($name, 1, -1);
if ($name === '"' . $tmp . '"') {
$name = stripslashes($tmp);

This comment has been minimized.

Copy link
@WinterSilence

WinterSilence Jun 5, 2019

then u need strip stripslashes in any case: return [$email => stripslashes($name)];

}
}
return [$email => $name];
}
@@ -0,0 +1,40 @@
<?php
/**
* Test: Nette\Mail\Message email address parsing.
*/
declare(strict_types=1);
use Nette\Mail\Message;
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
$mail = new Message;
$mail->setFrom('kun1@example.com');
Assert::same(['kun1@example.com' => null], $mail->getFrom());
$mail->setFrom('kun1@example.com', 'Žluťoučký kůň');
Assert::same(['kun1@example.com' => 'Žluťoučký kůň'], $mail->getFrom());
$mail->setFrom('Žluťoučký kůň <kun1@example.com>');
Assert::same(['kun1@example.com' => 'Žluťoučký kůň'], $mail->getFrom());
$mail->setFrom('Žluťoučký "kůň" <kun1@example.com>');
Assert::same(['kun1@example.com' => 'Žluťoučký "kůň"'], $mail->getFrom());
$mail->setFrom('"Žluťoučký kůň" <kun1@example.com>');
Assert::same(['kun1@example.com' => 'Žluťoučký kůň'], $mail->getFrom());
$mail->setFrom('"Žluťouč\"k\ý kůň" <kun1@example.com>');
Assert::same(['kun1@example.com' => 'Žluťouč"ký kůň'], $mail->getFrom());
$mail->setFrom('The\Mail <kun1@example.com>');
Assert::same(['kun1@example.com' => 'The\Mail'], $mail->getFrom());
$mail->setFrom('The.Mail <kun1@example.com>');
Assert::same(['kun1@example.com' => 'The.Mail'], $mail->getFrom());

0 comments on commit 89f8fd0

Please sign in to comment.
You can’t perform that action at this time.