TYPO3  7.6
MailUtility.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Utility;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
21 {
29  public static function getSystemFrom()
30  {
31  $address = self::getSystemFromAddress();
32  $name = self::getSystemFromName();
33  if (!$address) {
34  return null;
35  } elseif ($name) {
36  return array($address => $name);
37  } else {
38  return array($address);
39  }
40  }
41 
49  public static function getSystemFromName()
50  {
51  if ($GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName']) {
52  return $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'];
53  } else {
54  return null;
55  }
56  }
57 
71  public static function getSystemFromAddress()
72  {
73  // default, first check the localconf setting
74  $address = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'];
75  if (!GeneralUtility::validEmail($address)) {
76  // just get us a domain record we can use as the host
77  $host = '';
78  $domainRecord = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('domainName', 'sys_domain', 'hidden = 0', '', 'pid ASC, sorting ASC');
79  if (!empty($domainRecord['domainName'])) {
80  $tempUrl = $domainRecord['domainName'];
81  if (!GeneralUtility::isFirstPartOfStr($tempUrl, 'http')) {
82  // shouldn't be the case anyways, but you never know
83  // ... there're crazy people out there
84  $tempUrl = 'http://' . $tempUrl;
85  }
86  $host = parse_url($tempUrl, PHP_URL_HOST);
87  }
88  $address = 'no-reply@' . $host;
89  if (!GeneralUtility::validEmail($address)) {
90  // still nothing, get host name from server
91  $address = 'no-reply@' . php_uname('n');
92  if (!GeneralUtility::validEmail($address)) {
93  // if everything fails use a dummy address
94  $address = 'no-reply@example.com';
95  }
96  }
97  }
98  return $address;
99  }
100 
110  public static function breakLinesForEmail($str, $newlineChar = LF, $lineWidth = 76)
111  {
112  $lines = array();
113  $substrStart = 0;
114  while (strlen($str) > $substrStart) {
115  $substr = substr($str, $substrStart, $lineWidth);
116  // has line exceeded (reached) the maximum width?
117  if (strlen($substr) == $lineWidth) {
118  // find last space-char
119  $spacePos = strrpos(rtrim($substr), ' ');
120  // space-char found?
121  if ($spacePos !== false) {
122  // take everything up to last space-char
123  $theLine = substr($substr, 0, $spacePos);
124  $substrStart++;
125  } else {
126  // search for space-char in remaining text
127  // makes this line longer than $lineWidth!
128  $afterParts = explode(' ', substr($str, $lineWidth + $substrStart), 2);
129  $theLine = $substr . $afterParts[0];
130  }
131  if ($theLine === '') {
132  // prevent endless loop because of empty line
133  break;
134  }
135  } else {
136  $theLine = $substr;
137  }
138  $lines[] = trim($theLine);
139  $substrStart += strlen($theLine);
140  if (trim(substr($str, $substrStart, $lineWidth)) === '') {
141  // no more text
142  break;
143  }
144  }
145  return implode($newlineChar, $lines);
146  }
147 
160  public static function parseAddresses($rawAddresses)
161  {
163  $addressParser = GeneralUtility::makeInstance(
164  \TYPO3\CMS\Core\Mail\Rfc822AddressesParser::class,
165  $rawAddresses
166  );
167  $addresses = $addressParser->parseAddressList();
168  $addressList = array();
169  foreach ($addresses as $address) {
170  if ($address->mailbox === '') {
171  continue;
172  }
173  if ($address->personal) {
174  // item with name found ( name <email@example.org> )
175  $addressList[$address->mailbox . '@' . $address->host] = $address->personal;
176  } else {
177  // item without name found ( email@example.org )
178  $addressList[] = $address->mailbox . '@' . $address->host;
179  }
180  }
181  return $addressList;
182  }
183 }