| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\Mailer;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\Mailer\Exception\InvalidArgumentException;
|
|
|
15 |
use Symfony\Component\Mailer\Exception\LogicException;
|
|
|
16 |
use Symfony\Component\Mime\Address;
|
|
|
17 |
use Symfony\Component\Mime\RawMessage;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
21 |
*/
|
|
|
22 |
class Envelope
|
|
|
23 |
{
|
|
|
24 |
private Address $sender;
|
|
|
25 |
private array $recipients = [];
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* @param Address[] $recipients
|
|
|
29 |
*/
|
|
|
30 |
public function __construct(Address $sender, array $recipients)
|
|
|
31 |
{
|
|
|
32 |
$this->setSender($sender);
|
|
|
33 |
$this->setRecipients($recipients);
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
public static function create(RawMessage $message): self
|
|
|
37 |
{
|
|
|
38 |
if (RawMessage::class === $message::class) {
|
|
|
39 |
throw new LogicException('Cannot send a RawMessage instance without an explicit Envelope.');
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
return new DelayedEnvelope($message);
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
public function setSender(Address $sender): void
|
|
|
46 |
{
|
|
|
47 |
// to ensure deliverability of bounce emails independent of UTF-8 capabilities of SMTP servers
|
|
|
48 |
if (!preg_match('/^[^@\x80-\xFF]++@/', $sender->getAddress())) {
|
|
|
49 |
throw new InvalidArgumentException(sprintf('Invalid sender "%s": non-ASCII characters not supported in local-part of email.', $sender->getAddress()));
|
|
|
50 |
}
|
|
|
51 |
$this->sender = $sender;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* @return Address Returns a "mailbox" as specified by RFC 2822
|
|
|
56 |
* Must be converted to an "addr-spec" when used as a "MAIL FROM" value in SMTP (use getAddress())
|
|
|
57 |
*/
|
|
|
58 |
public function getSender(): Address
|
|
|
59 |
{
|
|
|
60 |
return $this->sender;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* @param Address[] $recipients
|
|
|
65 |
*/
|
|
|
66 |
public function setRecipients(array $recipients): void
|
|
|
67 |
{
|
|
|
68 |
if (!$recipients) {
|
|
|
69 |
throw new InvalidArgumentException('An envelope must have at least one recipient.');
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
$this->recipients = [];
|
|
|
73 |
foreach ($recipients as $recipient) {
|
|
|
74 |
if (!$recipient instanceof Address) {
|
|
|
75 |
throw new InvalidArgumentException(sprintf('A recipient must be an instance of "%s" (got "%s").', Address::class, get_debug_type($recipient)));
|
|
|
76 |
}
|
|
|
77 |
$this->recipients[] = new Address($recipient->getAddress());
|
|
|
78 |
}
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* @return Address[]
|
|
|
83 |
*/
|
|
|
84 |
public function getRecipients(): array
|
|
|
85 |
{
|
|
|
86 |
return $this->recipients;
|
|
|
87 |
}
|
|
|
88 |
}
|