[TASK] Deprecate \TYPO3\CMS\Core\Utility\MailUtility::mail()
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Utility / MailUtility.php
1 <?php
2 namespace TYPO3\CMS\Core\Utility;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 Tolleiv Nietsch <nietsch@aoemedia.de>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 use TYPO3\CMS\Core\Utility\GeneralUtility;
31
32 /**
33 * Class to handle mail specific functionality
34 *
35 * @author Tolleiv Nietsch <nietsch@aoemedia.de>
36 */
37 class MailUtility {
38
39 /**
40 * Proxy for the PHP mail() function. Adds possibility to hook in and send the mails in a different way.
41 * The hook can be used by adding function to the configuration array:
42 * $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/utility/class.t3lib_utility_mail.php']['substituteMailDelivery']
43 *
44 * @param string $to Email address to send to.
45 * @param string $subject Subject line, non-encoded. (see PHP function mail())
46 * @param string $messageBody Message content, non-encoded. (see PHP function mail())
47 * @param string $additionalHeaders Additional headers for the mail (see PHP function mail())
48 * @param string $additionalParameters Additional flags for the sending mail tool (see PHP function mail())
49 * @return boolean Indicates whether the mail has been sent or not
50 * @see PHP function mail() []
51 * @link http://www.php.net/manual/en/function.mail.php
52 * @deprecated since 6.1, will be removed two versions later - Use \TYPO3\CMS\Core\Mail\Mailer instead
53 */
54 static public function mail($to, $subject, $messageBody, $additionalHeaders = NULL, $additionalParameters = NULL) {
55 GeneralUtility::logDeprecatedFunction();
56 $success = TRUE;
57 // If the mail does not have a From: header, fall back to the default in TYPO3_CONF_VARS.
58 if (!preg_match('/^From:/im', $additionalHeaders) && $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress']) {
59 if (!is_null($additionalHeaders) && substr($additionalHeaders, -1) != LF) {
60 $additionalHeaders .= LF;
61 }
62 if ($GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName']) {
63 $additionalHeaders .= 'From: "' . $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'] . '" <' . $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'] . '>';
64 } else {
65 $additionalHeaders .= 'From: ' . $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'];
66 }
67 }
68 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/utility/class.t3lib_utility_mail.php']['substituteMailDelivery'])) {
69 $parameters = array(
70 'to' => $to,
71 'subject' => $subject,
72 'messageBody' => $messageBody,
73 'additionalHeaders' => $additionalHeaders,
74 'additionalParameters' => $additionalParameters
75 );
76 $fakeThis = FALSE;
77 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/utility/class.t3lib_utility_mail.php']['substituteMailDelivery'] as $hookSubscriber) {
78 $hookSubscriberContainsArrow = strpos($hookSubscriber, '->');
79 if ($hookSubscriberContainsArrow !== FALSE) {
80 throw new \RuntimeException($hookSubscriber . ' is an invalid hook implementation. Please consider using an implementation of TYPO3\\CMS\\Core\\Mail\\MailerAdapter.', 1322287600);
81 } else {
82 $mailerAdapter = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($hookSubscriber);
83 if ($mailerAdapter instanceof \TYPO3\CMS\Core\Mail\MailerAdapterInterface) {
84 $success = $success && $mailerAdapter->mail($to, $subject, $messageBody, $additionalHeaders, $additionalParameters, $fakeThis);
85 } else {
86 throw new \RuntimeException($hookSubscriber . ' is not an implementation of TYPO3\\CMS\\Core\\Mail\\MailerAdapter,
87 but must implement that interface to be used in the substituteMailDelivery hook.', 1294062286);
88 }
89 }
90 }
91 } else {
92 if (is_null($additionalParameters)) {
93 $success = @mail($to, $subject, $messageBody, $additionalHeaders);
94 } else {
95 $success = @mail($to, $subject, $messageBody, $additionalHeaders, $additionalParameters);
96 }
97 }
98 if (!$success) {
99 \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog('Mail to "' . $to . '" could not be sent (Subject: "' . $subject . '").', 'Core', \TYPO3\CMS\Core\Utility\GeneralUtility::SYSLOG_SEVERITY_ERROR);
100 }
101 return $success;
102 }
103
104 /**
105 * Gets a valid "from" for mail messages (email and name).
106 *
107 * Ready to be passed to $mail->setFrom()
108 *
109 * @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
110 */
111 static public function getSystemFrom() {
112 $address = self::getSystemFromAddress();
113 $name = self::getSystemFromName();
114 if (!$address) {
115 return NULL;
116 } elseif ($name) {
117 return array($address => $name);
118 } else {
119 return array($address);
120 }
121 }
122
123 /**
124 * Creates a valid "from" name for mail messages.
125 *
126 * As configured in Install Tool.
127 *
128 * @return string The name (unquoted, unformatted). NULL if none is set
129 */
130 static public function getSystemFromName() {
131 if ($GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName']) {
132 return $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'];
133 } else {
134 return NULL;
135 }
136 }
137
138 /**
139 * Creates a valid email address for the sender of mail messages.
140 *
141 * Uses a fallback chain:
142 * $TYPO3_CONF_VARS['MAIL']['defaultMailFromAddress'] ->
143 * no-reply@FirstDomainRecordFound ->
144 * no-reply@php_uname('n') ->
145 * no-reply@example.com
146 *
147 * Ready to be passed to $mail->setFrom()
148 *
149 * @return string An email address
150 */
151 static public function getSystemFromAddress() {
152 // default, first check the localconf setting
153 $address = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'];
154 if (!\TYPO3\CMS\Core\Utility\GeneralUtility::validEmail($address)) {
155 // just get us a domain record we can use as the host
156 $host = '';
157 $domainRecord = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('domainName', 'sys_domain', 'hidden = 0', '', 'pid ASC, sorting ASC');
158 if (!empty($domainRecord['domainName'])) {
159 $tempUrl = $domainRecord['domainName'];
160 if (!\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($tempUrl, 'http')) {
161 // shouldn't be the case anyways, but you never know
162 // ... there're crazy people out there
163 $tempUrl = 'http://' . $tempUrl;
164 }
165 $host = parse_url($tempUrl, PHP_URL_HOST);
166 }
167 $address = 'no-reply@' . $host;
168 if (!\TYPO3\CMS\Core\Utility\GeneralUtility::validEmail($address)) {
169 // still nothing, get host name from server
170 $address = 'no-reply@' . php_uname('n');
171 if (!\TYPO3\CMS\Core\Utility\GeneralUtility::validEmail($address)) {
172 // if everything fails use a dummy address
173 $address = 'no-reply@example.com';
174 }
175 }
176 }
177 return $address;
178 }
179
180 /**
181 * Breaks up a single line of text for emails
182 *
183 * @param string $str The string to break up
184 * @param string $newlineChar The string to implode the broken lines with (default/typically \n)
185 * @param integer $lineWidth The line width
186 * @return string Reformated text
187 */
188 static public function breakLinesForEmail($str, $newlineChar = LF, $lineWidth = 76) {
189 $lines = array();
190 $substrStart = 0;
191 while (strlen($str) > $substrStart) {
192 $substr = substr($str, $substrStart, $lineWidth);
193 // has line exceeded (reached) the maximum width?
194 if (strlen($substr) == $lineWidth) {
195 // find last space-char
196 $spacePos = strrpos(rtrim($substr), ' ');
197 // space-char found?
198 if ($spacePos !== FALSE) {
199 // take everything up to last space-char
200 $theLine = substr($substr, 0, $spacePos);
201 } else {
202 // search for space-char in remaining text
203 // makes this line longer than $lineWidth!
204 $afterParts = explode(' ', substr($str, $lineWidth + $substrStart), 2);
205 $theLine = $substr . $afterParts[0];
206 }
207 if (!strlen($theLine)) {
208 // prevent endless loop because of empty line
209 break;
210 }
211 } else {
212 $theLine = $substr;
213 }
214 $lines[] = trim($theLine);
215 $substrStart += strlen($theLine);
216 if (trim(substr($str, $substrStart, $lineWidth)) === '') {
217 // no more text
218 break;
219 }
220 }
221 return implode($newlineChar, $lines);
222 }
223
224 /**
225 * Parses mailbox headers and turns them into an array.
226 *
227 * Mailbox headers are a comma separated list of 'name <email@example.org>' combinations
228 * or plain email addresses (or a mix of these).
229 * The resulting array has key-value pairs where the key is either a number
230 * (no display name in the mailbox header) and the value is the email address,
231 * or the key is the email address and the value is the display name.
232 *
233 * @param string $rawAddresses Comma separated list of email addresses (optionally with display name)
234 * @return array Parsed list of addresses.
235 */
236 static public function parseAddresses($rawAddresses) {
237 /** @var $addressParser \TYPO3\CMS\Core\Mail\Rfc822AddressesParser */
238 $addressParser = GeneralUtility::makeInstance(
239 'TYPO3\\CMS\\Core\\Mail\\Rfc822AddressesParser',
240 $rawAddresses
241 );
242 $addresses = $addressParser->parseAddressList();
243 $addressList = array();
244 foreach ($addresses as $address) {
245 if ($address->personal) {
246 // item with name found ( name <email@example.org> )
247 $addressList[$address->mailbox . '@' . $address->host] = $address->personal;
248 } else {
249 // item without name found ( email@example.org )
250 $addressList[] = $address->mailbox . '@' . $address->host;
251 }
252 }
253 return $addressList;
254 }
255 }
256
257 ?>