TYPO3 CMS  TYPO3_6-2
MailUtility.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Utility;
3 
18 
24 class MailUtility {
25 
41  static public function mail($to, $subject, $messageBody, $additionalHeaders = NULL, $additionalParameters = NULL) {
43  $success = TRUE;
44  // If the mail does not have a From: header, fall back to the default in TYPO3_CONF_VARS.
45  if (!preg_match('/^From:/im', $additionalHeaders) && $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress']) {
46  if (!is_null($additionalHeaders) && substr($additionalHeaders, -1) != LF) {
47  $additionalHeaders .= LF;
48  }
49  if ($GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName']) {
50  $additionalHeaders .= 'From: "' . $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'] . '" <' . $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'] . '>';
51  } else {
52  $additionalHeaders .= 'From: ' . $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'];
53  }
54  }
55  if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/utility/class.t3lib_utility_mail.php']['substituteMailDelivery'])) {
56  $parameters = array(
57  'to' => $to,
58  'subject' => $subject,
59  'messageBody' => $messageBody,
60  'additionalHeaders' => $additionalHeaders,
61  'additionalParameters' => $additionalParameters
62  );
63  $fakeThis = FALSE;
64  foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/utility/class.t3lib_utility_mail.php']['substituteMailDelivery'] as $hookSubscriber) {
65  $hookSubscriberContainsArrow = strpos($hookSubscriber, '->');
66  if ($hookSubscriberContainsArrow !== FALSE) {
67  throw new \RuntimeException($hookSubscriber . ' is an invalid hook implementation. Please consider using an implementation of TYPO3\\CMS\\Core\\Mail\\MailerAdapter.', 1322287600);
68  } else {
69  $mailerAdapter = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($hookSubscriber);
70  if ($mailerAdapter instanceof \TYPO3\CMS\Core\Mail\MailerAdapterInterface) {
71  $success = $success && $mailerAdapter->mail($to, $subject, $messageBody, $additionalHeaders, $additionalParameters, $fakeThis);
72  } else {
73  throw new \RuntimeException($hookSubscriber . ' is not an implementation of TYPO3\\CMS\\Core\\Mail\\MailerAdapter,
74  but must implement that interface to be used in the substituteMailDelivery hook.', 1294062286);
75  }
76  }
77  }
78  } else {
79  if (is_null($additionalParameters)) {
80  $success = @mail($to, $subject, $messageBody, $additionalHeaders);
81  } else {
82  $success = @mail($to, $subject, $messageBody, $additionalHeaders, $additionalParameters);
83  }
84  }
85  if (!$success) {
86  \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog('Mail to "' . $to . '" could not be sent (Subject: "' . $subject . '").', 'Core', \TYPO3\CMS\Core\Utility\GeneralUtility::SYSLOG_SEVERITY_ERROR);
87  }
88  return $success;
89  }
90 
98  static public function getSystemFrom() {
99  $address = self::getSystemFromAddress();
100  $name = self::getSystemFromName();
101  if (!$address) {
102  return NULL;
103  } elseif ($name) {
104  return array($address => $name);
105  } else {
106  return array($address);
107  }
108  }
109 
117  static public function getSystemFromName() {
118  if ($GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName']) {
119  return $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'];
120  } else {
121  return NULL;
122  }
123  }
124 
138  static public function getSystemFromAddress() {
139  // default, first check the localconf setting
140  $address = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'];
141  if (!\TYPO3\CMS\Core\Utility\GeneralUtility::validEmail($address)) {
142  // just get us a domain record we can use as the host
143  $host = '';
144  $domainRecord = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('domainName', 'sys_domain', 'hidden = 0', '', 'pid ASC, sorting ASC');
145  if (!empty($domainRecord['domainName'])) {
146  $tempUrl = $domainRecord['domainName'];
147  if (!\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($tempUrl, 'http')) {
148  // shouldn't be the case anyways, but you never know
149  // ... there're crazy people out there
150  $tempUrl = 'http://' . $tempUrl;
151  }
152  $host = parse_url($tempUrl, PHP_URL_HOST);
153  }
154  $address = 'no-reply@' . $host;
155  if (!\TYPO3\CMS\Core\Utility\GeneralUtility::validEmail($address)) {
156  // still nothing, get host name from server
157  $address = 'no-reply@' . php_uname('n');
158  if (!\TYPO3\CMS\Core\Utility\GeneralUtility::validEmail($address)) {
159  // if everything fails use a dummy address
160  $address = 'no-reply@example.com';
161  }
162  }
163  }
164  return $address;
165  }
166 
176  static public function breakLinesForEmail($str, $newlineChar = LF, $lineWidth = 76) {
177  $lines = array();
178  $substrStart = 0;
179  while (strlen($str) > $substrStart) {
180  $substr = substr($str, $substrStart, $lineWidth);
181  // has line exceeded (reached) the maximum width?
182  if (strlen($substr) == $lineWidth) {
183  // find last space-char
184  $spacePos = strrpos(rtrim($substr), ' ');
185  // space-char found?
186  if ($spacePos !== FALSE) {
187  // take everything up to last space-char
188  $theLine = substr($substr, 0, $spacePos);
189  $substrStart++;
190  } else {
191  // search for space-char in remaining text
192  // makes this line longer than $lineWidth!
193  $afterParts = explode(' ', substr($str, $lineWidth + $substrStart), 2);
194  $theLine = $substr . $afterParts[0];
195  }
196  if (!strlen($theLine)) {
197  // prevent endless loop because of empty line
198  break;
199  }
200  } else {
201  $theLine = $substr;
202  }
203  $lines[] = trim($theLine);
204  $substrStart += strlen($theLine);
205  if (trim(substr($str, $substrStart, $lineWidth)) === '') {
206  // no more text
207  break;
208  }
209  }
210  return implode($newlineChar, $lines);
211  }
212 
225  static public function parseAddresses($rawAddresses) {
227  $addressParser = GeneralUtility::makeInstance(
228  'TYPO3\\CMS\\Core\\Mail\\Rfc822AddressesParser',
229  $rawAddresses
230  );
231  $addresses = $addressParser->parseAddressList();
232  $addressList = array();
233  foreach ($addresses as $address) {
234  if ($address->mailbox === '') {
235  continue;
236  }
237  if ($address->personal) {
238  // item with name found ( name <email@example.org> )
239  $addressList[$address->mailbox . '@' . $address->host] = $address->personal;
240  } else {
241  // item without name found ( email@example.org )
242  $addressList[] = $address->mailbox . '@' . $address->host;
243  }
244  }
245  return $addressList;
246  }
247 }
static mail($to, $subject, $messageBody, $additionalHeaders=NULL, $additionalParameters=NULL)
Definition: MailUtility.php:41
$parameters
Definition: FileDumpEID.php:15
static isFirstPartOfStr($str, $partStr)
static breakLinesForEmail($str, $newlineChar=LF, $lineWidth=76)
$host
Definition: server.php:35
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]