Added feature #15998: Create a new API to send mails based on SwiftMailer to replace...
[Packages/TYPO3.CMS.git] / t3lib / mail / class.t3lib_mail_mailer.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 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 * @throws t3lib_exception
55 */
56 public function __construct() {
57 try {
58 $this->initializeTransport();
59 } catch (Exception $e) {
60 throw new t3lib_exception($e->getMessage());
61 }
62 parent::__construct($this->transport);
63 }
64
65 /**
66 * Prepares a transport using the TYPO3_CONF_VARS configuration
67 *
68 * Used options:
69 * $TYPO3_CONF_VARS['MAIL']['transport'] = 'smtp' | 'sendmail' | 'mail'
70 *
71 * $TYPO3_CONF_VARS['MAIL']['transport_smtp_server'] = 'smtp.example.org';
72 * $TYPO3_CONF_VARS['MAIL']['transport_smtp_port'] = '25';
73 * $TYPO3_CONF_VARS['MAIL']['transport_smtp_encrypt'] = FALSE; # requires openssl in PHP
74 * $TYPO3_CONF_VARS['MAIL']['transport_smtp_username'] = 'username';
75 * $TYPO3_CONF_VARS['MAIL']['transport_smtp_password'] = 'password';
76 *
77 * $TYPO3_CONF_VARS['MAIL']['transport_sendmail_command'] = '/usr/sbin/sendmail -bs'
78 *
79 * @throws Exception
80 */
81 private function initializeTransport() {
82 $mailSettings = $GLOBALS['TYPO3_CONF_VARS']['MAIL'];
83 switch ($mailSettings['transport']) {
84
85 case 'smtp':
86 // Get settings to be used when constructing the transport object
87 list($host, $port) = split(':', $mailSettings['transport_smtp_server']);
88 if ($host === '') {
89 throw new t3lib_exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_smtp_server\'] needs to be set when transport is set to "smtp"');
90 }
91 if ($port === '') {
92 $port = '25';
93 }
94 $useEncryption = ( $mailSettings['transport_smtp_encrypt'] ? TRUE : FALSE);
95
96 // Create our transport
97 $this->transport = Swift_SmtpTransport::newInstance($host, $port, $useEncryption);
98
99 // Need authentication?
100 $username = $mailSettings['transport_smtp_username'];
101 if ($username !== '') {
102 $this->transport->setUsername($username);
103 }
104 $password = $mailSettings['transport_smtp_password'];
105 if ($password !== '') {
106 $this->transport->setPassword($password);
107 }
108 break;
109
110 case 'sendmail':
111 $sendmailCommand = $mailSettings['transport_sendmail_command'];
112 if ($sendmailCommand === '') {
113 throw new t3lib_exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_sendmail_command\'] needs to be set when transport is set to "sendmail"');
114 }
115 // Create our transport
116 $this->transport = Swift_SendmailTransport::newInstance($sendmailCommand);
117 break;
118
119 case 'mail':
120 default:
121 // Create the transport, no configuration required
122 $this->transport = Swift_MailTransport::newInstance();
123 break;
124 }
125 return;
126 }
127
128 }
129
130 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_mail_mailer.php']) {
131 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_mail_mailer.php']);
132 }
133
134 ?>