Open Journal Systems  3.3.0
Message.php
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 
18 
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 
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 
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  $headers->addTextHeader('MIME-Version', '1.0');
84 
85  if (!$headers->has('Date')) {
86  $headers->addDateHeader('Date', new \DateTimeImmutable());
87  }
88 
89  // determine the "real" sender
90  if (!$headers->has('Sender') && \count($froms = $headers->get('From')->getAddresses()) > 1) {
91  $headers->addMailboxHeader('Sender', $froms[0]);
92  }
93 
94  if (!$headers->has('Message-ID')) {
95  $headers->addIdHeader('Message-ID', $this->generateMessageId());
96  }
97 
98  // remove the Bcc field which should NOT be part of the sent message
99  $headers->remove('Bcc');
100 
101  return $headers;
102  }
103 
104  public function toString(): string
105  {
106  if (null === $body = $this->getBody()) {
107  $body = new TextPart('');
108  }
109 
110  return $this->getPreparedHeaders()->toString().$body->toString();
111  }
112 
113  public function toIterable(): iterable
114  {
115  if (null === $body = $this->getBody()) {
116  $body = new TextPart('');
117  }
118 
119  yield $this->getPreparedHeaders()->toString();
120  yield from $body->toIterable();
121  }
122 
123  public function ensureValidity()
124  {
125  if (!$this->headers->has('To') && !$this->headers->has('Cc') && !$this->headers->has('Bcc')) {
126  throw new LogicException('An email must have a "To", "Cc", or "Bcc" header.');
127  }
128 
129  if (!$this->headers->has('From') && !$this->headers->has('Sender')) {
130  throw new LogicException('An email must have a "From" or a "Sender" header.');
131  }
132 
133  parent::ensureValidity();
134  }
135 
136  public function generateMessageId(): string
137  {
138  if ($this->headers->has('Sender')) {
139  $sender = $this->headers->get('Sender')->getAddress();
140  } elseif ($this->headers->has('From')) {
141  $sender = $this->headers->get('From')->getAddresses()[0];
142  } else {
143  throw new LogicException('An email must have a "From" or a "Sender" header.');
144  }
145 
146  return bin2hex(random_bytes(16)).strstr($sender->getAddress(), '@');
147  }
148 
149  public function __serialize(): array
150  {
151  return [$this->headers, $this->body];
152  }
153 
154  public function __unserialize(array $data): void
155  {
156  [$this->headers, $this->body] = $data;
157  }
158 }
Symfony\Component\Mime\Message\__serialize
__serialize()
Definition: Message.php:149
Symfony\Component\Mime\Message\setHeaders
setHeaders(Headers $headers)
Definition: Message.php:60
Symfony\Component\Mime
Definition: Address.php:12
Symfony\Component\Mime\Message\generateMessageId
generateMessageId()
Definition: Message.php:136
Symfony\Component\Mime\Message\setBody
setBody(AbstractPart $body=null)
Definition: Message.php:45
Symfony\Component\Mime\Part\AbstractPart
Definition: AbstractPart.php:19
Symfony\Component\Mime\Message
Definition: Message.php:22
Symfony\Component\Mime\RawMessage
Definition: RawMessage.php:19
Symfony\Component\Mime\Message\__construct
__construct(Headers $headers=null, AbstractPart $body=null)
Definition: Message.php:27
Symfony\Component\Mime\Part\TextPart
Definition: TextPart.php:24
Symfony\Component\Mime\Message\toString
toString()
Definition: Message.php:104
Symfony\Component\Mime\Message\toIterable
toIterable()
Definition: Message.php:113
Symfony\Component\Mime\Message\getBody
getBody()
Definition: Message.php:52
Symfony\Component\Mime\Header\Headers
Definition: Headers.php:22
Symfony\Component\Mime\Message\ensureValidity
ensureValidity()
Definition: Message.php:123
Symfony\Component\Mime\Exception\LogicException
Definition: LogicException.php:17
Symfony\Component\Mime\Message\__clone
__clone()
Definition: Message.php:33
Symfony\Component\Mime\Message\getPreparedHeaders
getPreparedHeaders()
Definition: Message.php:72
Symfony\Component\Mime\Message\getHeaders
getHeaders()
Definition: Message.php:67
Symfony\Component\Mime\Message\__unserialize
__unserialize(array $data)
Definition: Message.php:154