‪TYPO3CMS  10.4
MailUtility.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 
17 
19 
24 {
32  public static function ‪getSystemFrom()
33  {
34  $address = ‪self::getSystemFromAddress();
36  if (!$address) {
37  return null;
38  }
39  if ($name) {
40  return [$address => $name];
41  }
42  return [$address];
43  }
44 
52  public static function ‪getSystemFromName()
53  {
54  if (‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName']) {
55  return ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'];
56  }
57  return null;
58  }
59 
73  public static function ‪getSystemFromAddress()
74  {
75  // default, first check the localconf setting
76  $address = ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'];
77  if (!GeneralUtility::validEmail($address)) {
78  // still nothing, get host name from server
79  $address = 'no-reply@' . php_uname('n');
80  if (!GeneralUtility::validEmail($address)) {
81  // if everything fails use a dummy address
82  $address = 'no-reply@example.com';
83  }
84  }
85  return $address;
86  }
87 
95  public static function ‪getSystemReplyTo(): array
96  {
97  $mailConfiguration = ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL'];
98  $replyToAddress = $mailConfiguration['defaultMailReplyToAddress'];
99  if (empty($replyToAddress) || !GeneralUtility::validEmail($replyToAddress)) {
100  return [];
101  }
102 
103  if (!empty($mailConfiguration['defaultMailReplyToName'])) {
104  $replyTo = [$replyToAddress => $mailConfiguration['defaultMailReplyToName']];
105  } else {
106  $replyTo = [$replyToAddress];
107  }
108 
109  return $replyTo;
110  }
111 
121  public static function ‪breakLinesForEmail($str, $newlineChar = LF, $lineWidth = 76)
122  {
123  $lines = [];
124  $substrStart = 0;
125  while (strlen($str) > $substrStart) {
126  $substr = substr($str, $substrStart, $lineWidth);
127  // has line exceeded (reached) the maximum width?
128  if (strlen($substr) == $lineWidth) {
129  // find last space-char
130  $spacePos = strrpos(rtrim($substr), ' ');
131  // space-char found?
132  if ($spacePos !== false) {
133  // take everything up to last space-char
134  $theLine = substr($substr, 0, $spacePos);
135  $substrStart++;
136  } else {
137  // search for space-char in remaining text
138  // makes this line longer than $lineWidth!
139  $afterParts = explode(' ', substr($str, $lineWidth + $substrStart), 2);
140  $theLine = $substr . $afterParts[0];
141  }
142  if ($theLine === '') {
143  // prevent endless loop because of empty line
144  break;
145  }
146  } else {
147  $theLine = $substr;
148  }
149  $lines[] = trim($theLine);
150  $substrStart += strlen($theLine);
151  if (trim(substr($str, $substrStart, $lineWidth)) === '') {
152  // no more text
153  break;
154  }
155  }
156  return implode($newlineChar, $lines);
157  }
158 
171  public static function ‪parseAddresses($rawAddresses)
172  {
174  $addressParser = GeneralUtility::makeInstance(
175  Rfc822AddressesParser::class,
176  $rawAddresses
177  );
178  $addresses = $addressParser->parseAddressList();
179  $addressList = [];
180  foreach ($addresses as $address) {
181  if ($address->mailbox === '') {
182  continue;
183  }
184  if ($address->personal) {
185  // item with name found ( name <email@example.org> )
186  $addressList[$address->mailbox . '@' . $address->host] = $address->personal;
187  } else {
188  // item without name found ( email@example.org )
189  $addressList[] = $address->mailbox . '@' . $address->host;
190  }
191  }
192  return $addressList;
193  }
194 }
‪TYPO3\CMS\Core\Utility\MailUtility\getSystemReplyTo
‪static array getSystemReplyTo()
Definition: MailUtility.php:95
‪TYPO3\CMS\Core\Mail\Rfc822AddressesParser
Definition: Rfc822AddressesParser.php:65
‪TYPO3\CMS\Core\Utility\MailUtility\getSystemFromAddress
‪static string getSystemFromAddress()
Definition: MailUtility.php:73
‪TYPO3\CMS\Core\Utility
Definition: ArrayUtility.php:16
‪TYPO3\CMS\Core\Utility\MailUtility\getSystemFrom
‪static array getSystemFrom()
Definition: MailUtility.php:32
‪TYPO3\CMS\Core\Utility\MailUtility\parseAddresses
‪static array parseAddresses($rawAddresses)
Definition: MailUtility.php:171
‪TYPO3\CMS\Core\Utility\MailUtility
Definition: MailUtility.php:24
‪TYPO3\CMS\Core\Utility\MailUtility\breakLinesForEmail
‪static string breakLinesForEmail($str, $newlineChar=LF, $lineWidth=76)
Definition: MailUtility.php:121
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Utility\MailUtility\getSystemFromName
‪static string getSystemFromName()
Definition: MailUtility.php:52