-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Envelope.php
117 lines (103 loc) · 3.55 KB
/
Envelope.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Mailer;
use Symfony\Component\Mailer\Exception\InvalidArgumentException;
use Symfony\Component\Mailer\Exception\LogicException;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\RawMessage;
/**
* @author Fabien Potencier <[email protected]>
*/
class Envelope
{
private Address $sender;
private array $recipients = [];
/**
* @param Address[] $recipients
*/
public function __construct(Address $sender, array $recipients)
{
$this->setSender($sender);
$this->setRecipients($recipients);
}
public static function create(RawMessage $message): self
{
if (RawMessage::class === $message::class) {
throw new LogicException('Cannot send a RawMessage instance without an explicit Envelope.');
}
return new DelayedEnvelope($message);
}
public function setSender(Address $sender): void
{
// to ensure deliverability of bounce emails independent of UTF-8 capabilities of SMTP servers
if (!preg_match('/^[^@\x80-\xFF]++@/', $sender->getAddress())) {
throw new InvalidArgumentException(\sprintf('Invalid sender "%s": non-ASCII characters not supported in local-part of email.', $sender->getAddress()));
}
$this->sender = $sender;
}
/**
* @return Address Returns a "mailbox" as specified by RFC 2822
* Must be converted to an "addr-spec" when used as a "MAIL FROM" value in SMTP (use getAddress())
*/
public function getSender(): Address
{
return $this->sender;
}
/**
* @param Address[] $recipients
*/
public function setRecipients(array $recipients): void
{
if (!$recipients) {
throw new InvalidArgumentException('An envelope must have at least one recipient.');
}
$this->recipients = [];
foreach ($recipients as $recipient) {
if (!$recipient instanceof Address) {
throw new InvalidArgumentException(\sprintf('A recipient must be an instance of "%s" (got "%s").', Address::class, get_debug_type($recipient)));
}
$this->recipients[] = new Address($recipient->getAddress());
}
}
/**
* @return Address[]
*/
public function getRecipients(): array
{
return $this->recipients;
}
/**
* Returns true if any address' localpart contains at least one
* non-ASCII character, and false if all addresses have all-ASCII
* localparts.
*
* This helps to decide whether to the SMTPUTF8 extensions (RFC
* 6530 and following) for any given message.
*
* The SMTPUTF8 extension is strictly required if any address
* contains a non-ASCII character in its localpart. If non-ASCII
* is only used in domains (e.g. horst@freiherr-von-mühlhausen.de)
* then it is possible to send the message using IDN encoding
* instead of SMTPUTF8. The most common software will display the
* message as intended.
*/
public function anyAddressHasUnicodeLocalpart(): bool
{
if ($this->getSender()->hasUnicodeLocalpart()) {
return true;
}
foreach ($this->getRecipients() as $r) {
if ($r->hasUnicodeLocalpart()) {
return true;
}
}
return false;
}
}