c55403823940568965f29935a58e58b924912a0b
[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 * Class to handle mail specific functionality
31 *
32 * @author Tolleiv Nietsch <nietsch@aoemedia.de>
33 */
34 class MailUtility {
35
36 /**
37 * Proxy for the PHP mail() function. Adds possibility to hook in and send the mails in a different way.
38 * The hook can be used by adding function to the configuration array:
39 * $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/utility/class.t3lib_utility_mail.php']['substituteMailDelivery']
40 *
41 * @param string $to Email address to send to.
42 * @param string $subject Subject line, non-encoded. (see PHP function mail())
43 * @param string $messageBody Message content, non-encoded. (see PHP function mail())
44 * @param string $additionalHeaders Additional headers for the mail (see PHP function mail())
45 * @param string $additionalParameters Additional flags for the sending mail tool (see PHP function mail())
46 * @return boolean Indicates whether the mail has been sent or not
47 * @see PHP function mail() []
48 * @link http://www.php.net/manual/en/function.mail.php
49 */
50 static public function mail($to, $subject, $messageBody, $additionalHeaders = NULL, $additionalParameters = NULL) {
51 $success = TRUE;
52 // If the mail does not have a From: header, fall back to the default in TYPO3_CONF_VARS.
53 if (!preg_match('/^From:/im', $additionalHeaders) && $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress']) {
54 if (!is_null($additionalHeaders) && substr($additionalHeaders, -1) != LF) {
55 $additionalHeaders .= LF;
56 }
57 if ($GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName']) {
58 $additionalHeaders .= 'From: "' . $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'] . '" <' . $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'] . '>';
59 } else {
60 $additionalHeaders .= 'From: ' . $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'];
61 }
62 }
63 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/utility/class.t3lib_utility_mail.php']['substituteMailDelivery'])) {
64 $parameters = array(
65 'to' => $to,
66 'subject' => $subject,
67 'messageBody' => $messageBody,
68 'additionalHeaders' => $additionalHeaders,
69 'additionalParameters' => $additionalParameters
70 );
71 $fakeThis = FALSE;
72 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/utility/class.t3lib_utility_mail.php']['substituteMailDelivery'] as $hookSubscriber) {
73 $hookSubscriberContainsArrow = strpos($hookSubscriber, '->');
74 if ($hookSubscriberContainsArrow !== FALSE) {
75 throw new \RuntimeException($hookSubscriber . ' is an invalid hook implementation. Please consider using an implementation of TYPO3\\CMS\\Core\\Mail\\MailerAdapter.', 1322287600);
76 } else {
77 $mailerAdapter = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($hookSubscriber);
78 if ($mailerAdapter instanceof \TYPO3\CMS\Core\Mail\MailerAdapterInterface) {
79 $success = $success && $mailerAdapter->mail($to, $subject, $messageBody, $additionalHeaders, $additionalParameters, $fakeThis);
80 } else {
81 throw new \RuntimeException($hookSubscriber . ' is not an implementation of TYPO3\\CMS\\Core\\Mail\\MailerAdapter,
82 but must implement that interface to be used in the substituteMailDelivery hook.', 1294062286);
83 }
84 }
85 }
86 } else {
87 if (is_null($additionalParameters)) {
88 $success = @mail($to, $subject, $messageBody, $additionalHeaders);
89 } else {
90 $success = @mail($to, $subject, $messageBody, $additionalHeaders, $additionalParameters);
91 }
92 }
93 if (!$success) {
94 \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog('Mail to "' . $to . '" could not be sent (Subject: "' . $subject . '").', 'Core', \TYPO3\CMS\Core\Utility\GeneralUtility::SYSLOG_SEVERITY_ERROR);
95 }
96 return $success;
97 }
98
99 /**
100 * Gets a valid "from" for mail messages (email and name).
101 *
102 * Ready to be passed to $mail->setFrom()
103 *
104 * @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
105 */
106 static public function getSystemFrom() {
107 $address = self::getSystemFromAddress();
108 $name = self::getSystemFromName();
109 if (!$address) {
110 return NULL;
111 } elseif ($name) {
112 return array($address => $name);
113 } else {
114 return array($address);
115 }
116 }
117
118 /**
119 * Creates a valid "from" name for mail messages.
120 *
121 * As configured in Install Tool.
122 *
123 * @return string The name (unquoted, unformatted). NULL if none is set
124 */
125 static public function getSystemFromName() {
126 if ($GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName']) {
127 return $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'];
128 } else {
129 return NULL;
130 }
131 }
132
133 /**
134 * Creates a valid email address for the sender of mail messages.
135 *
136 * Uses a fallback chain:
137 * $TYPO3_CONF_VARS['MAIL']['defaultMailFromAddress'] ->
138 * no-reply@FirstDomainRecordFound ->
139 * no-reply@php_uname('n') ->
140 * no-reply@example.com
141 *
142 * Ready to be passed to $mail->setFrom()
143 *
144 * @return string An email address
145 */
146 static public function getSystemFromAddress() {
147 // default, first check the localconf setting
148 $address = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'];
149 if (!\TYPO3\CMS\Core\Utility\GeneralUtility::validEmail($address)) {
150 // just get us a domain record we can use as the host
151 $host = '';
152 $domainRecord = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('domainName', 'sys_domain', 'hidden = 0', '', 'pid ASC, sorting ASC');
153 if (!empty($domainRecord['domainName'])) {
154 $tempUrl = $domainRecord['domainName'];
155 if (!\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($tempUrl, 'http')) {
156 // shouldn't be the case anyways, but you never know
157 // ... there're crazy people out there
158 $tempUrl = 'http://' . $tempUrl;
159 }
160 $host = parse_url($tempUrl, PHP_URL_HOST);
161 }
162 $address = 'no-reply@' . $host;
163 if (!\TYPO3\CMS\Core\Utility\GeneralUtility::validEmail($address)) {
164 // still nothing, get host name from server
165 $address = 'no-reply@' . php_uname('n');
166 if (!\TYPO3\CMS\Core\Utility\GeneralUtility::validEmail($address)) {
167 // if everything fails use a dummy address
168 $address = 'no-reply@example.com';
169 }
170 }
171 }
172 return $address;
173 }
174
175 /**
176 * Breaks up a single line of text for emails
177 *
178 * @param string $str The string to break up
179 * @param string $newlineChar The string to implode the broken lines with (default/typically \n)
180 * @param integer $lineWidth The line width
181 * @return string Reformated text
182 */
183 static public function breakLinesForEmail($str, $newlineChar = LF, $lineWidth = 76) {
184 $lines = array();
185 $substrStart = 0;
186 while (strlen($str) > $substrStart) {
187 $substr = substr($str, $substrStart, $lineWidth);
188 // has line exceeded (reached) the maximum width?
189 if (strlen($substr) == $lineWidth) {
190 // find last space-char
191 $spacePos = strrpos(rtrim($substr), ' ');
192 // space-char found?
193 if ($spacePos !== FALSE) {
194 // take everything up to last space-char
195 $theLine = substr($substr, 0, $spacePos);
196 } else {
197 // search for space-char in remaining text
198 // makes this line longer than $lineWidth!
199 $afterParts = explode(' ', substr($str, $lineWidth + $substrStart), 2);
200 $theLine = $substr . $afterParts[0];
201 }
202 if (!strlen($theLine)) {
203 // prevent endless loop because of empty line
204 break;
205 }
206 } else {
207 $theLine = $substr;
208 }
209 $lines[] = trim($theLine);
210 $substrStart += strlen($theLine);
211 if (trim(substr($str, $substrStart, $lineWidth)) === '') {
212 // no more text
213 break;
214 }
215 }
216 return implode($newlineChar, $lines);
217 }
218
219 }
220
221
222 ?>