‪TYPO3CMS  11.5
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 {
37  public static function getSystemFrom()
38  {
39  $address = ‪self::getSystemFromAddress();
41  if (!$address) {
42  return null;
43  }
44  if (‪$name) {
45  return [$address => ‪$name];
46  }
47  return [$address];
48  }
49 
57  public static function ‪getSystemFromName()
58  {
59  if (‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName']) {
60  return ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'];
61  }
62  return null;
63  }
64 
78  public static function ‪getSystemFromAddress()
79  {
80  // default, first check the localconf setting
81  $address = ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'];
82  if (!GeneralUtility::validEmail($address)) {
83  // still nothing, get host name from server
84  $address = 'no-reply@' . php_uname('n');
85  if (!GeneralUtility::validEmail($address)) {
86  // if everything fails use a dummy address
87  $address = 'no-reply@example.com';
88  }
89  }
90  return $address;
91  }
92 
106  public static function getSystemReplyTo(): array
107  {
108  ‪$mailConfiguration = ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL'];
109  $replyToAddress = ‪$mailConfiguration['defaultMailReplyToAddress'];
110  if (empty($replyToAddress) || !GeneralUtility::validEmail($replyToAddress)) {
111  return [];
112  }
113 
114  if (!empty(‪$mailConfiguration['defaultMailReplyToName'])) {
115  ‪$replyTo = [$replyToAddress => ‪$mailConfiguration['defaultMailReplyToName']];
116  } else {
117  ‪$replyTo = [$replyToAddress];
118  }
119 
120  return ‪$replyTo;
121  }
122 
132  public static function ‪breakLinesForEmail($str, $newlineChar = LF, $lineWidth = 76)
133  {
134  $lines = [];
135  $substrStart = 0;
136  while (strlen($str) > $substrStart) {
137  $substr = substr($str, $substrStart, $lineWidth);
138  // has line exceeded (reached) the maximum width?
139  if (strlen($substr) == $lineWidth) {
140  // find last space-char
141  $spacePos = strrpos(rtrim($substr), ' ');
142  // space-char found?
143  if ($spacePos !== false) {
144  // take everything up to last space-char
145  $theLine = substr($substr, 0, $spacePos);
146  $substrStart++;
147  } else {
148  // search for space-char in remaining text
149  // makes this line longer than $lineWidth!
150  $afterParts = explode(' ', substr($str, $lineWidth + $substrStart), 2);
151  $theLine = $substr . $afterParts[0];
152  }
153  if ($theLine === '') {
154  // prevent endless loop because of empty line
155  break;
156  }
157  } else {
158  $theLine = $substr;
159  }
160  $lines[] = trim($theLine);
161  $substrStart += strlen($theLine);
162  if (trim(substr($str, $substrStart, $lineWidth)) === '') {
163  // no more text
164  break;
165  }
166  }
167  return implode($newlineChar, $lines);
168  }
169 
182  public static function ‪parseAddresses($rawAddresses)
183  {
184  $addressParser = GeneralUtility::makeInstance(
185  Rfc822AddressesParser::class,
186  $rawAddresses
187  );
188  $addresses = $addressParser->parseAddressList();
189  $addressList = [];
190  foreach ($addresses as $address) {
191  if ($address->mailbox === '') {
192  continue;
193  }
194  if ($address->personal) {
195  // item with name found ( name <email@example.org> )
196  $addressList[$address->mailbox . '@' . $address->host] = $address->personal;
197  } else {
198  // item without name found ( email@example.org )
199  $addressList[] = $address->mailbox . '@' . $address->host;
200  }
201  }
202  return $addressList;
203  }
204 }
‪TYPO3\CMS\Core\Mail\Rfc822AddressesParser
Definition: Rfc822AddressesParser.php:65
‪TYPO3\CMS\Core\Utility\MailUtility\$replyTo
‪return $replyTo
Definition: MailUtility.php:120
‪TYPO3\CMS\Core\Utility\MailUtility\getSystemFromAddress
‪static string getSystemFromAddress()
Definition: MailUtility.php:78
‪TYPO3\CMS\Core\Utility
Definition: ArrayUtility.php:16
‪TYPO3\CMS\Core\Utility\MailUtility\parseAddresses
‪static array parseAddresses($rawAddresses)
Definition: MailUtility.php:182
‪TYPO3\CMS\Core\Utility\MailUtility\$name
‪static array< string|int, function getSystemFrom() { $address=self::getSystemFromAddress();$name=self::getSystemFromName();if(! $address) { return null;} if( $name) { return[ $address=> $name
Definition: MailUtility.php:45
‪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:132
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Utility\MailUtility\getSystemFromName
‪static string null getSystemFromName()
Definition: MailUtility.php:57
‪TYPO3\CMS\Core\Utility\MailUtility\$mailConfiguration
‪static array< string|int, function getSystemReplyTo():array { $mailConfiguration=$GLOBALS[ 'TYPO3_CONF_VARS'][ 'MAIL'];$replyToAddress=$mailConfiguration[ 'defaultMailReplyToAddress'];if(empty( $replyToAddress)||!GeneralUtility::validEmail( $replyToAddress)) { return[];} if(!empty( $mailConfiguration[ 'defaultMailReplyToName'])) { $replyTo=[ $replyToAddress=> $mailConfiguration['defaultMailReplyToName']
Definition: MailUtility.php:115