TYPO3 API  SVNRelease
class.t3lib_utility_mail.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  * Copyright notice
00004  *
00005  * (c) 2010-2011 Tolleiv Nietsch <nietsch@aoemedia.de>
00006  * All rights reserved
00007  *
00008  * This script is part of the TYPO3 project. The TYPO3 project is
00009  * free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * The GNU General Public License can be found at
00015  * http://www.gnu.org/copyleft/gpl.html.
00016  * A copy is found in the textfile GPL.txt and important notices to the license
00017  * from the author is found in LICENSE.txt distributed with these scripts.
00018  *
00019  *
00020  * This script is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  * GNU General Public License for more details.
00024  *
00025  * This copyright notice MUST APPEAR in all copies of the script!
00026  ***************************************************************/
00027 
00028 /**
00029  * Class to handle mail specific functionality
00030  *
00031  * $Id: class.t3lib_utility_mail.php 6536 2009-11-25 14:07:18Z stucki $
00032  *
00033  *
00034  * @author   Tolleiv Nietsch <nietsch@aoemedia.de>
00035  * @package TYPO3
00036  * @subpackage t3lib
00037  */
00038 final class t3lib_utility_Mail {
00039 
00040     /**
00041      * Proxy for the PHP mail() function. Adds possibility to hook in and send the mails in a different way.
00042      * The hook can be used by adding function to the configuration array:
00043      * $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/utility/class.t3lib_utility_mail.php']['substituteMailDelivery']
00044      *
00045      * @param   string      Email address to send to.
00046      * @param   string      Subject line, non-encoded. (see PHP function mail())
00047      * @param   string      Message content, non-encoded. (see PHP function mail())
00048      * @param   string       Additional headers for the mail (see PHP function mail())
00049      * @param   string      Additional flags for the sending mail tool (see PHP function mail())
00050      * @return  boolean     Indicates whether the mail has been sent or not
00051      * @see     PHP function mail() []
00052      * @link    http://www.php.net/manual/en/function.mail.php
00053      */
00054     public static function mail($to, $subject, $messageBody, $additionalHeaders = NULL, $additionalParameters = NULL) {
00055         $success = TRUE;
00056 
00057             // If the mail does not have a From: header, fall back to the default in TYPO3_CONF_VARS.
00058         if (!preg_match('/^From:/im', $additionalHeaders) && $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress']) {
00059             if (!is_null($additionalHeaders) && substr($additionalHeaders, -1) != LF) {
00060                 $additionalHeaders .= LF;
00061             }
00062             if ($GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName']) {
00063                 $additionalHeaders .= 'From: "' . $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName']
00064                         . '" <' . $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'] . '>';
00065             } else {
00066                 $additionalHeaders .= 'From: ' . $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'];
00067             }
00068         }
00069 
00070         if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/utility/class.t3lib_utility_mail.php']['substituteMailDelivery'])) {
00071             $parameters = array(
00072                 'to' => $to,
00073                 'subject' => $subject,
00074                 'messageBody' => $messageBody,
00075                 'additionalHeaders' => $additionalHeaders,
00076                 'additionalParameters' => $additionalParameters,
00077             );
00078             $fakeThis = FALSE;
00079             foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/utility/class.t3lib_utility_mail.php']['substituteMailDelivery'] as $hookSubscriber) {
00080                 $hookSubscriberContainsArrow = strpos($hookSubscriber, '->');
00081 
00082                 if ($hookSubscriberContainsArrow !== FALSE) {
00083                         // deprecated, remove in TYPO3 4.7
00084                     t3lib_div::deprecationLog(
00085                         'The usage of user function notation for the substituteMailDelivery hook is deprecated,
00086                         use the t3lib_mail_MailerAdapter interface instead.'
00087                     );
00088                     $success = $success && t3lib_div::callUserFunction($hookSubscriber, $parameters, $fakeThis);
00089                 } else {
00090                     $mailerAdapter = t3lib_div::makeInstance($hookSubscriber);
00091                     if ($mailerAdapter instanceof t3lib_mail_MailerAdapter) {
00092                         $success = $success && $mailerAdapter->mail($to, $subject, $messageBody, $additionalHeaders, $additionalParameters, $fakeThis);
00093                     } else {
00094                         throw new RuntimeException(
00095                             $hookSubscriber . ' is not an implementation of t3lib_mail_MailerAdapter,
00096                             but must implement that interface to be used in the substituteMailDelivery hook.',
00097                             1294062286
00098                         );
00099                     }
00100                 }
00101             }
00102         } else {
00103             if (t3lib_utility_PhpOptions::isSafeModeEnabled() && !is_null($additionalParameters)) {
00104                 $additionalParameters = null;
00105             }
00106 
00107             if (is_null($additionalParameters)) {
00108                 $success = @mail($to, $subject, $messageBody, $additionalHeaders);
00109             } else {
00110                 $success = @mail($to, $subject, $messageBody, $additionalHeaders, $additionalParameters);
00111             }
00112         }
00113 
00114         if (!$success) {
00115             t3lib_div::sysLog('Mail to "' . $to . '" could not be sent (Subject: "' . $subject . '").', 'Core', 3);
00116         }
00117         return $success;
00118     }
00119 
00120     /**
00121      * Gets a valid "from" for mail messages (email and name).
00122      *
00123      * Ready to be passed to $mail->setFrom() (t3lib_mail)
00124      *
00125      * @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
00126      */
00127     public static function getSystemFrom() {
00128         $address = self::getSystemFromAddress();
00129         $name = self::getSystemFromName();
00130         if (!$address) {
00131             return NULL;
00132         } elseif ($name) {
00133             return array($address => $name);
00134         } else {
00135             return array($address);
00136         }
00137     }
00138 
00139     /**
00140      * Creates a valid "from" name for mail messages.
00141      *
00142      * As configured in Install Tool.
00143      *
00144      * @return string The name (unquoted, unformatted). NULL if none is set
00145      */
00146     public static function getSystemFromName() {
00147         if ($GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName']) {
00148             return $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'];
00149         } else {
00150             return NULL;
00151         }
00152     }
00153 
00154     /**
00155      * Creates a valid email address for the sender of mail messages.
00156      *
00157      * Uses a fallback chain:
00158      *     $TYPO3_CONF_VARS['MAIL']['defaultMailFromAddress'] ->
00159      *     no-reply@FirstDomainRecordFound ->
00160      *     no-reply@php_uname('n') ->
00161      *     no-reply@example.com
00162      *
00163      * Ready to be passed to $mail->setFrom() (t3lib_mail)
00164      *
00165      * @return  string  An email address
00166      */
00167     public static function getSystemFromAddress() {
00168             // default, first check the localconf setting
00169         $address = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'];
00170 
00171         if (!t3lib_div::validEmail($address)) {
00172                 // just get us a domain record we can use as the host
00173             $host = '';
00174             $domainRecord = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
00175                 'domainName',
00176                 'sys_domain',
00177                 'hidden = 0',
00178                 '',
00179                 'pid ASC, sorting ASC'
00180             );
00181 
00182             if (!empty($domainRecord['domainName'])) {
00183                 $tempUrl = $domainRecord['domainName'];
00184 
00185                 if (!t3lib_div::isFirstPartOfStr($tempUrl, 'http')) {
00186                         // shouldn't be the case anyways, but you never know
00187                         // ... there're crazy people out there
00188                     $tempUrl = 'http://' .$tempUrl;
00189                 }
00190                 $host = parse_url($tempUrl, PHP_URL_HOST);
00191             }
00192 
00193             $address = 'no-reply@' . $host;
00194 
00195             if (!t3lib_div::validEmail($address)) {
00196                     // still nothing, get host name from server
00197                 $address = 'no-reply@' . php_uname('n');
00198 
00199                 if (!t3lib_div::validEmail($address)) {
00200                         // if everything fails use a dummy address
00201                     $address = 'no-reply@example.com';
00202                 }
00203             }
00204         }
00205 
00206         return $address;
00207     }
00208 }
00209 
00210 ?>