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