Textpattern PHP Cross Reference Content Management Systems

Source: /textpattern/vendors/Textpattern/Mail/Adapter/Mail.php - 263 lines - 6537 bytes - Summary - Text - Print

Description: Adapter for PHP's mail function.

   1  <?php
   2  
   3  /*
   4   * Textpattern Content Management System
   5   * https://textpattern.com/
   6   *
   7   * Copyright (C) 2020 The Textpattern Development Team
   8   *
   9   * This file is part of Textpattern.
  10   *
  11   * Textpattern is free software; you can redistribute it and/or
  12   * modify it under the terms of the GNU General Public License
  13   * as published by the Free Software Foundation, version 2.
  14   *
  15   * Textpattern is distributed in the hope that it will be useful,
  16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18   * GNU General Public License for more details.
  19   *
  20   * You should have received a copy of the GNU General Public License
  21   * along with Textpattern. If not, see <https://www.gnu.org/licenses/>.
  22   */
  23  /**
  24   * Adapter for PHP's mail function.
  25   *
  26   * @since   4.6.0
  27   * @package Mail
  28   */
  29  
  30  namespace Textpattern\Mail\Adapter;
  31  
  32  use Textpattern\Mail\Encode;
  33  use Textpattern\Mail\Exception;
  34  use Textpattern\Mail\Message;
  35  
  36  class Mail implements \Textpattern\Mail\AdapterInterface
  37  {
  38      /**
  39       * The email fields.
  40       *
  41       * @var \Textpattern\Mail\Message
  42       */
  43  
  44      protected $mail;
  45  
  46      /**
  47       * Encoded email fields.
  48       *
  49       * @var \Textpattern\Mail\Message
  50       */
  51  
  52      protected $encoded;
  53  
  54      /**
  55       * Line separator.
  56       *
  57       * @var string
  58       */
  59  
  60      protected $separator = "\n";
  61  
  62      /**
  63       * The message encoding.
  64       *
  65       * @var string
  66       */
  67  
  68      protected $charset = 'UTF-8';
  69  
  70      /**
  71       * SMTP envelope sender address.
  72       *
  73       * @var string|bool
  74       */
  75  
  76      protected $smtpFrom = false;
  77  
  78      /**
  79       * The encoder.
  80       *
  81       * @var Encode
  82       */
  83  
  84      protected $encoder;
  85  
  86      /**
  87       * Constructor.
  88       */
  89  
  90      public function __construct()
  91      {
  92          $this->mail = new Message();
  93          $this->encoded = new Message();
  94          $this->encoder = new Encode();
  95  
  96          if (IS_WIN) {
  97              $this->separator = "\r\n";
  98          }
  99  
 100          if (get_pref('override_emailcharset') && is_callable('utf8_decode')) {
 101              $this->charset = 'ISO-8859-1';
 102              $this->mail->headers['Content-Type'] = 'text/plain; charset="ISO-8859-1"';
 103              $this->encoded->headers['Content-Type'] = 'text/plain; charset="ISO-8859-1"';
 104          }
 105  
 106          if (filter_var(get_pref('smtp_from'), FILTER_VALIDATE_EMAIL)) {
 107              if (IS_WIN) {
 108                  ini_set('sendmail_from', get_pref('smtp_from'));
 109              } else {
 110                  $this->smtpFrom = get_pref('smtp_from');
 111              }
 112          }
 113      }
 114  
 115      /**
 116       * Sets or gets a message field.
 117       *
 118       * @param  string $name The field
 119       * @param  array  $args Arguments
 120       * @return \Textpattern\Mail\AdapterInterface
 121       * @throws \Textpattern\Mail\Exception
 122       */
 123  
 124      public function __call($name, array $args = null)
 125      {
 126          if (!$args) {
 127              if (property_exists($this->mail, $name) === false) {
 128                  throw new Exception(gTxt('invalid_argument', array('{name}' => 'name')));
 129              }
 130  
 131              return $this->mail->$name;
 132          }
 133  
 134          if (isset($args[1])) {
 135              return $this->addAddress($name, $args[0], $args[1]);
 136          }
 137  
 138          return $this->addAddress($name, $args[0]);
 139      }
 140  
 141      /**
 142       * {@inheritdoc}
 143       */
 144  
 145      public function subject($subject)
 146      {
 147          if (!is_scalar($subject) || (string)$subject === '') {
 148              throw new Exception(gTxt('invalid_argument', array('{name}' => 'subject')));
 149          }
 150  
 151          $this->mail->subject = $subject;
 152  
 153          if ($this->charset != 'UTF-8') {
 154              $subject = utf8_decode($subject);
 155          }
 156  
 157          $this->encoded->subject = $this->encoder->header($this->encoder->escapeHeader($subject), 'text');
 158  
 159          return $this;
 160      }
 161  
 162      /**
 163       * {@inheritdoc}
 164       */
 165  
 166      public function body($body)
 167      {
 168          $this->mail->body = $body;
 169  
 170          if ($this->charset != 'UTF-8') {
 171              $body = utf8_decode($body);
 172          }
 173  
 174          $body = str_replace("\r\n", "\n", $body);
 175          $body = str_replace("\r", "\n", $body);
 176          $body = str_replace("\n", $this->separator, $body);
 177          $this->encoded->body = deNull($body);
 178  
 179          return $this;
 180      }
 181  
 182      /**
 183       * {@inheritdoc}
 184       */
 185  
 186      public function header($name, $value)
 187      {
 188          if ((string)$value === '' || !preg_match('/^[\041-\071\073-\176]+$/', $name)) {
 189              throw new Exception(gTxt('invalid_header'));
 190          }
 191  
 192          $this->mail->headers[$name] = $value;
 193          $this->encoded->headers[$name] = $this->encoder->header($this->encoder->escapeHeader($value), 'phrase');
 194  
 195          return $this;
 196      }
 197  
 198      /**
 199       * {@inheritdoc}
 200       */
 201  
 202      public function send()
 203      {
 204          if (is_disabled('mail')) {
 205              throw new Exception(gTxt('disabled_function', array('{name}' => 'mail')));
 206          }
 207  
 208          if (!$this->mail->from || !$this->mail->to) {
 209              throw new Exception(gTxt('from_or_to_address_missing'));
 210          }
 211  
 212          $headers = array();
 213          $headers['From'] = $this->encoded->from;
 214  
 215          if ($this->encoded->cc) {
 216              $headers['Cc'] = $this->encoded->cc;
 217          }
 218  
 219          if ($this->encoded->bcc) {
 220              $headers['Bcc'] = $this->encoded->bcc;
 221          }
 222  
 223          if ($this->encoded->replyTo) {
 224              $headers['Reply-to'] = $this->encoded->replyTo;
 225          }
 226  
 227          $headers += $this->encoded->headers;
 228  
 229          foreach ($headers as $name => &$value) {
 230              $value = $name.': '.$value;
 231          }
 232  
 233          $headers = join($this->separator, $headers).$this->separator;
 234          $additional_headers = ($this->smtpFrom ? '-f'.$this->smtpFrom : null);
 235  
 236          if (mail($this->encoded->to, $this->encoded->subject, $this->encoded->body, $headers, $additional_headers) === false) {
 237              throw new Exception(gTxt('sending_failed'));
 238          }
 239  
 240          return $this;
 241      }
 242  
 243      /**
 244       * Adds an address to the specified field.
 245       *
 246       * @param  string $field   The field
 247       * @param  string $address The email address
 248       * @param  string $name    The name
 249       * @return \Textpattern\Mail\AdapterInterface
 250       */
 251  
 252      protected function addAddress($field, $address, $name = '')
 253      {
 254          if (filter_var($address, FILTER_VALIDATE_EMAIL)) {
 255              $this->mail->$field = array_merge($this->mail->$field, array($address => $name));
 256              $this->encoded->$field = $this->encoder->addressList($this->mail->$field);
 257  
 258              return $this;
 259          }
 260  
 261          throw new Exception(gTxt('invalid_argument', array('{name}' => 'address')));
 262      }
 263  }

title

Description

title

Description

title

Description

title

title

Body