Skip to content

Commit 89f8fd0

Browse files
committed
Message: added support for quoted "display-name" <email> [Closes #64]
see https://tools.ietf.org/html/rfc2822
1 parent f60b1fb commit 89f8fd0

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

src/Mail/Message.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,13 @@ public function addBcc(string $email, string $name = null)
142142
private function formatEmail(string $email, string $name = null): array
143143
{
144144
if (!$name && preg_match('#^(.+) +<(.*)>\z#', $email, $matches)) {
145-
return [$matches[2] => $matches[1]];
146-
} else {
147-
return [$email => $name];
145+
[, $name, $email] = $matches;
146+
$tmp = substr($name, 1, -1);
147+
if ($name === '"' . $tmp . '"') {
148+
$name = stripslashes($tmp);
149+
}
148150
}
151+
return [$email => $name];
149152
}
150153

151154

tests/Mail/Mail.email.parse.phpt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Mail\Message email address parsing.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Nette\Mail\Message;
10+
use Tester\Assert;
11+
12+
13+
require __DIR__ . '/../bootstrap.php';
14+
15+
16+
$mail = new Message;
17+
18+
$mail->setFrom('[email protected]');
19+
Assert::same(['[email protected]' => null], $mail->getFrom());
20+
21+
$mail->setFrom('[email protected]', 'Žluťoučký kůň');
22+
Assert::same(['[email protected]' => 'Žluťoučký kůň'], $mail->getFrom());
23+
24+
$mail->setFrom('Žluťoučký kůň <[email protected]>');
25+
Assert::same(['[email protected]' => 'Žluťoučký kůň'], $mail->getFrom());
26+
27+
$mail->setFrom('Žluťoučký "kůň" <[email protected]>');
28+
Assert::same(['[email protected]' => 'Žluťoučký "kůň"'], $mail->getFrom());
29+
30+
$mail->setFrom('"Žluťoučký kůň" <[email protected]>');
31+
Assert::same(['[email protected]' => 'Žluťoučký kůň'], $mail->getFrom());
32+
33+
$mail->setFrom('"Žluťouč\"k\ý kůň" <[email protected]>');
34+
Assert::same(['[email protected]' => 'Žluťouč"ký kůň'], $mail->getFrom());
35+
36+
$mail->setFrom('The\Mail <[email protected]>');
37+
Assert::same(['[email protected]' => 'The\Mail'], $mail->getFrom());
38+
39+
$mail->setFrom('The.Mail <[email protected]>');
40+
Assert::same(['[email protected]' => 'The.Mail'], $mail->getFrom());

0 commit comments

Comments
 (0)