[BUGFIX] Throw correct Exceptions everywhere
[Packages/TYPO3.CMS.git] / t3lib / mail / class.t3lib_mail_mailer.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010-2011 Ernesto Baschny <ernst@cron-it.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 // Make sure Swift's auto-loader is registered
29 require_once(PATH_typo3 . 'contrib/swiftmailer/swift_required.php');
30
31
32 /**
33 * Adapter for Swift_Mailer to be used by TYPO3 extensions.
34 *
35 * This will use the setting in TYPO3_CONF_VARS to choose the correct transport
36 * for it to work out-of-the-box.
37 *
38 * $Id$
39 *
40 * @author Ernesto Baschny <ernst@cron-it.de>
41 * @package TYPO3
42 * @subpackage t3lib
43 */
44 class t3lib_mail_Mailer extends Swift_Mailer {
45
46 /**
47 * @var Swift_Transport
48 */
49 protected $transport;
50
51 /**
52 * When constructing, also initializes the Swift_Transport like configured
53 *
54 * @param Swift_Transport optionally pass a transport to the constructor. By default the configured transport from $TYPO3_CONF_VARS is used
55 * @throws t3lib_exception
56 */
57 public function __construct(Swift_Transport $transport = NULL) {
58 if ($transport !== NULL) {
59 $this->transport = $transport;
60 } else {
61 try {
62 $this->initializeTransport();
63 } catch (Exception $e) {
64 throw new t3lib_exception($e->getMessage(), 1291068569);
65 }
66 }
67 parent::__construct($this->transport);
68 }
69
70 /**
71 * Prepares a transport using the TYPO3_CONF_VARS configuration
72 *
73 * Used options:
74 * $TYPO3_CONF_VARS['MAIL']['transport'] = 'smtp' | 'sendmail' | 'mail' | 'mbox'
75 *
76 * $TYPO3_CONF_VARS['MAIL']['transport_smtp_server'] = 'smtp.example.org';
77 * $TYPO3_CONF_VARS['MAIL']['transport_smtp_port'] = '25';
78 * $TYPO3_CONF_VARS['MAIL']['transport_smtp_encrypt'] = FALSE; # requires openssl in PHP
79 * $TYPO3_CONF_VARS['MAIL']['transport_smtp_username'] = 'username';
80 * $TYPO3_CONF_VARS['MAIL']['transport_smtp_password'] = 'password';
81 *
82 * $TYPO3_CONF_VARS['MAIL']['transport_sendmail_command'] = '/usr/sbin/sendmail -bs'
83 *
84 * @throws t3lib_exception
85 */
86 private function initializeTransport() {
87 $mailSettings = $GLOBALS['TYPO3_CONF_VARS']['MAIL'];
88 switch ($mailSettings['transport']) {
89
90 case 'smtp':
91 // Get settings to be used when constructing the transport object
92 list($host, $port) = preg_split('/:/', $mailSettings['transport_smtp_server']);
93 if ($host === '') {
94 throw new t3lib_exception(
95 '$TYPO3_CONF_VARS[\'MAIL\'][\'transport_smtp_server\'] needs to be set when transport is set to "smtp"',
96 1291068606
97 );
98 }
99 if ($port === '') {
100 $port = '25';
101 }
102 $useEncryption = ($mailSettings['transport_smtp_encrypt'] ? TRUE : FALSE);
103
104 // Create our transport
105 $this->transport = Swift_SmtpTransport::newInstance($host, $port, $useEncryption);
106
107 // Need authentication?
108 $username = $mailSettings['transport_smtp_username'];
109 if ($username !== '') {
110 $this->transport->setUsername($username);
111 }
112 $password = $mailSettings['transport_smtp_password'];
113 if ($password !== '') {
114 $this->transport->setPassword($password);
115 }
116 break;
117
118 case 'sendmail':
119 $sendmailCommand = $mailSettings['transport_sendmail_command'];
120 if ($sendmailCommand === '') {
121 throw new t3lib_exception(
122 '$TYPO3_CONF_VARS[\'MAIL\'][\'transport_sendmail_command\'] needs to be set when transport is set to "sendmail"',
123 1291068620
124 );
125 }
126 // Create our transport
127 $this->transport = Swift_SendmailTransport::newInstance($sendmailCommand);
128 break;
129
130 case 'mbox':
131 $mboxFile = $mailSettings['transport_mbox_file'];
132 if ($mboxFile == '') {
133 throw new t3lib_exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_mbox_file\'] needs to be set when transport is set to "mbox"', 1294586645);
134 }
135 // Create our transport
136 $this->transport = t3lib_div::makeInstance('t3lib_mail_mboxtransport', $mboxFile);
137 break;
138
139 case 'mail':
140 default:
141 // Create the transport, no configuration required
142 $this->transport = Swift_MailTransport::newInstance();
143 break;
144 }
145 return;
146 }
147
148 }
149
150 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_mail_mailer.php'])) {
151 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_mail_mailer.php']);
152 }
153
154 ?>