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