d591ec690200a88ef91ea3e95447721d4826e56f
[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 * When constructing, also initializes the Swift_Transport like configured
51 *
52 * @param Swift_Transport optionally pass a transport to the constructor. By default the configured transport from $TYPO3_CONF_VARS is used
53 * @throws t3lib_exception
54 */
55 public function __construct(Swift_Transport $transport = NULL) {
56 if ($transport !== NULL) {
57 $this->transport = $transport;
58 } else {
59 try {
60 $this->initializeTransport();
61 } catch (Exception $e) {
62 throw new t3lib_exception($e->getMessage(), 1291068569);
63 }
64 }
65 parent::__construct($this->transport);
66 }
67
68 /**
69 * Prepares a transport using the TYPO3_CONF_VARS configuration
70 *
71 * Used options:
72 * $TYPO3_CONF_VARS['MAIL']['transport'] = 'smtp' | 'sendmail' | 'mail' | 'mbox'
73 *
74 * $TYPO3_CONF_VARS['MAIL']['transport_smtp_server'] = 'smtp.example.org';
75 * $TYPO3_CONF_VARS['MAIL']['transport_smtp_port'] = '25';
76 * $TYPO3_CONF_VARS['MAIL']['transport_smtp_encrypt'] = FALSE; # requires openssl in PHP
77 * $TYPO3_CONF_VARS['MAIL']['transport_smtp_username'] = 'username';
78 * $TYPO3_CONF_VARS['MAIL']['transport_smtp_password'] = 'password';
79 *
80 * $TYPO3_CONF_VARS['MAIL']['transport_sendmail_command'] = '/usr/sbin/sendmail -bs'
81 *
82 * @throws t3lib_exception
83 */
84 private function initializeTransport() {
85 $mailSettings = $GLOBALS['TYPO3_CONF_VARS']['MAIL'];
86 switch ($mailSettings['transport']) {
87
88 case 'smtp':
89 // Get settings to be used when constructing the transport object
90 list($host, $port) = preg_split('/:/', $mailSettings['transport_smtp_server']);
91 if ($host === '') {
92 throw new t3lib_exception(
93 '$TYPO3_CONF_VARS[\'MAIL\'][\'transport_smtp_server\'] needs to be set when transport is set to "smtp"',
94 1291068606
95 );
96 }
97 if ($port === '') {
98 $port = '25';
99 }
100 $useEncryption = ($mailSettings['transport_smtp_encrypt'] ? $mailSettings['transport_smtp_encrypt'] : NULL);
101
102 // Create our transport
103 $this->transport = Swift_SmtpTransport::newInstance($host, $port, $useEncryption);
104
105 // Need authentication?
106 $username = $mailSettings['transport_smtp_username'];
107 if ($username !== '') {
108 $this->transport->setUsername($username);
109 }
110 $password = $mailSettings['transport_smtp_password'];
111 if ($password !== '') {
112 $this->transport->setPassword($password);
113 }
114 break;
115
116 case 'sendmail':
117 $sendmailCommand = $mailSettings['transport_sendmail_command'];
118 if ($sendmailCommand === '') {
119 throw new t3lib_exception(
120 '$TYPO3_CONF_VARS[\'MAIL\'][\'transport_sendmail_command\'] needs to be set when transport is set to "sendmail"',
121 1291068620
122 );
123 }
124 // Create our transport
125 $this->transport = Swift_SendmailTransport::newInstance($sendmailCommand);
126 break;
127
128 case 'mbox':
129 $mboxFile = $mailSettings['transport_mbox_file'];
130 if ($mboxFile == '') {
131 throw new t3lib_exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_mbox_file\'] needs to be set when transport is set to "mbox"', 1294586645);
132 }
133 // Create our transport
134 $this->transport = t3lib_div::makeInstance('t3lib_mail_mboxtransport', $mboxFile);
135 break;
136
137 case 'mail':
138 // Create the transport, no configuration required
139 $this->transport = Swift_MailTransport::newInstance();
140 break;
141 default:
142 // Custom mail transport
143 $customTransport = t3lib_div::makeInstance($mailSettings['transport'], $mailSettings);
144 if ($customTransport instanceof Swift_Transport) {
145 $this->transport = $customTransport;
146 } else {
147 throw new RuntimeException(
148 $mailSettings['transport'] . ' is not an implementation of Swift_Transport,
149 but must implement that interface to be used as a mail transport.',
150 1323006478
151 );
152 }
153 }
154 return;
155 }
156 }
157
158 ?>