TYPO3 API  SVNRelease
class.t3lib_mail_mailer.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 2010-2011 Ernesto Baschny <ernst@cron-it.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     // Make sure Swift's auto-loader is registered
00029 require_once(PATH_typo3 . 'contrib/swiftmailer/swift_required.php');
00030 
00031 
00032 /**
00033  * Adapter for Swift_Mailer to be used by TYPO3 extensions.
00034  *
00035  * This will use the setting in TYPO3_CONF_VARS to choose the correct transport
00036  * for it to work out-of-the-box.
00037  *
00038  * $Id$
00039  *
00040  * @author  Ernesto Baschny <ernst@cron-it.de>
00041  * @package TYPO3
00042  * @subpackage t3lib
00043  */
00044 class t3lib_mail_Mailer extends Swift_Mailer {
00045 
00046     /**
00047      * @var Swift_Transport
00048      */
00049     protected $transport;
00050 
00051     /**
00052      * When constructing, also initializes the Swift_Transport like configured
00053      *
00054      * @param Swift_Transport optionally pass a transport to the constructor. By default the configured transport from $TYPO3_CONF_VARS is used
00055      * @throws t3lib_exception
00056      */
00057     public function __construct(Swift_Transport $transport = NULL) {
00058         if ($transport !== NULL) {
00059             $this->transport = $transport;
00060         } else {
00061             try {
00062                 $this->initializeTransport();
00063             } catch (Exception $e) {
00064                 throw new t3lib_exception($e->getMessage(), 1291068569);
00065             }
00066         }
00067         parent::__construct($this->transport);
00068     }
00069 
00070     /**
00071      * Prepares a transport using the TYPO3_CONF_VARS configuration
00072      *
00073      * Used options:
00074      * $TYPO3_CONF_VARS['MAIL']['transport'] = 'smtp' | 'sendmail' | 'mail' | 'mbox'
00075      *
00076      * $TYPO3_CONF_VARS['MAIL']['transport_smtp_server'] = 'smtp.example.org';
00077      * $TYPO3_CONF_VARS['MAIL']['transport_smtp_port'] = '25';
00078      * $TYPO3_CONF_VARS['MAIL']['transport_smtp_encrypt'] = FALSE; # requires openssl in PHP
00079      * $TYPO3_CONF_VARS['MAIL']['transport_smtp_username'] = 'username';
00080      * $TYPO3_CONF_VARS['MAIL']['transport_smtp_password'] = 'password';
00081      *
00082      * $TYPO3_CONF_VARS['MAIL']['transport_sendmail_command'] = '/usr/sbin/sendmail -bs'
00083      *
00084      * @throws t3lib_exception
00085      */
00086     private function initializeTransport() {
00087         $mailSettings = $GLOBALS['TYPO3_CONF_VARS']['MAIL'];
00088         switch ($mailSettings['transport']) {
00089 
00090             case 'smtp':
00091                     // Get settings to be used when constructing the transport object
00092                 list($host, $port) = preg_split('/:/', $mailSettings['transport_smtp_server']);
00093                 if ($host === '') {
00094                     throw new t3lib_exception(
00095                         '$TYPO3_CONF_VARS[\'MAIL\'][\'transport_smtp_server\'] needs to be set when transport is set to "smtp"',
00096                         1291068606
00097                     );
00098                 }
00099                 if ($port === '') {
00100                     $port = '25';
00101                 }
00102                 $useEncryption = ($mailSettings['transport_smtp_encrypt'] ? TRUE : FALSE);
00103 
00104                     // Create our transport
00105                 $this->transport = Swift_SmtpTransport::newInstance($host, $port, $useEncryption);
00106 
00107                     // Need authentication?
00108                 $username = $mailSettings['transport_smtp_username'];
00109                 if ($username !== '') {
00110                     $this->transport->setUsername($username);
00111                 }
00112                 $password = $mailSettings['transport_smtp_password'];
00113                 if ($password !== '') {
00114                     $this->transport->setPassword($password);
00115                 }
00116                 break;
00117 
00118             case 'sendmail':
00119                 $sendmailCommand = $mailSettings['transport_sendmail_command'];
00120                 if ($sendmailCommand === '') {
00121                     throw new t3lib_exception(
00122                         '$TYPO3_CONF_VARS[\'MAIL\'][\'transport_sendmail_command\'] needs to be set when transport is set to "sendmail"',
00123                         1291068620
00124                     );
00125                 }
00126                     // Create our transport
00127                 $this->transport = Swift_SendmailTransport::newInstance($sendmailCommand);
00128                 break;
00129 
00130             case 'mbox':
00131                 $mboxFile = $mailSettings['transport_mbox_file'];
00132                 if ($mboxFile == '') {
00133                     throw new t3lib_exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_mbox_file\'] needs to be set when transport is set to "mbox"');
00134                 }
00135                     // Create our transport
00136                 $this->transport = t3lib_div::makeInstance('t3lib_mail_mboxtransport', $mboxFile);
00137                 break;
00138 
00139             case 'mail':
00140             default:
00141                     // Create the transport, no configuration required
00142                 $this->transport = Swift_MailTransport::newInstance();
00143                 break;
00144         }
00145         return;
00146     }
00147 
00148 }
00149 
00150 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_mail_mailer.php'])) {
00151     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_mail_mailer.php']);
00152 }
00153 
00154 ?>