Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Sendmail.php
Go to the documentation of this file.
1 <?php
27 #require_once 'Zend/Mail/Transport/Abstract.php';
28 
29 
40 {
46  public $subject = null;
47 
53  public $parameters;
54 
60  public $EOL = PHP_EOL;
61 
66  protected $_errstr;
67 
74  public function __construct($parameters = null)
75  {
76  if ($parameters instanceof Zend_Config) {
77  $parameters = $parameters->toArray();
78  }
79 
80  if (is_array($parameters)) {
81  $parameters = implode(' ', $parameters);
82  }
83 
84  $this->parameters = $parameters;
85  }
86 
96  public function _sendMail()
97  {
98  set_error_handler(array($this, '_handleMailErrors'));
99  $result = mail(
100  $this->recipients,
101  $this->_mail->getSubject(),
102  $this->body,
103  $this->header);
104  restore_error_handler();
105 
106  if ($this->_errstr !== null || !$result) {
110  #require_once 'Zend/Mail/Transport/Exception.php';
111  throw new Zend_Mail_Transport_Exception('Unable to send mail. ' . $this->_errstr);
112  }
113  }
114 
127  protected function _prepareHeaders($headers)
128  {
129  if (!$this->_mail) {
133  #require_once 'Zend/Mail/Transport/Exception.php';
134  throw new Zend_Mail_Transport_Exception('_prepareHeaders requires a registered Zend_Mail object');
135  }
136 
137  // mail() uses its $to parameter to set the To: header, and the $subject
138  // parameter to set the Subject: header. We need to strip them out.
139  if (0 === strpos(PHP_OS, 'WIN')) {
140  // If the current recipients list is empty, throw an error
141  if (empty($this->recipients)) {
145  #require_once 'Zend/Mail/Transport/Exception.php';
146  throw new Zend_Mail_Transport_Exception('Missing To addresses');
147  }
148  } else {
149  // All others, simply grab the recipients and unset the To: header
150  if (!isset($headers['To'])) {
154  #require_once 'Zend/Mail/Transport/Exception.php';
155  throw new Zend_Mail_Transport_Exception('Missing To header');
156  }
157 
158  unset($headers['To']['append']);
159  $this->recipients = implode(',', $headers['To']);
160  }
161 
162  // Remove recipient header
163  unset($headers['To']);
164 
165  // Remove subject header, if present
166  if (isset($headers['Subject'])) {
167  unset($headers['Subject']);
168  }
169 
170  // Sanitize the From header
171  if (isset($headers['From'])) {
172  $addressList = array_filter($headers['From'], function ($key) {
173  return $key !== 'append';
174  }, ARRAY_FILTER_USE_KEY);
175  foreach ($addressList as $address) {
176  if (preg_match('/\\\"/', $address)) {
177  throw new Zend_Mail_Transport_Exception('Potential code injection in From header');
178  }
179  }
180  }
181 
182  // Prepare headers
183  parent::_prepareHeaders($headers);
184 
185  // Fix issue with empty blank line ontop when using Sendmail Trnasport
186  $this->header = rtrim($this->header);
187  }
188 
199  public function _handleMailErrors($errno, $errstr, $errfile = null, $errline = null, array $errcontext = null)
200  {
201  $this->_errstr = $errstr;
202  return true;
203  }
204 
205 }
__construct($parameters=null)
Definition: Sendmail.php:74
$address
Definition: customer.php:38
_handleMailErrors($errno, $errstr, $errfile=null, $errline=null, array $errcontext=null)
Definition: Sendmail.php:199