| 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\Mime; |
| 13 |
|
| 14 |
use Symfony\Component\Mime\Exception\LogicException; |
| 15 |
use Symfony\Component\Mime\Header\Headers; |
| 16 |
use Symfony\Component\Mime\Part\AbstractPart; |
| 17 |
use Symfony\Component\Mime\Part\TextPart; |
| 18 |
|
| 19 |
/** |
| 20 |
* @author Fabien Potencier <fabien@symfony.com> |
| 21 |
*/ |
| 22 |
class Message extends RawMessage |
| 23 |
{ |
| 24 |
private $headers; |
| 25 |
private $body; |
| 26 |
|
| 27 |
public function __construct(?Headers $headers = null, ?AbstractPart $body = null) |
| 28 |
{ |
| 29 |
$this->headers = $headers ? clone $headers : new Headers(); |
| 30 |
$this->body = $body; |
| 31 |
} |
| 32 |
|
| 33 |
public function __clone() |
| 34 |
{ |
| 35 |
$this->headers = clone $this->headers; |
| 36 |
|
| 37 |
if (null !== $this->body) { |
| 38 |
$this->body = clone $this->body; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* @return $this |
| 44 |
*/ |
| 45 |
public function setBody(?AbstractPart $body = null) |
| 46 |
{ |
| 47 |
$this->body = $body; |
| 48 |
|
| 49 |
return $this; |
| 50 |
} |
| 51 |
|
| 52 |
public function getBody(): ?AbstractPart |
| 53 |
{ |
| 54 |
return $this->body; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* @return $this |
| 59 |
*/ |
| 60 |
public function setHeaders(Headers $headers) |
| 61 |
{ |
| 62 |
$this->headers = $headers; |
| 63 |
|
| 64 |
return $this; |
| 65 |
} |
| 66 |
|
| 67 |
public function getHeaders(): Headers |
| 68 |
{ |
| 69 |
return $this->headers; |
| 70 |
} |
| 71 |
|
| 72 |
public function getPreparedHeaders(): Headers |
| 73 |
{ |
| 74 |
$headers = clone $this->headers; |
| 75 |
|
| 76 |
if (!$headers->has('From')) { |
| 77 |
if (!$headers->has('Sender')) { |
| 78 |
throw new LogicException('An email must have a "From" or a "Sender" header.'); |
| 79 |
} |
| 80 |
$headers->addMailboxListHeader('From', [$headers->get('Sender')->getAddress()]); |
| 81 |
} |
| 82 |
|
| 83 |
if (!$headers->has('MIME-Version')) { |
| 84 |
$headers->addTextHeader('MIME-Version', '1.0'); |
| 85 |
} |
| 86 |
|
| 87 |
if (!$headers->has('Date')) { |
| 88 |
$headers->addDateHeader('Date', new \DateTimeImmutable()); |
| 89 |
} |
| 90 |
|
| 91 |
// determine the "real" sender |
| 92 |
if (!$headers->has('Sender') && \count($froms = $headers->get('From')->getAddresses()) > 1) { |
| 93 |
$headers->addMailboxHeader('Sender', $froms[0]); |
| 94 |
} |
| 95 |
|
| 96 |
if (!$headers->has('Message-ID')) { |
| 97 |
$headers->addIdHeader('Message-ID', $this->generateMessageId()); |
| 98 |
} |
| 99 |
|
| 100 |
// remove the Bcc field which should NOT be part of the sent message |
| 101 |
$headers->remove('Bcc'); |
| 102 |
|
| 103 |
return $headers; |
| 104 |
} |
| 105 |
|
| 106 |
public function toString(): string |
| 107 |
{ |
| 108 |
if (null === $body = $this->getBody()) { |
| 109 |
$body = new TextPart(''); |
| 110 |
} |
| 111 |
|
| 112 |
return $this->getPreparedHeaders()->toString().$body->toString(); |
| 113 |
} |
| 114 |
|
| 115 |
public function toIterable(): iterable |
| 116 |
{ |
| 117 |
if (null === $body = $this->getBody()) { |
| 118 |
$body = new TextPart(''); |
| 119 |
} |
| 120 |
|
| 121 |
yield $this->getPreparedHeaders()->toString(); |
| 122 |
yield from $body->toIterable(); |
| 123 |
} |
| 124 |
|
| 125 |
public function ensureValidity() |
| 126 |
{ |
| 127 |
$to = (null !== $header = $this->headers->get('To')) ? $header->getBody() : null; |
| 128 |
$cc = (null !== $header = $this->headers->get('Cc')) ? $header->getBody() : null; |
| 129 |
$bcc = (null !== $header = $this->headers->get('Bcc')) ? $header->getBody() : null; |
| 130 |
|
| 131 |
if (!$to && !$cc && !$bcc) { |
| 132 |
throw new LogicException('An email must have a "To", "Cc", or "Bcc" header.'); |
| 133 |
} |
| 134 |
|
| 135 |
$from = (null !== $header = $this->headers->get('From')) ? $header->getBody() : null; |
| 136 |
$sender = (null !== $header = $this->headers->get('Sender')) ? $header->getBody() : null; |
| 137 |
|
| 138 |
if (!$from && !$sender) { |
| 139 |
throw new LogicException('An email must have a "From" or a "Sender" header.'); |
| 140 |
} |
| 141 |
|
| 142 |
parent::ensureValidity(); |
| 143 |
} |
| 144 |
|
| 145 |
public function generateMessageId(): string |
| 146 |
{ |
| 147 |
if ($this->headers->has('Sender')) { |
| 148 |
$sender = $this->headers->get('Sender')->getAddress(); |
| 149 |
} elseif ($this->headers->has('From')) { |
| 150 |
if (!$froms = $this->headers->get('From')->getAddresses()) { |
| 151 |
throw new LogicException('A "From" header must have at least one email address.'); |
| 152 |
} |
| 153 |
$sender = $froms[0]; |
| 154 |
} else { |
| 155 |
throw new LogicException('An email must have a "From" or a "Sender" header.'); |
| 156 |
} |
| 157 |
|
| 158 |
return bin2hex(random_bytes(16)).strstr($sender->getAddress(), '@'); |
| 159 |
} |
| 160 |
|
| 161 |
public function __serialize(): array |
| 162 |
{ |
| 163 |
return [$this->headers, $this->body]; |
| 164 |
} |
| 165 |
|
| 166 |
public function __unserialize(array $data): void |
| 167 |
{ |
| 168 |
[$this->headers, $this->body] = $data; |
| 169 |
} |
| 170 |
} |
| 171 |
|