[TASK] Refactor t3lib_mail_Mailer and add unit tests
[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 * @author Ernesto Baschny <ernst@cron-it.de>
39 * @package TYPO3
40 * @subpackage t3lib
41 */
42 class t3lib_mail_Mailer extends Swift_Mailer {
43
44 /**
45 * @var Swift_Transport
46 */
47 protected $transport;
48
49 /**
50 * @var array
51 */
52 protected $mailSettings = array();
53
54 /**
55 * When constructing, also initializes the Swift_Transport like configured
56 *
57 * @param null|Swift_Transport $transport optionally pass a transport to the constructor. By default the configured transport from $TYPO3_CONF_VARS is used
58 * @throws t3lib_exception
59 */
60 public function __construct(Swift_Transport $transport = NULL) {
61 if ($transport !== NULL) {
62 $this->transport = $transport;
63 } else {
64 if (empty($this->mailSettings)) {
65 $this->injectMailSettings();
66 }
67 try {
68 $this->initializeTransport();
69 } catch (Exception $e) {
70 throw new t3lib_exception($e->getMessage(), 1291068569);
71 }
72 }
73 parent::__construct($this->transport);
74 }
75
76 /**
77 * Prepares a transport using the TYPO3_CONF_VARS configuration
78 *
79 * Used options:
80 * $TYPO3_CONF_VARS['MAIL']['transport'] = 'smtp' | 'sendmail' | 'mail' | 'mbox'
81 *
82 * $TYPO3_CONF_VARS['MAIL']['transport_smtp_server'] = 'smtp.example.org';
83 * $TYPO3_CONF_VARS['MAIL']['transport_smtp_port'] = '25';
84 * $TYPO3_CONF_VARS['MAIL']['transport_smtp_encrypt'] = FALSE; # requires openssl in PHP
85 * $TYPO3_CONF_VARS['MAIL']['transport_smtp_username'] = 'username';
86 * $TYPO3_CONF_VARS['MAIL']['transport_smtp_password'] = 'password';
87 *
88 * $TYPO3_CONF_VARS['MAIL']['transport_sendmail_command'] = '/usr/sbin/sendmail -bs'
89 *
90 * @throws t3lib_exception
91 * @throws RuntimeException
92 */
93 private function initializeTransport() {
94 switch ($this->mailSettings['transport']) {
95 case 'smtp':
96 // Get settings to be used when constructing the transport object
97 list($host, $port) = preg_split('/:/', $this->mailSettings['transport_smtp_server']);
98 if ($host === '') {
99 throw new t3lib_exception(
100 '$TYPO3_CONF_VARS[\'MAIL\'][\'transport_smtp_server\'] needs to be set when transport is set to "smtp"',
101 1291068606
102 );
103 }
104 if ($port === '') {
105 $port = '25';
106 }
107 $useEncryption = ($this->mailSettings['transport_smtp_encrypt'] ? $this->mailSettings['transport_smtp_encrypt'] : NULL);
108
109 // Create our transport
110 $this->transport = Swift_SmtpTransport::newInstance($host, $port, $useEncryption);
111
112 // Need authentication?
113 $username = $this->mailSettings['transport_smtp_username'];
114 if ($username !== '') {
115 $this->transport->setUsername($username);
116 }
117 $password = $this->mailSettings['transport_smtp_password'];
118 if ($password !== '') {
119 $this->transport->setPassword($password);
120 }
121 break;
122
123 case 'sendmail':
124 $sendmailCommand = $this->mailSettings['transport_sendmail_command'];
125 if (empty($sendmailCommand)) {
126 throw new t3lib_exception(
127 '$TYPO3_CONF_VARS[\'MAIL\'][\'transport_sendmail_command\'] needs to be set when transport is set to "sendmail"',
128 1291068620
129 );
130 }
131 // Create our transport
132 $this->transport = Swift_SendmailTransport::newInstance($sendmailCommand);
133 break;
134
135 case 'mbox':
136 $mboxFile = $this->mailSettings['transport_mbox_file'];
137 if ($mboxFile == '') {
138 throw new t3lib_exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_mbox_file\'] needs to be set when transport is set to "mbox"', 1294586645);
139 }
140 // Create our transport
141 $this->transport = t3lib_div::makeInstance('t3lib_mail_mboxtransport', $mboxFile);
142 break;
143
144 case 'mail':
145 // Create the transport, no configuration required
146 $this->transport = Swift_MailTransport::newInstance();
147 break;
148 default:
149 // Custom mail transport
150 $customTransport = t3lib_div::makeInstance($this->mailSettings['transport'], $this->mailSettings);
151 if ($customTransport instanceof Swift_Transport) {
152 $this->transport = $customTransport;
153 } else {
154 throw new RuntimeException(
155 $this->mailSettings['transport'] . ' is not an implementation of Swift_Transport,
156 but must implement that interface to be used as a mail transport.',
157 1323006478
158 );
159 }
160 }
161 return;
162 }
163
164 /**
165 * This method is only used in unit tests
166 *
167 * @param array $mailSettings
168 * @access private
169 */
170 public function injectMailSettings(array $mailSettings = NULL) {
171 if (is_array($mailSettings)) {
172 $this->mailSettings = $mailSettings;
173 } else {
174 $this->mailSettings = (array)$GLOBALS['TYPO3_CONF_VARS']['MAIL'];
175 }
176 }
177 }
178
179 ?>