Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Smtp.php
Go to the documentation of this file.
1 <?php
2 
28 #require_once 'Zend/Mime.php';
29 
30 
34 #require_once 'Zend/Mail/Protocol/Abstract.php';
35 
36 
49 {
55  protected $_transport = 'tcp';
56 
57 
63  protected $_secure;
64 
65 
71  protected $_sess = false;
72 
73 
79  protected $_helo = false;
80 
81 
87  protected $_auth = false;
88 
89 
95  protected $_mail = false;
96 
97 
103  protected $_rcpt = false;
104 
105 
111  protected $_data = null;
112 
113 
123  public function __construct($host = '127.0.0.1', $port = null, array $config = array())
124  {
125  if (isset($config['ssl'])) {
126  switch (strtolower($config['ssl'])) {
127  case 'tls':
128  $this->_secure = 'tls';
129  break;
130 
131  case 'ssl':
132  $this->_transport = 'ssl';
133  $this->_secure = 'ssl';
134  if ($port == null) {
135  $port = 465;
136  }
137  break;
138 
139  default:
143  #require_once 'Zend/Mail/Protocol/Exception.php';
144  throw new Zend_Mail_Protocol_Exception($config['ssl'] . ' is unsupported SSL type');
145  break;
146  }
147  }
148 
149  // If no port has been specified then check the master PHP ini file. Defaults to 25 if the ini setting is null.
150  if ($port == null) {
151  if (($port = ini_get('smtp_port')) == '') {
152  $port = 25;
153  }
154  }
155 
156  parent::__construct($host, $port);
157  }
158 
159 
165  public function connect()
166  {
167  return $this->_connect($this->_transport . '://' . $this->_host . ':'. $this->_port);
168  }
169 
170 
178  public function helo($host = '127.0.0.1')
179  {
180  // Respect RFC 2821 and disallow HELO attempts if session is already initiated.
181  if ($this->_sess === true) {
185  #require_once 'Zend/Mail/Protocol/Exception.php';
186  throw new Zend_Mail_Protocol_Exception('Cannot issue HELO to existing session');
187  }
188 
189  // Validate client hostname
190  if (!$this->_validHost->isValid($host)) {
194  #require_once 'Zend/Mail/Protocol/Exception.php';
195  throw new Zend_Mail_Protocol_Exception(join(', ', $this->_validHost->getMessages()));
196  }
197 
198  // Initiate helo sequence
199  $this->_expect(220, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
200  $this->_ehlo($host);
201 
202  // If a TLS session is required, commence negotiation
203  if ($this->_secure == 'tls') {
204  $this->_send('STARTTLS');
205  $this->_expect(220, 180);
206  // TODO: Add STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT in the future when it is supported by PHP
207  if (!stream_socket_enable_crypto($this->_socket, true, STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT)) {
211  #require_once 'Zend/Mail/Protocol/Exception.php';
212  throw new Zend_Mail_Protocol_Exception('Unable to connect via TLS');
213  }
214  $this->_ehlo($host);
215  }
216 
217  $this->_startSession();
218  $this->auth();
219  }
220 
221 
229  protected function _ehlo($host)
230  {
231  // Support for older, less-compliant remote servers. Tries multiple attempts of EHLO or HELO.
232  try {
233  $this->_send('EHLO ' . $host);
234  $this->_expect(250, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
235  } catch (Zend_Mail_Protocol_Exception $e) {
236  $this->_send('HELO ' . $host);
237  $this->_expect(250, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
238  } catch (Zend_Mail_Protocol_Exception $e) {
239  throw $e;
240  }
241  }
242 
243 
251  public function mail($from)
252  {
253  if ($this->_sess !== true) {
257  #require_once 'Zend/Mail/Protocol/Exception.php';
258  throw new Zend_Mail_Protocol_Exception('A valid session has not been started');
259  }
260 
261  $this->_send('MAIL FROM:<' . $from . '>');
262  $this->_expect(250, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
263 
264  // Set mail to true, clear recipients and any existing data flags as per 4.1.1.2 of RFC 2821
265  $this->_mail = true;
266  $this->_rcpt = false;
267  $this->_data = false;
268  }
269 
270 
278  public function rcpt($to)
279  {
280  if ($this->_mail !== true) {
284  #require_once 'Zend/Mail/Protocol/Exception.php';
285  throw new Zend_Mail_Protocol_Exception('No sender reverse path has been supplied');
286  }
287 
288  // Set rcpt to true, as per 4.1.1.3 of RFC 2821
289  $this->_send('RCPT TO:<' . $to . '>');
290  $this->_expect(array(250, 251), 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
291  $this->_rcpt = true;
292  }
293 
294 
302  public function data($data)
303  {
304  // Ensure recipients have been set
305  if ($this->_rcpt !== true) {
309  #require_once 'Zend/Mail/Protocol/Exception.php';
310  throw new Zend_Mail_Protocol_Exception('No recipient forward path has been supplied');
311  }
312 
313  $this->_send('DATA');
314  $this->_expect(354, 120); // Timeout set for 2 minutes as per RFC 2821 4.5.3.2
315 
316  foreach (explode(Zend_Mime::LINEEND, $data) as $line) {
317  if (strpos($line, '.') === 0) {
318  // Escape lines prefixed with a '.'
319  $line = '.' . $line;
320  }
321  $this->_send($line);
322  }
323 
324  $this->_send('.');
325  $this->_expect(250, 600); // Timeout set for 10 minutes as per RFC 2821 4.5.3.2
326  $this->_data = true;
327  }
328 
329 
337  public function rset()
338  {
339  $this->_send('RSET');
340  // MS ESMTP doesn't follow RFC, see [ZF-1377]
341  $this->_expect(array(250, 220));
342 
343  $this->_mail = false;
344  $this->_rcpt = false;
345  $this->_data = false;
346  }
347 
348 
356  public function noop()
357  {
358  $this->_send('NOOP');
359  $this->_expect(250, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
360  }
361 
362 
371  public function vrfy($user)
372  {
373  $this->_send('VRFY ' . $user);
374  $this->_expect(array(250, 251, 252), 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
375  }
376 
377 
383  public function quit()
384  {
385  if ($this->_sess) {
386  $this->_send('QUIT');
387  $this->_expect(221, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
388  $this->_stopSession();
389  }
390  }
391 
392 
401  public function auth()
402  {
403  if ($this->_auth === true) {
407  #require_once 'Zend/Mail/Protocol/Exception.php';
408  throw new Zend_Mail_Protocol_Exception('Already authenticated for this session');
409  }
410  }
411 
412 
418  public function disconnect()
419  {
420  $this->_disconnect();
421  }
422 
423 
429  protected function _startSession()
430  {
431  $this->_sess = true;
432  }
433 
434 
440  protected function _stopSession()
441  {
442  $this->_sess = false;
443  }
444 }
_expect($code, $timeout=null)
Definition: Abstract.php:402
$config
Definition: fraud_order.php:17
__construct($host='127.0.0.1', $port=null, array $config=array())
Definition: Smtp.php:123
const LINEEND
Definition: Mime.php:42
$user
Definition: dummy_user.php:13
helo($host='127.0.0.1')
Definition: Smtp.php:178