‪TYPO3CMS  ‪main
MailUtility.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
21 
26 {
39  public static function getSystemFrom(): ?array
40  {
41  $address = ‪self::getSystemFromAddress();
43  if (!$address) {
44  return null;
45  }
46  if (‪$name) {
47  return [$address => ‪$name];
48  }
49  return [$address];
50  }
51 
59  public static function ‪getSystemFromName(): ?string
60  {
61  ‪$name = ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'] ?? null;
62 
63  return (!empty(‪$name) && is_string(‪$name)) ? ‪$name : null;
64  }
65 
79  public static function ‪getSystemFromAddress(): string
80  {
81  $address = ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'] ?? null;
82 
83  if (!is_string($address) || !GeneralUtility::validEmail($address)) {
84  // still nothing, get host name from server
85  $address = 'no-reply@' . php_uname('n');
86  if (!GeneralUtility::validEmail($address)) {
87  // if everything fails use a dummy address
88  $address = 'no-reply@example.com';
89  }
90  }
91  return $address;
92  }
93 
107  public static function getSystemReplyTo(): array
108  {
109  ‪$mailConfiguration = ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL'] ?? [];
110  $replyToAddress = ‪$mailConfiguration['defaultMailReplyToAddress'] ?? null;
111  if (empty($replyToAddress) || !GeneralUtility::validEmail($replyToAddress)) {
112  return [];
113  }
114 
115  if (!empty(‪$mailConfiguration['defaultMailReplyToName'])) {
116  ‪$replyTo = [$replyToAddress => ‪$mailConfiguration['defaultMailReplyToName']];
117  } else {
118  ‪$replyTo = [$replyToAddress];
119  }
120 
121  return ‪$replyTo;
122  }
123 
133  public static function ‪breakLinesForEmail(string $str, string $newlineChar = LF, int $lineWidth = 76): string
134  {
135  $lines = [];
136  $substrStart = 0;
137  while (strlen($str) > $substrStart) {
138  $substr = substr($str, $substrStart, $lineWidth);
139  // has line exceeded (reached) the maximum width?
140  if (strlen($substr) === $lineWidth) {
141  // find last space-char
142  $spacePos = strrpos(rtrim($substr), ' ');
143  // space-char found?
144  if ($spacePos !== false) {
145  // take everything up to last space-char
146  $theLine = substr($substr, 0, $spacePos);
147  $substrStart++;
148  } else {
149  // search for space-char in remaining text
150  // makes this line longer than $lineWidth!
151  $afterParts = explode(' ', substr($str, $lineWidth + $substrStart), 2);
152  $theLine = $substr . $afterParts[0];
153  }
154  if ($theLine === '') {
155  // prevent endless loop because of empty line
156  break;
157  }
158  } else {
159  $theLine = $substr;
160  }
161  $lines[] = trim($theLine);
162  $substrStart += strlen($theLine);
163  if (trim(substr($str, $substrStart, $lineWidth)) === '') {
164  // no more text
165  break;
166  }
167  }
168  return implode($newlineChar, $lines);
169  }
170 
183  public static function ‪parseAddresses(string $rawAddresses): array
184  {
185  $addressParser = GeneralUtility::makeInstance(
186  Rfc822AddressesParser::class,
187  $rawAddresses
188  );
189  $addresses = $addressParser->parseAddressList();
190  $addressList = [];
191  foreach ($addresses as $address) {
192  if ($address->mailbox === '') {
193  continue;
194  }
195  if ($address->personal) {
196  // item with name found ( name <email@example.org> )
197  $addressList[$address->mailbox . '@' . $address->host] = $address->personal;
198  } else {
199  // item without name found ( email@example.org )
200  $addressList[] = $address->mailbox . '@' . $address->host;
201  }
202  }
203  return $addressList;
204  }
205 }
‪TYPO3\CMS\Core\Mail\Rfc822AddressesParser
Definition: Rfc822AddressesParser.php:67
‪TYPO3\CMS\Core\Utility\MailUtility\$replyTo
‪return $replyTo
Definition: MailUtility.php:121
‪TYPO3\CMS\Core\Utility\MailUtility\$name
‪static array< string|int, function getSystemFrom():?array { $address=self::getSystemFromAddress();$name=self::getSystemFromName();if(! $address) { return null;} if( $name) { return[ $address=> $name
Definition: MailUtility.php:47
‪TYPO3\CMS\Core\Utility\MailUtility\getSystemFromAddress
‪static string getSystemFromAddress()
Definition: MailUtility.php:79
‪TYPO3\CMS\Core\Utility
Definition: ArrayUtility.php:18
‪TYPO3\CMS\Core\Utility\MailUtility\breakLinesForEmail
‪static string breakLinesForEmail(string $str, string $newlineChar=LF, int $lineWidth=76)
Definition: MailUtility.php:133
‪TYPO3\CMS\Core\Utility\MailUtility
Definition: MailUtility.php:26
‪TYPO3\CMS\Core\Utility\MailUtility\$mailConfiguration
‪static array< string|int, function getSystemReplyTo():array { $mailConfiguration=$GLOBALS[ 'TYPO3_CONF_VARS'][ 'MAIL'] ??[];$replyToAddress=$mailConfiguration[ 'defaultMailReplyToAddress'] ?? null;if(empty( $replyToAddress)||!GeneralUtility::validEmail( $replyToAddress)) { return[];} if(!empty( $mailConfiguration[ 'defaultMailReplyToName'])) { $replyTo=[ $replyToAddress=> $mailConfiguration['defaultMailReplyToName']
Definition: MailUtility.php:116
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Utility\MailUtility\parseAddresses
‪static array parseAddresses(string $rawAddresses)
Definition: MailUtility.php:183
‪TYPO3\CMS\Core\Utility\MailUtility\getSystemFromName
‪static string null getSystemFromName()
Definition: MailUtility.php:59