[!!!][TASK] Remove sys_domain and LegacyDomains
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Utility / MailUtility.php
1 <?php
2 namespace TYPO3\CMS\Core\Utility;
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
17 /**
18 * Class to handle mail specific functionality
19 */
20 class MailUtility
21 {
22 /**
23 * Gets a valid "from" for mail messages (email and name).
24 *
25 * Ready to be passed to $mail->setFrom()
26 *
27 * @return array key=Valid email address which can be used as sender, value=Valid name which can be used as a sender. NULL if no address is configured
28 */
29 public static function getSystemFrom()
30 {
31 $address = self::getSystemFromAddress();
32 $name = self::getSystemFromName();
33 if (!$address) {
34 return null;
35 }
36 if ($name) {
37 return [$address => $name];
38 }
39 return [$address];
40 }
41
42 /**
43 * Creates a valid "from" name for mail messages.
44 *
45 * As configured in Install Tool.
46 *
47 * @return string The name (unquoted, unformatted). NULL if none is set
48 */
49 public static function getSystemFromName()
50 {
51 if ($GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName']) {
52 return $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'];
53 }
54 return null;
55 }
56
57 /**
58 * Creates a valid email address for the sender of mail messages.
59 *
60 * Uses a fallback chain:
61 * $TYPO3_CONF_VARS['MAIL']['defaultMailFromAddress'] ->
62 * no-reply@FirstDomainRecordFound ->
63 * no-reply@php_uname('n') ->
64 * no-reply@example.com
65 *
66 * Ready to be passed to $mail->setFrom()
67 *
68 * @return string An email address
69 */
70 public static function getSystemFromAddress()
71 {
72 // default, first check the localconf setting
73 $address = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'];
74 if (!GeneralUtility::validEmail($address)) {
75 // still nothing, get host name from server
76 $address = 'no-reply@' . php_uname('n');
77 if (!GeneralUtility::validEmail($address)) {
78 // if everything fails use a dummy address
79 $address = 'no-reply@example.com';
80 }
81 }
82 return $address;
83 }
84
85 /**
86 * Gets a default "reply-to" for mail messages (email and name).
87 *
88 * Ready to be passed to $mail->setReplyTo()
89 *
90 * @return array List of email-addresses. Specifying a realname can be done in the form of "replyToName <replyTo@example.com>".
91 */
92 public static function getSystemReplyTo(): array
93 {
94 $mailConfiguration = $GLOBALS['TYPO3_CONF_VARS']['MAIL'];
95 $replyToAddress = $mailConfiguration['defaultMailReplyToAddress'];
96 if (empty($replyToAddress) || !GeneralUtility::validEmail($replyToAddress)) {
97 return [];
98 }
99
100 if (!empty($mailConfiguration['defaultMailReplyToName'])) {
101 $replyTo = [$replyToAddress => $mailConfiguration['defaultMailReplyToName']];
102 } else {
103 $replyTo = [$replyToAddress];
104 }
105
106 return $replyTo;
107 }
108
109 /**
110 * Breaks up a single line of text for emails
111 * Words - longer than $lineWidth - will not be split into parts
112 *
113 * @param string $str The string to break up
114 * @param string $newlineChar The string to implode the broken lines with (default/typically \n)
115 * @param int $lineWidth The line width
116 * @return string Reformated text
117 */
118 public static function breakLinesForEmail($str, $newlineChar = LF, $lineWidth = 76)
119 {
120 $lines = [];
121 $substrStart = 0;
122 while (strlen($str) > $substrStart) {
123 $substr = substr($str, $substrStart, $lineWidth);
124 // has line exceeded (reached) the maximum width?
125 if (strlen($substr) == $lineWidth) {
126 // find last space-char
127 $spacePos = strrpos(rtrim($substr), ' ');
128 // space-char found?
129 if ($spacePos !== false) {
130 // take everything up to last space-char
131 $theLine = substr($substr, 0, $spacePos);
132 $substrStart++;
133 } else {
134 // search for space-char in remaining text
135 // makes this line longer than $lineWidth!
136 $afterParts = explode(' ', substr($str, $lineWidth + $substrStart), 2);
137 $theLine = $substr . $afterParts[0];
138 }
139 if ($theLine === '') {
140 // prevent endless loop because of empty line
141 break;
142 }
143 } else {
144 $theLine = $substr;
145 }
146 $lines[] = trim($theLine);
147 $substrStart += strlen($theLine);
148 if (trim(substr($str, $substrStart, $lineWidth)) === '') {
149 // no more text
150 break;
151 }
152 }
153 return implode($newlineChar, $lines);
154 }
155
156 /**
157 * Parses mailbox headers and turns them into an array.
158 *
159 * Mailbox headers are a comma separated list of 'name <email@example.org>' combinations
160 * or plain email addresses (or a mix of these).
161 * The resulting array has key-value pairs where the key is either a number
162 * (no display name in the mailbox header) and the value is the email address,
163 * or the key is the email address and the value is the display name.
164 *
165 * @param string $rawAddresses Comma separated list of email addresses (optionally with display name)
166 * @return array Parsed list of addresses.
167 */
168 public static function parseAddresses($rawAddresses)
169 {
170 /** @var \TYPO3\CMS\Core\Mail\Rfc822AddressesParser $addressParser */
171 $addressParser = GeneralUtility::makeInstance(
172 \TYPO3\CMS\Core\Mail\Rfc822AddressesParser::class,
173 $rawAddresses
174 );
175 $addresses = $addressParser->parseAddressList();
176 $addressList = [];
177 foreach ($addresses as $address) {
178 if ($address->mailbox === '') {
179 continue;
180 }
181 if ($address->personal) {
182 // item with name found ( name <email@example.org> )
183 $addressList[$address->mailbox . '@' . $address->host] = $address->personal;
184 } else {
185 // item without name found ( email@example.org )
186 $addressList[] = $address->mailbox . '@' . $address->host;
187 }
188 }
189 return $addressList;
190 }
191 }