Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   1  <?php
   2  /**
   3   * Copyright 2010-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 2010-2017 Horde LLC
  33   * @license   http://www.horde.org/licenses/bsd New BSD License
  34   * @package   Mail
  35   */
  36  
  37  /**
  38   * Mock implementation, for testing.
  39   *
  40   * @author    Chuck Hagenbuch <chuck@horde.org>
  41   * @author    Michael Slusarz <slusarz@horde.org>
  42   * @category  Horde
  43   * @copyright 2010-2017 Horde LLC
  44   * @license   http://www.horde.org/licenses/bsd New BSD License
  45   * @package   Mail
  46   */
  47  class Horde_Mail_Transport_Mock extends Horde_Mail_Transport
  48  {
  49      /**
  50       * Array of messages that have been sent with the mock.
  51       *
  52       * @var array
  53       */
  54      public $sentMessages = array();
  55  
  56      /**
  57       * Callback before sending mail.
  58       *
  59       * @var callback
  60       */
  61      protected $_preSendCallback;
  62  
  63      /**
  64       * Callback after sending mai.
  65       *
  66       * @var callback
  67       */
  68      protected $_postSendCallback;
  69  
  70      /**
  71       * @param array  Optional parameters:
  72       *   - postSendCallback: (callback) Called after an email would have been
  73       *                       sent.
  74       *   - preSendCallback: (callback) Called before an email would be sent.
  75       */
  76      public function __construct(array $params = array())
  77      {
  78          if (isset($params['preSendCallback']) &&
  79              is_callable($params['preSendCallback'])) {
  80              $this->_preSendCallback = $params['preSendCallback'];
  81          }
  82  
  83          if (isset($params['postSendCallback']) &&
  84              is_callable($params['postSendCallback'])) {
  85              $this->_postSendCallback = $params['postSendCallback'];
  86          }
  87      }
  88  
  89      /**
  90       */
  91      public function send($recipients, array $headers, $body)
  92      {
  93          if ($this->_preSendCallback) {
  94              call_user_func_array($this->_preSendCallback, array($this, $recipients, $headers, $body));
  95          }
  96  
  97          $headers = $this->_sanitizeHeaders($headers);
  98          list($from, $text_headers) = $this->prepareHeaders($headers);
  99  
 100          if (is_resource($body)) {
 101              stream_filter_register('horde_eol', 'Horde_Stream_Filter_Eol');
 102              stream_filter_append($body, 'horde_eol', STREAM_FILTER_READ, array('eol' => $this->sep));
 103  
 104              rewind($body);
 105              $body_txt = stream_get_contents($body);
 106          } else {
 107              $body_txt = $this->_normalizeEOL($body);
 108          }
 109  
 110          $from = $this->_getFrom($from, $headers);
 111          $recipients = $this->parseRecipients($recipients);
 112  
 113          $this->sentMessages[] = array(
 114              'body' => $body_txt,
 115              'from' => $from,
 116              'headers' => $headers,
 117              'header_text' => $text_headers,
 118              'recipients' => $recipients
 119          );
 120  
 121          if ($this->_postSendCallback) {
 122              call_user_func_array($this->_postSendCallback, array($this, $recipients, $headers, $body_txt));
 123          }
 124      }
 125  
 126  }