Fixed bug #14050: CleanUp - CGL format of t3lib files - t3lib_mail_directory
[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 * @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());
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'
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 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) = split(':', $mailSettings['transport_smtp_server']);
93 if ($host === '') {
94 throw new t3lib_exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_smtp_server\'] needs to be set when transport is set to "smtp"');
95 }
96 if ($port === '') {
97 $port = '25';
98 }
99 $useEncryption = ($mailSettings['transport_smtp_encrypt'] ? TRUE : FALSE);
100
101 // Create our transport
102 $this->transport = Swift_SmtpTransport::newInstance($host, $port, $useEncryption);
103
104 // Need authentication?
105 $username = $mailSettings['transport_smtp_username'];
106 if ($username !== '') {
107 $this->transport->setUsername($username);
108 }
109 $password = $mailSettings['transport_smtp_password'];
110 if ($password !== '') {
111 $this->transport->setPassword($password);
112 }
113 break;
114
115 case 'sendmail':
116 $sendmailCommand = $mailSettings['transport_sendmail_command'];
117 if ($sendmailCommand === '') {
118 throw new t3lib_exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_sendmail_command\'] needs to be set when transport is set to "sendmail"');
119 }
120 // Create our transport
121 $this->transport = Swift_SendmailTransport::newInstance($sendmailCommand);
122 break;
123
124 case 'mail':
125 default:
126 // Create the transport, no configuration required
127 $this->transport = Swift_MailTransport::newInstance();
128 break;
129 }
130 return;
131 }
132
133 }
134
135 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_mail_mailer.php']) {
136 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_mail_mailer.php']);
137 }
138
139 ?>