Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   1  <?php
   2  /**
   3   * Copyright 2013-2017 Horde LLC (http://www.horde.org/)
   4   * All rights reserved.
   5   *
   6   * Redistribution and use in source and binary forms, with or without
   7   * modification, are permitted provided that the following conditions
   8   * are met:
   9   *
  10   * o Redistributions of source code must retain the above copyright
  11   *   notice, this list of conditions and the following disclaimer.
  12   * o Redistributions in binary form must reproduce the above copyright
  13   *   notice, this list of conditions and the following disclaimer in the
  14   *   documentation and/or other materials provided with the distribution.
  15   * o The names of the authors may not be used to endorse or promote
  16   *   products derived from this software without specific prior written
  17   *   permission.
  18   *
  19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30   *
  31   * @category  Horde
  32   * @copyright 2013-2017 Horde LLC
  33   * @license   http://www.horde.org/licenses/bsd New BSD License
  34   * @package   Mail
  35   */
  36  
  37  /**
  38   * SMTP implementation using Horde_Smtp.
  39   *
  40   * @author    Michael Slusarz <slusarz@horde.org>
  41   * @category  Horde
  42   * @copyright 2013-2017 Horde LLC
  43   * @license   http://www.horde.org/licenses/bsd New BSD License
  44   * @package   Mail
  45   */
  46  class Horde_Mail_Transport_Smtphorde extends Horde_Mail_Transport
  47  {
  48      /**
  49       * @deprecated
  50       */
  51      public $send8bit = false;
  52  
  53      /**
  54       * SMTP object.
  55       *
  56       * @var Horde_Smtp
  57       */
  58      protected $_smtp = null;
  59  
  60      /**
  61       * Constructor.
  62       *
  63       * @param array $params  Additional parameters:
  64       *   - chunk_size: (integer) If CHUNKING is supported on the server, the
  65       *                 chunk size (in octets) to send. 0 will disable chunking.
  66       *                 @since Horde_Smtp 1.7.0
  67       *   - context: (array) Any context parameters passed to
  68       *              stream_create_context(). @since Horde_Smtp 1.9.0
  69       *   - debug: (string) If set, will output debug information to the stream
  70       *            provided. The value can be any PHP supported wrapper that
  71       *            can be opened via fopen().
  72       *            DEFAULT: No debug output
  73       *   - host: (string) The SMTP server.
  74       *           DEFAULT: localhost
  75       *   - localhost: (string) The hostname of the localhost. (since Horde_Smtp
  76                        1.9.0)
  77       *                DEFAULT: Auto-determined.
  78       *   - password: (string) The SMTP password.
  79       *               DEFAULT: NONE
  80       *   - port: (string) The SMTP port.
  81       *           DEFAULT: 587
  82       *   - secure: (string) Use SSL or TLS to connect.
  83       *             DEFAULT: true (use 'tls' option, if available)
  84       *     - false (No encryption)
  85       *     - 'ssl' (Auto-detect SSL version)
  86       *     - 'sslv2' (Force SSL version 2)
  87       *     - 'sslv3' (Force SSL version 3)
  88       *     - 'tls' (TLS; started via protocol-level negotation over
  89       *       unencrypted channel; RECOMMENDED way of initiating secure
  90       *       connection)
  91       *     - 'tlsv1' (TLS direct version 1.x connection to server) [@since
  92       *       Horde_Smtp .3.0]
  93       *     - true (Use TLS, if available) [@since Horde_Smtp 1.2.0]
  94       *             DEFAULT: No encryption
  95       *   - timeout: (integer) Connection timeout, in seconds.
  96       *              DEFAULT: 30 seconds
  97       *   - username: (string) The SMTP username.
  98       *               DEFAULT: NONE
  99       *   - xoauth2_token: (string) If set, will authenticate via the XOAUTH2
 100       *                    mechanism (if available) with this token. Either a
 101       *                    string or a Horde_Smtp_Password object (since
 102       *                    Horde_Smtp 1.1.0).
 103       */
 104      public function __construct(array $params = array())
 105      {
 106          $this->_params = $params;
 107  
 108          /* SMTP requires CRLF line endings. */
 109          $this->sep = "\r\n";
 110      }
 111  
 112      /**
 113       */
 114      public function __get($name)
 115      {
 116          switch ($name) {
 117          case 'eai':
 118              $this->getSMTPObject();
 119              return $this->_smtp->data_intl;
 120          }
 121  
 122          return parent::__get($name);
 123      }
 124  
 125      /**
 126       */
 127      public function send($recipients, array $headers, $body)
 128      {
 129          /* If we don't already have an SMTP object, create one. */
 130          $this->getSMTPObject();
 131  
 132          $headers = $this->_sanitizeHeaders($headers);
 133          list($from, $textHeaders) = $this->prepareHeaders($headers);
 134          $from = $this->_getFrom($from, $headers);
 135  
 136          $combine = Horde_Stream_Wrapper_Combine::getStream(array(
 137              rtrim($textHeaders, $this->sep),
 138              $this->sep . $this->sep,
 139              $body
 140          ));
 141  
 142          try {
 143              $this->_smtp->send($from, $recipients, $combine);
 144          } catch (Horde_Smtp_Exception $e) {
 145              throw new Horde_Mail_Exception($e);
 146          }
 147      }
 148  
 149      /**
 150       * Connect to the SMTP server by instantiating a Horde_Smtp object.
 151       *
 152       * @return Horde_Smtp  The SMTP object.
 153       * @throws Horde_Mail_Exception
 154       */
 155      public function getSMTPObject()
 156      {
 157          if (!$this->_smtp) {
 158              $this->_smtp = new Horde_Smtp($this->_params);
 159              try {
 160                  $this->_smtp->login();
 161              } catch (Horde_Smtp_Exception $e) {
 162                  throw new Horde_Mail_Exception($e);
 163              }
 164          }
 165  
 166          return $this->_smtp;
 167      }
 168  
 169  }