TYPO3  7.6
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 
21 {
25  protected $mailer;
26 
30  protected $mailerHeader = 'TYPO3';
31 
37  protected $sent = false;
38 
44  protected $failedRecipients = array();
45 
49  private function initializeMailer()
50  {
51  $this->mailer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\Mailer::class);
52  }
53 
59  public function send()
60  {
61  $this->initializeMailer();
62  $this->sent = true;
63  $this->getHeaders()->addTextHeader('X-Mailer', $this->mailerHeader);
64  return $this->mailer->send($this, $this->failedRecipients);
65  }
66 
72  public function isSent()
73  {
74  return $this->sent;
75  }
76 
82  public function getFailedRecipients()
83  {
85  }
86 
93  public function setReturnPath($address)
94  {
95  $address = $this->idnaEncodeAddresses($address);
96  return parent::setReturnPath($address);
97  }
98 
108  public function setSender($address, $name = null)
109  {
110  $address = $this->idnaEncodeAddresses($address);
111  return parent::setSender($address, $name);
112  }
113 
126  public function setFrom($addresses, $name = null)
127  {
128  $addresses = $this->idnaEncodeAddresses($addresses);
129  return parent::setFrom($addresses, $name);
130  }
131 
144  public function setReplyTo($addresses, $name = null)
145  {
146  $addresses = $this->idnaEncodeAddresses($addresses);
147  return parent::setReplyTo($addresses, $name);
148  }
149 
163  public function setTo($addresses, $name = null)
164  {
165  $addresses = $this->idnaEncodeAddresses($addresses);
166  return parent::setTo($addresses, $name);
167  }
168 
179  public function setCc($addresses, $name = null)
180  {
181  $addresses = $this->idnaEncodeAddresses($addresses);
182  return parent::setCc($addresses, $name);
183  }
184 
195  public function setBcc($addresses, $name = null)
196  {
197  $addresses = $this->idnaEncodeAddresses($addresses);
198  return parent::setBcc($addresses, $name);
199  }
200 
207  public function setReadReceiptTo($addresses)
208  {
209  $addresses = $this->idnaEncodeAddresses($addresses);
210  return parent::setReadReceiptTo($addresses);
211  }
212 
219  protected function idnaEncodeAddresses($addresses)
220  {
221  if (!is_array($addresses)) {
222  return $this->idnaEncodeAddress($addresses);
223  }
224  $newAddresses = [];
225  foreach ($addresses as $email => $name) {
226  if (ctype_digit($email)) {
227  $newAddresses[] = $this->idnaEncodeAddress($name);
228  } else {
229  $newAddresses[$this->idnaEncodeAddress($email)] = $name;
230  }
231  }
232 
233  return $newAddresses;
234  }
235 
243  protected function idnaEncodeAddress($email)
244  {
245  // Early return in case input is not a string
246  if (!is_string($email)) {
247  return $email;
248  }
249  // Split on the last "@" since adresses like "foo@bar"@example.org are valid
250  $atPosition = strrpos($email, '@');
251  if (!$atPosition || $atPosition + 1 === strlen($email)) {
252  // Return if no @ found or it is placed at the very beginning or end of the email
253  return $email;
254  }
255  $domain = substr($email, $atPosition + 1);
256  $local = substr($email, 0, $atPosition);
257  if (!mb_check_encoding($domain, 'ASCII')) {
258  $domain = \TYPO3\CMS\Core\Utility\GeneralUtility::idnaEncode($domain);
259  }
260 
261  return $local . '@' . $domain;
262  }
263 }