Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.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   * Internal PHP-mail() interface.
  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_Mail extends Horde_Mail_Transport
  48  {
  49      /**
  50       * @param array $params  Additional parameters:
  51       *   - args: (string) Extra arguments for the mail() function.
  52       */
  53      public function __construct(array $params = array())
  54      {
  55          $this->_params = array_merge($this->_params, $params);
  56      }
  57  
  58      /**
  59       */
  60      public function send($recipients, array $headers, $body)
  61      {
  62          $headers = $this->_sanitizeHeaders($headers);
  63          $recipients = implode(',', $this->parseRecipients($recipients));
  64          $subject = '';
  65  
  66          foreach (array_keys($headers) as $hdr) {
  67              if (strcasecmp($hdr, 'Subject') === 0) {
  68                  // Get the Subject out of the headers array so that we can
  69                  // pass it as a separate argument to mail().
  70                  $subject = $headers[$hdr];
  71                  unset($headers[$hdr]);
  72              } elseif (strcasecmp($hdr, 'To') === 0) {
  73                  // Remove the To: header.  The mail() function will add its
  74                  // own To: header based on the contents of $recipients.
  75                  unset($headers[$hdr]);
  76              }
  77          }
  78  
  79          // Flatten the headers out.
  80          list(, $text_headers) = $this->prepareHeaders($headers);
  81  
  82          // mail() requires a string for $body. If resource, need to convert
  83          // to a string.
  84          if (is_resource($body)) {
  85              $body_str = '';
  86  
  87              stream_filter_register('horde_eol', 'Horde_Stream_Filter_Eol');
  88              stream_filter_append($body, 'horde_eol', STREAM_FILTER_READ, array('eol' => $this->sep));
  89  
  90              rewind($body);
  91              while (!feof($body)) {
  92                  $body_str .= fread($body, 8192);
  93              }
  94              $body = $body_str;
  95          } else {
  96              // Convert EOL characters in body.
  97              $body = $this->_normalizeEOL($body);
  98          }
  99  
 100          // We only use mail()'s optional fifth parameter if the additional
 101          // parameters have been provided and we're not running in safe mode.
 102          if (empty($this->_params) || ini_get('safe_mode')) {
 103              $result = mail($recipients, $subject, $body, $text_headers);
 104          } else {
 105              $result = mail($recipients, $subject, $body, $text_headers, isset($this->_params['args']) ? $this->_params['args'] : '');
 106          }
 107  
 108          // If the mail() function returned failure, we need to create an
 109          // Exception and return it instead of the boolean result.
 110          if ($result === false) {
 111              throw new Horde_Mail_Exception('mail() returned failure.');
 112          }
 113      }
 114  
 115  }