/t3lib/mail/class.t3lib_mail_mailer.php

https://github.com/itag/TYPO3v4-Core · PHP · 152 lines · 70 code · 15 blank · 67 comment · 12 complexity · f0502ea7b7edea20c66964e33e50aece MD5 · raw file

  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. // Make sure Swift's auto-loader is registered
  28. require_once(PATH_typo3 . 'contrib/swiftmailer/swift_required.php');
  29. /**
  30. * Adapter for Swift_Mailer to be used by TYPO3 extensions.
  31. *
  32. * This will use the setting in TYPO3_CONF_VARS to choose the correct transport
  33. * for it to work out-of-the-box.
  34. *
  35. * @author Ernesto Baschny <ernst@cron-it.de>
  36. * @package TYPO3
  37. * @subpackage t3lib
  38. */
  39. class t3lib_mail_Mailer extends Swift_Mailer {
  40. /**
  41. * @var Swift_Transport
  42. */
  43. protected $transport;
  44. /**
  45. * When constructing, also initializes the Swift_Transport like configured
  46. *
  47. * @param Swift_Transport optionally pass a transport to the constructor. By default the configured transport from $TYPO3_CONF_VARS is used
  48. * @throws t3lib_exception
  49. */
  50. public function __construct(Swift_Transport $transport = NULL) {
  51. if ($transport !== NULL) {
  52. $this->transport = $transport;
  53. } else {
  54. try {
  55. $this->initializeTransport();
  56. } catch (Exception $e) {
  57. throw new t3lib_exception($e->getMessage(), 1291068569);
  58. }
  59. }
  60. parent::__construct($this->transport);
  61. }
  62. /**
  63. * Prepares a transport using the TYPO3_CONF_VARS configuration
  64. *
  65. * Used options:
  66. * $TYPO3_CONF_VARS['MAIL']['transport'] = 'smtp' | 'sendmail' | 'mail' | 'mbox'
  67. *
  68. * $TYPO3_CONF_VARS['MAIL']['transport_smtp_server'] = 'smtp.example.org';
  69. * $TYPO3_CONF_VARS['MAIL']['transport_smtp_port'] = '25';
  70. * $TYPO3_CONF_VARS['MAIL']['transport_smtp_encrypt'] = FALSE; # requires openssl in PHP
  71. * $TYPO3_CONF_VARS['MAIL']['transport_smtp_username'] = 'username';
  72. * $TYPO3_CONF_VARS['MAIL']['transport_smtp_password'] = 'password';
  73. *
  74. * $TYPO3_CONF_VARS['MAIL']['transport_sendmail_command'] = '/usr/sbin/sendmail -bs'
  75. *
  76. * @throws t3lib_exception
  77. */
  78. private function initializeTransport() {
  79. $mailSettings = $GLOBALS['TYPO3_CONF_VARS']['MAIL'];
  80. switch ($mailSettings['transport']) {
  81. case 'smtp':
  82. // Get settings to be used when constructing the transport object
  83. list($host, $port) = preg_split('/:/', $mailSettings['transport_smtp_server']);
  84. if ($host === '') {
  85. throw new t3lib_exception(
  86. '$TYPO3_CONF_VARS[\'MAIL\'][\'transport_smtp_server\'] needs to be set when transport is set to "smtp"',
  87. 1291068606
  88. );
  89. }
  90. if ($port === '') {
  91. $port = '25';
  92. }
  93. $useEncryption = ($mailSettings['transport_smtp_encrypt'] ? $mailSettings['transport_smtp_encrypt'] : NULL);
  94. // Create our transport
  95. $this->transport = Swift_SmtpTransport::newInstance($host, $port, $useEncryption);
  96. // Need authentication?
  97. $username = $mailSettings['transport_smtp_username'];
  98. if ($username !== '') {
  99. $this->transport->setUsername($username);
  100. }
  101. $password = $mailSettings['transport_smtp_password'];
  102. if ($password !== '') {
  103. $this->transport->setPassword($password);
  104. }
  105. break;
  106. case 'sendmail':
  107. $sendmailCommand = $mailSettings['transport_sendmail_command'];
  108. if ($sendmailCommand === '') {
  109. throw new t3lib_exception(
  110. '$TYPO3_CONF_VARS[\'MAIL\'][\'transport_sendmail_command\'] needs to be set when transport is set to "sendmail"',
  111. 1291068620
  112. );
  113. }
  114. // Create our transport
  115. $this->transport = Swift_SendmailTransport::newInstance($sendmailCommand);
  116. break;
  117. case 'mbox':
  118. $mboxFile = $mailSettings['transport_mbox_file'];
  119. if ($mboxFile == '') {
  120. throw new t3lib_exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_mbox_file\'] needs to be set when transport is set to "mbox"', 1294586645);
  121. }
  122. // Create our transport
  123. $this->transport = t3lib_div::makeInstance('t3lib_mail_mboxtransport', $mboxFile);
  124. break;
  125. case 'mail':
  126. default:
  127. // Create the transport, no configuration required
  128. $this->transport = Swift_MailTransport::newInstance();
  129. break;
  130. }
  131. return;
  132. }
  133. }
  134. if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_mail_mailer.php'])) {
  135. include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_mail_mailer.php']);
  136. }
  137. ?>