‪TYPO3CMS  9.5
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  if (empty($this->getReplyTo())) {
68  if (!empty($replyTo)) {
69  $this->‪setReplyTo($replyTo);
70  }
71  }
72  $this->‪initializeMailer();
73  $this->sent = true;
74  $this->getHeaders()->addTextHeader('X-Mailer', $this->mailerHeader);
75  return $this->mailer->send($this, $this->failedRecipients);
76  }
77 
83  public function ‪isSent()
84  {
85  return ‪$this->sent;
86  }
87 
93  public function ‪getFailedRecipients()
94  {
96  }
97 
104  public function ‪setReturnPath($address)
105  {
106  $address = $this->‪idnaEncodeAddresses($address);
107  return parent::setReturnPath($address);
108  }
109 
119  public function ‪setSender($address, $name = null)
120  {
121  $address = $this->‪idnaEncodeAddresses($address);
122  return parent::setSender($address, $name);
123  }
124 
137  public function ‪setFrom($addresses, $name = null)
138  {
139  $addresses = $this->‪idnaEncodeAddresses($addresses);
140  return parent::setFrom($addresses, $name);
141  }
142 
155  public function ‪setReplyTo($addresses, $name = null)
156  {
157  $addresses = $this->‪idnaEncodeAddresses($addresses);
158  return parent::setReplyTo($addresses, $name);
159  }
160 
174  public function ‪setTo($addresses, $name = null)
175  {
176  $addresses = $this->‪idnaEncodeAddresses($addresses);
177  return parent::setTo($addresses, $name);
178  }
179 
190  public function ‪setCc($addresses, $name = null)
191  {
192  $addresses = $this->‪idnaEncodeAddresses($addresses);
193  return parent::setCc($addresses, $name);
194  }
195 
206  public function ‪setBcc($addresses, $name = null)
207  {
208  $addresses = $this->‪idnaEncodeAddresses($addresses);
209  return parent::setBcc($addresses, $name);
210  }
211 
218  public function ‪setReadReceiptTo($addresses)
219  {
220  $addresses = $this->‪idnaEncodeAddresses($addresses);
221  return parent::setReadReceiptTo($addresses);
222  }
223 
230  protected function ‪idnaEncodeAddresses($addresses)
231  {
232  if (!is_array($addresses)) {
233  return $this->‪idnaEncodeAddress($addresses);
234  }
235  $newAddresses = [];
236  foreach ($addresses as $email => $name) {
237  if (ctype_digit($email)) {
238  $newAddresses[] = $this->‪idnaEncodeAddress($name);
239  } else {
240  $newAddresses[$this->‪idnaEncodeAddress($email)] = $name;
241  }
242  }
243 
244  return $newAddresses;
245  }
246 
254  protected function ‪idnaEncodeAddress($email)
255  {
256  // Early return in case input is not a string
257  if (!is_string($email)) {
258  return $email;
259  }
260  // Split on the last "@" since adresses like "foo@bar"@example.org are valid
261  $atPosition = strrpos($email, '@');
262  if (!$atPosition || $atPosition + 1 === strlen($email)) {
263  // Return if no @ found or it is placed at the very beginning or end of the email
264  return $email;
265  }
266  $domain = substr($email, $atPosition + 1);
267  $local = substr($email, 0, $atPosition);
268  $domain = \TYPO3\CMS\Core\Utility\GeneralUtility::idnaEncode($domain);
269 
270  return $local . '@' . $domain;
271  }
272 }
‪TYPO3\CMS\Core\Utility\MailUtility\getSystemReplyTo
‪static array getSystemReplyTo()
Definition: MailUtility.php:124
‪TYPO3\CMS\Core\Mail\MailMessage\initializeMailer
‪initializeMailer()
Definition: MailMessage.php:46
‪TYPO3\CMS\Core\Mail\MailMessage\setBcc
‪TYPO3 CMS Core Mail MailMessage setBcc($addresses, $name=null)
Definition: MailMessage.php:202
‪TYPO3
‪TYPO3\CMS\Core\Mail\MailMessage
Definition: MailMessage.php:23
‪TYPO3\CMS\Core\Mail\MailMessage\$mailer
‪TYPO3 CMS Core Mail Mailer $mailer
Definition: MailMessage.php:26
‪TYPO3\CMS\Core\Utility\MailUtility\getSystemFrom
‪static array getSystemFrom()
Definition: MailUtility.php:32
‪TYPO3\CMS\Core\Mail\MailMessage\isSent
‪bool isSent()
Definition: MailMessage.php:79
‪TYPO3\CMS\Core\Mail\MailMessage\setFrom
‪TYPO3 CMS Core Mail MailMessage setFrom($addresses, $name=null)
Definition: MailMessage.php:133
‪TYPO3\CMS\Core\Mail\MailMessage\idnaEncodeAddresses
‪string array idnaEncodeAddresses($addresses)
Definition: MailMessage.php:226
‪TYPO3\CMS\Core\Mail\MailMessage\getFailedRecipients
‪array getFailedRecipients()
Definition: MailMessage.php:89
‪TYPO3\CMS\Core\Mail\MailMessage\setReadReceiptTo
‪TYPO3 CMS Core Mail MailMessage setReadReceiptTo($addresses)
Definition: MailMessage.php:214
‪TYPO3\CMS\Core\Mail\MailMessage\idnaEncodeAddress
‪mixed idnaEncodeAddress($email)
Definition: MailMessage.php:250
‪TYPO3\CMS\Core\Mail\MailMessage\send
‪int send()
Definition: MailMessage.php:56
‪TYPO3\CMS\Core\Utility\MailUtility
Definition: MailUtility.php:24
‪TYPO3\CMS\Core\Mail\MailMessage\setTo
‪TYPO3 CMS Core Mail MailMessage setTo($addresses, $name=null)
Definition: MailMessage.php:170
‪TYPO3\CMS\Core\Mail\MailMessage\$sent
‪bool $sent
Definition: MailMessage.php:36
‪TYPO3\CMS\Core\Mail\MailMessage\setReturnPath
‪TYPO3 CMS Core Mail MailMessage setReturnPath($address)
Definition: MailMessage.php:100
‪TYPO3\CMS\Core\Mail\MailMessage\setSender
‪TYPO3 CMS Core Mail MailMessage setSender($address, $name=null)
Definition: MailMessage.php:115
‪TYPO3\CMS\Core\Mail\MailMessage\setReplyTo
‪TYPO3 CMS Core Mail MailMessage setReplyTo($addresses, $name=null)
Definition: MailMessage.php:151
‪TYPO3\CMS\Core\Mail\MailMessage\$failedRecipients
‪array $failedRecipients
Definition: MailMessage.php:42
‪TYPO3\CMS\Core\Mail\MailMessage\$mailerHeader
‪string $mailerHeader
Definition: MailMessage.php:30
‪TYPO3\CMS\Core\Mail
Definition: Mailer.php:2
‪TYPO3\CMS\Core\Mail\MailMessage\setCc
‪TYPO3 CMS Core Mail MailMessage setCc($addresses, $name=null)
Definition: MailMessage.php:186