‪TYPO3CMS  9.5
MailUtility.php
Go to the documentation of this file.
1 <?php
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 
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  // just get us a domain record we can use as the host
79  $host = '';
80  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
81  ->getQueryBuilderForTable('sys_domain');
82 
83  $queryBuilder->getRestrictions()
84  ->removeAll()
85  ->add(GeneralUtility::makeInstance(HiddenRestriction::class));
86 
87  $domainRecord = $queryBuilder
88  ->select('domainName')
89  ->from('sys_domain')
90  ->orderBy('pid', 'ASC')
91  ->orderBy('sorting', 'ASC')
92  ->execute()
93  ->fetch();
94 
95  if (!empty($domainRecord['domainName'])) {
96  $tempUrl = $domainRecord['domainName'];
97  if (!GeneralUtility::isFirstPartOfStr($tempUrl, 'http')) {
98  // shouldn't be the case anyways, but you never know
99  // ... there're crazy people out there
100  $tempUrl = 'http://' . $tempUrl;
101  }
102  $host = parse_url($tempUrl, PHP_URL_HOST);
103  }
104  $address = 'no-reply@' . $host;
105  if (!GeneralUtility::validEmail($address)) {
106  // still nothing, get host name from server
107  $address = 'no-reply@' . php_uname('n');
108  if (!GeneralUtility::validEmail($address)) {
109  // if everything fails use a dummy address
110  $address = 'no-reply@example.com';
111  }
112  }
113  }
114  return $address;
115  }
116 
124  public static function ‪getSystemReplyTo(): array
125  {
126  $mailConfiguration = ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL'];
127  $replyToAddress = $mailConfiguration['defaultMailReplyToAddress'];
128  if (empty($replyToAddress) || !GeneralUtility::validEmail($replyToAddress)) {
129  return [];
130  }
131 
132  if (!empty($mailConfiguration['defaultMailReplyToName'])) {
133  $replyTo = [$replyToAddress => $mailConfiguration['defaultMailReplyToName']];
134  } else {
135  $replyTo = [$replyToAddress];
136  }
137 
138  return $replyTo;
139  }
140 
150  public static function ‪breakLinesForEmail($str, $newlineChar = LF, $lineWidth = 76)
151  {
152  $lines = [];
153  $substrStart = 0;
154  while (strlen($str) > $substrStart) {
155  $substr = substr($str, $substrStart, $lineWidth);
156  // has line exceeded (reached) the maximum width?
157  if (strlen($substr) == $lineWidth) {
158  // find last space-char
159  $spacePos = strrpos(rtrim($substr), ' ');
160  // space-char found?
161  if ($spacePos !== false) {
162  // take everything up to last space-char
163  $theLine = substr($substr, 0, $spacePos);
164  $substrStart++;
165  } else {
166  // search for space-char in remaining text
167  // makes this line longer than $lineWidth!
168  $afterParts = explode(' ', substr($str, $lineWidth + $substrStart), 2);
169  $theLine = $substr . $afterParts[0];
170  }
171  if ($theLine === '') {
172  // prevent endless loop because of empty line
173  break;
174  }
175  } else {
176  $theLine = $substr;
177  }
178  $lines[] = trim($theLine);
179  $substrStart += strlen($theLine);
180  if (trim(substr($str, $substrStart, $lineWidth)) === '') {
181  // no more text
182  break;
183  }
184  }
185  return implode($newlineChar, $lines);
186  }
187 
200  public static function ‪parseAddresses($rawAddresses)
201  {
203  $addressParser = GeneralUtility::makeInstance(
204  \‪TYPO3\CMS\Core\Mail\Rfc822AddressesParser::class,
205  $rawAddresses
206  );
207  $addresses = $addressParser->parseAddressList();
208  $addressList = [];
209  foreach ($addresses as $address) {
210  if ($address->mailbox === '') {
211  continue;
212  }
213  if ($address->personal) {
214  // item with name found ( name <email@example.org> )
215  $addressList[$address->mailbox . '@' . $address->host] = $address->personal;
216  } else {
217  // item without name found ( email@example.org )
218  $addressList[] = $address->mailbox . '@' . $address->host;
219  }
220  }
221  return $addressList;
222  }
223 }
‪TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction
Definition: HiddenRestriction.php:25
‪TYPO3\CMS\Core\Utility\MailUtility\getSystemReplyTo
‪static array getSystemReplyTo()
Definition: MailUtility.php:124
‪TYPO3
‪TYPO3\CMS\Core\Utility\MailUtility\getSystemFromAddress
‪static string getSystemFromAddress()
Definition: MailUtility.php:73
‪TYPO3\CMS\Core\Utility
Definition: ArrayUtility.php:2
‪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:200
‪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:150
‪$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
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44