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