TYPO3 CMS  TYPO3_8-7
MailMessage.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Mail;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 
22 class MailMessage extends \Swift_Message
23 {
27  protected $mailer;
28 
32  protected $mailerHeader = 'TYPO3';
33 
39  protected $sent = false;
40 
46  protected $failedRecipients = [];
47 
50  private function initializeMailer()
51  {
52  $this->mailer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\Mailer::class);
53  }
54 
60  public function send()
61  {
62  // Ensure to always have a From: header set
63  if (empty($this->getFrom())) {
65  }
66  $this->initializeMailer();
67  $this->sent = true;
68  $this->getHeaders()->addTextHeader('X-Mailer', $this->mailerHeader);
69  return $this->mailer->send($this, $this->failedRecipients);
70  }
71 
77  public function isSent()
78  {
79  return $this->sent;
80  }
81 
87  public function getFailedRecipients()
88  {
90  }
91 
98  public function setReturnPath($address)
99  {
100  $address = $this->idnaEncodeAddresses($address);
101  return parent::setReturnPath($address);
102  }
103 
113  public function setSender($address, $name = null)
114  {
115  $address = $this->idnaEncodeAddresses($address);
116  return parent::setSender($address, $name);
117  }
118 
131  public function setFrom($addresses, $name = null)
132  {
133  $addresses = $this->idnaEncodeAddresses($addresses);
134  return parent::setFrom($addresses, $name);
135  }
136 
149  public function setReplyTo($addresses, $name = null)
150  {
151  $addresses = $this->idnaEncodeAddresses($addresses);
152  return parent::setReplyTo($addresses, $name);
153  }
154 
168  public function setTo($addresses, $name = null)
169  {
170  $addresses = $this->idnaEncodeAddresses($addresses);
171  return parent::setTo($addresses, $name);
172  }
173 
184  public function setCc($addresses, $name = null)
185  {
186  $addresses = $this->idnaEncodeAddresses($addresses);
187  return parent::setCc($addresses, $name);
188  }
189 
200  public function setBcc($addresses, $name = null)
201  {
202  $addresses = $this->idnaEncodeAddresses($addresses);
203  return parent::setBcc($addresses, $name);
204  }
205 
212  public function setReadReceiptTo($addresses)
213  {
214  $addresses = $this->idnaEncodeAddresses($addresses);
215  return parent::setReadReceiptTo($addresses);
216  }
217 
224  protected function idnaEncodeAddresses($addresses)
225  {
226  if (!is_array($addresses)) {
227  return $this->idnaEncodeAddress($addresses);
228  }
229  $newAddresses = [];
230  foreach ($addresses as $email => $name) {
231  if (ctype_digit($email)) {
232  $newAddresses[] = $this->idnaEncodeAddress($name);
233  } else {
234  $newAddresses[$this->idnaEncodeAddress($email)] = $name;
235  }
236  }
237 
238  return $newAddresses;
239  }
240 
248  protected function idnaEncodeAddress($email)
249  {
250  // Early return in case input is not a string
251  if (!is_string($email)) {
252  return $email;
253  }
254  // Split on the last "@" since adresses like "foo@bar"@example.org are valid
255  $atPosition = strrpos($email, '@');
256  if (!$atPosition || $atPosition + 1 === strlen($email)) {
257  // Return if no @ found or it is placed at the very beginning or end of the email
258  return $email;
259  }
260  $domain = substr($email, $atPosition + 1);
261  $local = substr($email, 0, $atPosition);
263 
264  return $local . '@' . $domain;
265  }
266 }
setReplyTo($addresses, $name=null)
setFrom($addresses, $name=null)
setTo($addresses, $name=null)
static makeInstance($className,... $constructorArguments)
setCc($addresses, $name=null)
setSender($address, $name=null)
setBcc($addresses, $name=null)