‪TYPO3CMS  10.4
MailMessage.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
16 namespace ‪TYPO3\CMS\Core\Mail;
17 
18 use Symfony\Component\Mime\Address;
19 use Symfony\Component\Mime\Email;
21 
27 class ‪MailMessage extends Email
28 {
32  protected ‪$mailer;
33 
39  protected ‪$sent = false;
40 
41  private function ‪initializeMailer(): void
42  {
43  $this->mailer = GeneralUtility::makeInstance(Mailer::class);
44  }
45 
54  public function ‪send(): bool
55  {
56  $this->‪initializeMailer();
57  $this->sent = false;
58  $this->mailer->send($this);
59  $sentMessage = $this->mailer->getSentMessage();
60  if ($sentMessage) {
61  $this->sent = true;
62  }
63  return ‪$this->sent;
64  }
65 
71  public function ‪isSent(): bool
72  {
73  return ‪$this->sent;
74  }
75 
90  public function ‪setSubject($subject): self
91  {
92  return $this->subject($subject);
93  }
94 
101  public function ‪setDate($date): self
102  {
103  return $this->date(new \DateTime('@' . $date));
104  }
105 
112  public function ‪setReturnPath($address): self
113  {
114  return $this->returnPath($address);
115  }
116 
126  public function ‪setSender($address, $name = null): self
127  {
128  return $this->sender(...$this->‪convertNamedAddress($address, $name));
129  }
130 
144  public function ‪setFrom($addresses, $name = null): self
145  {
146  $this->‪checkArguments($addresses, $name);
147  return $this->from(...$this->‪convertNamedAddress($addresses, $name));
148  }
149 
163  public function ‪setReplyTo($addresses, $name = null): self
164  {
165  $this->‪checkArguments($addresses, $name);
166  return $this->replyTo(...$this->‪convertNamedAddress($addresses, $name));
167  }
168 
183  public function ‪setTo($addresses, $name = null): self
184  {
185  $this->‪checkArguments($addresses, $name);
186  return $this->to(...$this->‪convertNamedAddress($addresses, $name));
187  }
188 
203  public function ‪setCc($addresses, $name = null): self
204  {
205  $this->‪checkArguments($addresses, $name);
206  return $this->cc(...$this->‪convertNamedAddress($addresses, $name));
207  }
208 
223  public function ‪setBcc($addresses, $name = null): self
224  {
225  $this->‪checkArguments($addresses, $name);
226  return $this->bcc(...$this->‪convertNamedAddress($addresses, $name));
227  }
228 
235  public function ‪setReadReceiptTo(string $address): self
236  {
237  $this->getHeaders()->addMailboxHeader('Disposition-Notification-To', $address);
238  return $this;
239  }
240 
247  protected function ‪convertNamedAddress(...‪$args): array
248  {
249  if (isset(‪$args[1])) {
250  return [new Address(‪$args[0], \trim(‪$args[1], ' \'"'))];
251  }
252  if (is_string(‪$args[0]) || is_array(‪$args[0])) {
253  return $this->‪convertAddresses(‪$args[0]);
254  }
255  return $this->‪convertAddresses(‪$args);
256  }
257 
264  protected function ‪convertAddresses($addresses): array
265  {
266  if (!is_array($addresses)) {
267  return [Address::create($addresses)];
268  }
269  $newAddresses = [];
270  foreach ($addresses as $email => $name) {
271  if (is_numeric($email) || ctype_digit($email)) {
272  $newAddresses[] = Address::create($name);
273  } else {
274  $newAddresses[] = new Address($email, \trim($name, ' \'"'));
275  }
276  }
277 
278  return $newAddresses;
279  }
280 
281  //
282  // Compatibility methods, as it was possible in TYPO3 v9 / SwiftMailer.
283  //
284 
285  public function ‪addFrom(...$addresses): Email
286  {
287  return parent::addFrom(...$this->‪convertNamedAddress(...$addresses));
288  }
289 
290  public function ‪addReplyTo(...$addresses): Email
291  {
292  return parent::addReplyTo(...$this->‪convertNamedAddress(...$addresses));
293  }
294 
295  public function ‪addTo(...$addresses): Email
296  {
297  return parent::addTo(...$this->‪convertNamedAddress(...$addresses));
298  }
299 
300  public function ‪addCc(...$addresses): Email
301  {
302  return parent::addCc(...$this->‪convertNamedAddress(...$addresses));
303  }
304 
305  public function ‪addBcc(...$addresses): Email
306  {
307  return parent::addBcc(...$this->‪convertNamedAddress(...$addresses));
308  }
309 
310  protected function ‪checkArguments($addresses, string $name = null): void
311  {
312  if ($name !== null && !is_string($addresses)) {
313  throw new \InvalidArgumentException('The combination of a name and an array of addresses is invalid.', 1570543657);
314  }
315  }
316 }
‪TYPO3\CMS\Core\Mail\MailMessage\setCc
‪MailMessage setCc($addresses, $name=null)
Definition: MailMessage.php:201
‪TYPO3\CMS\Core\Mail\MailMessage\initializeMailer
‪initializeMailer()
Definition: MailMessage.php:39
‪TYPO3\CMS\Core\Mail\MailMessage\addCc
‪addCc(... $addresses)
Definition: MailMessage.php:298
‪TYPO3\CMS\Core\Mail\MailMessage\setSubject
‪MailMessage setSubject($subject)
Definition: MailMessage.php:88
‪TYPO3\CMS\Core\Mail\MailMessage\setReplyTo
‪MailMessage setReplyTo($addresses, $name=null)
Definition: MailMessage.php:161
‪TYPO3\CMS\Core\Mail\MailMessage\addFrom
‪addFrom(... $addresses)
Definition: MailMessage.php:283
‪TYPO3\CMS\Core\Mail\MailMessage
Definition: MailMessage.php:28
‪TYPO3\CMS\Core\Mail\MailMessage\$mailer
‪Mailer $mailer
Definition: MailMessage.php:31
‪TYPO3\CMS\Core\Mail\MailMessage\isSent
‪bool isSent()
Definition: MailMessage.php:69
‪TYPO3\CMS\Core\Mail\MailMessage\convertNamedAddress
‪Address[] convertNamedAddress(... $args)
Definition: MailMessage.php:245
‪TYPO3\CMS\Core\Mail\MailMessage\convertAddresses
‪Address[] convertAddresses($addresses)
Definition: MailMessage.php:262
‪TYPO3\CMS\Core\Mail\MailMessage\setSender
‪MailMessage setSender($address, $name=null)
Definition: MailMessage.php:124
‪TYPO3\CMS\Core\Mail\MailMessage\setBcc
‪MailMessage setBcc($addresses, $name=null)
Definition: MailMessage.php:221
‪TYPO3\CMS\Core\Mail\MailMessage\setFrom
‪MailMessage setFrom($addresses, $name=null)
Definition: MailMessage.php:142
‪TYPO3\CMS\Core\Mail\MailMessage\addBcc
‪addBcc(... $addresses)
Definition: MailMessage.php:303
‪TYPO3\CMS\Core\Mail\Mailer
Definition: Mailer.php:38
‪TYPO3\CMS\Core\Mail\MailMessage\$sent
‪bool $sent
Definition: MailMessage.php:37
‪$args
‪$args
Definition: validateRstFiles.php:214
‪TYPO3\CMS\Core\Mail\MailMessage\checkArguments
‪checkArguments($addresses, string $name=null)
Definition: MailMessage.php:308
‪TYPO3\CMS\Core\Mail\MailMessage\setReadReceiptTo
‪MailMessage setReadReceiptTo(string $address)
Definition: MailMessage.php:233
‪TYPO3\CMS\Core\Mail\MailMessage\setTo
‪MailMessage setTo($addresses, $name=null)
Definition: MailMessage.php:181
‪TYPO3\CMS\Core\Mail\MailMessage\setReturnPath
‪MailMessage setReturnPath($address)
Definition: MailMessage.php:110
‪TYPO3\CMS\Core\Mail\MailMessage\addReplyTo
‪addReplyTo(... $addresses)
Definition: MailMessage.php:288
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Mail\MailMessage\addTo
‪addTo(... $addresses)
Definition: MailMessage.php:293
‪TYPO3\CMS\Core\Mail
Definition: DelayedTransportInterface.php:18
‪TYPO3\CMS\Core\Mail\MailMessage\setDate
‪MailMessage setDate($date)
Definition: MailMessage.php:99
‪TYPO3\CMS\Core\Mail\MailMessage\send
‪bool send()
Definition: MailMessage.php:52