TYPO3 CMS  TYPO3_6-2
Mailer.php
Go to the documentation of this file.
1 <?php
3 
17 // Make sure Swift's auto-loader is registered
18 require_once PATH_typo3 . 'contrib/swiftmailer/swift_required.php';
19 
28 class Mailer extends \Swift_Mailer {
29 
33  protected $transport;
34 
38  protected $mailSettings = array();
39 
46  public function __construct(\Swift_Transport $transport = NULL) {
47  if ($transport !== NULL) {
48  $this->transport = $transport;
49  } else {
50  if (empty($this->mailSettings)) {
51  $this->injectMailSettings();
52  }
53  try {
54  $this->initializeTransport();
55  } catch (\Exception $e) {
56  throw new \TYPO3\CMS\Core\Exception($e->getMessage(), 1291068569);
57  }
58  }
59  parent::__construct($this->transport);
60  }
61 
79  private function initializeTransport() {
80  switch ($this->mailSettings['transport']) {
81  case 'smtp':
82  // Get settings to be used when constructing the transport object
83  list($host, $port) = preg_split('/:/', $this->mailSettings['transport_smtp_server']);
84  if ($host === '') {
85  throw new \TYPO3\CMS\Core\Exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_smtp_server\'] needs to be set when transport is set to "smtp"', 1291068606);
86  }
87  if ($port === NULL || $port === '') {
88  $port = '25';
89  }
90  $useEncryption = $this->mailSettings['transport_smtp_encrypt'] ?: NULL;
91  // Create our transport
92  $this->transport = \Swift_SmtpTransport::newInstance($host, $port, $useEncryption);
93  // Need authentication?
94  $username = $this->mailSettings['transport_smtp_username'];
95  if ($username !== '') {
96  $this->transport->setUsername($username);
97  }
98  $password = $this->mailSettings['transport_smtp_password'];
99  if ($password !== '') {
100  $this->transport->setPassword($password);
101  }
102  break;
103  case 'sendmail':
104  $sendmailCommand = $this->mailSettings['transport_sendmail_command'];
105  if (empty($sendmailCommand)) {
106  throw new \TYPO3\CMS\Core\Exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_sendmail_command\'] needs to be set when transport is set to "sendmail"', 1291068620);
107  }
108  // Create our transport
109  $this->transport = \Swift_SendmailTransport::newInstance($sendmailCommand);
110  break;
111  case 'mbox':
112  $mboxFile = $this->mailSettings['transport_mbox_file'];
113  if ($mboxFile == '') {
114  throw new \TYPO3\CMS\Core\Exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_mbox_file\'] needs to be set when transport is set to "mbox"', 1294586645);
115  }
116  // Create our transport
117  $this->transport = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MboxTransport', $mboxFile);
118  break;
119  case 'mail':
120  // Create the transport, no configuration required
121  $this->transport = \Swift_MailTransport::newInstance();
122  break;
123  default:
124  // Custom mail transport
125  $customTransport = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($this->mailSettings['transport'], $this->mailSettings);
126  if ($customTransport instanceof \Swift_Transport) {
127  $this->transport = $customTransport;
128  } else {
129  throw new \RuntimeException($this->mailSettings['transport'] . ' is not an implementation of \\Swift_Transport,
130  but must implement that interface to be used as a mail transport.', 1323006478);
131  }
132  }
133  }
134 
141  public function injectMailSettings(array $mailSettings = NULL) {
142  if (is_array($mailSettings)) {
143  $this->mailSettings = $mailSettings;
144  } else {
145  $this->mailSettings = (array) $GLOBALS['TYPO3_CONF_VARS']['MAIL'];
146  }
147  }
148 
149 }
$host
Definition: server.php:35
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
__construct(\Swift_Transport $transport=NULL)
Definition: Mailer.php:46
injectMailSettings(array $mailSettings=NULL)
Definition: Mailer.php:141