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
27 #require_once 'Zend/Mime.php';
28 
32 #require_once 'Zend/Mail/Protocol/Smtp.php';
33 
37 #require_once 'Zend/Mail/Transport/Abstract.php';
38 
39 
52 {
58  public $EOL = "\n";
59 
65  protected $_host;
66 
67 
73  protected $_port;
74 
75 
81  protected $_name = 'localhost';
82 
83 
89  protected $_auth;
90 
91 
97  protected $_config;
98 
99 
105  protected $_connection;
106 
107 
118  public function __construct($host = '127.0.0.1', Array $config = array())
119  {
120  if (isset($config['name'])) {
121  $this->_name = $config['name'];
122  }
123  if (isset($config['port'])) {
124  $this->_port = $config['port'];
125  }
126  if (isset($config['auth'])) {
127  $this->_auth = $config['auth'];
128  }
129 
130  $this->_host = $host;
131  $this->_config = $config;
132  }
133 
134 
140  public function __destruct()
141  {
142  if ($this->_connection instanceof Zend_Mail_Protocol_Smtp) {
143  try {
144  $this->_connection->quit();
145  } catch (Zend_Mail_Protocol_Exception $e) {
146  // ignore
147  }
148  $this->_connection->disconnect();
149  }
150  }
151 
152 
161  {
162  $this->_connection = $connection;
163  }
164 
165 
171  public function getConnection()
172  {
173  return $this->_connection;
174  }
175 
185  public function _sendMail()
186  {
187  // If sending multiple messages per session use existing adapter
188  if (!($this->_connection instanceof Zend_Mail_Protocol_Smtp)) {
189  // Check if authentication is required and determine required class
190  $connectionClass = 'Zend_Mail_Protocol_Smtp';
191  if ($this->_auth) {
192  $connectionClass .= '_Auth_' . ucwords($this->_auth);
193  }
194  if (!class_exists($connectionClass)) {
195  #require_once 'Zend/Loader.php';
196  Zend_Loader::loadClass($connectionClass);
197  }
198  $this->setConnection(new $connectionClass($this->_host, $this->_port, $this->_config));
199  $this->_connection->connect();
200  $this->_connection->helo($this->_name);
201  } else {
202  // Reset connection to ensure reliable transaction
203  $this->_connection->rset();
204  }
205 
206  // Set sender email address
207  $this->_connection->mail($this->_mail->getReturnPath());
208 
209  // Set recipient forward paths
210  foreach ($this->_mail->getRecipients() as $recipient) {
211  $this->_connection->rcpt($recipient);
212  }
213 
214  // Issue DATA command to client
215  $this->_connection->data($this->header . Zend_Mime::LINEEND . $this->body);
216  }
217 
228  protected function _prepareHeaders($headers)
229  {
230  if (!$this->_mail) {
234  #require_once 'Zend/Mail/Transport/Exception.php';
235  throw new Zend_Mail_Transport_Exception('_prepareHeaders requires a registered Zend_Mail object');
236  }
237 
238  unset($headers['Bcc']);
239 
240  // Prepare headers
241  parent::_prepareHeaders($headers);
242  }
243 }
static loadClass($class, $dirs=null)
Definition: Loader.php:52
$config
Definition: fraud_order.php:17
_prepareHeaders($headers)
Definition: Smtp.php:228
setConnection(Zend_Mail_Protocol_Abstract $connection)
Definition: Smtp.php:160
const LINEEND
Definition: Mime.php:42
__construct($host='127.0.0.1', Array $config=array())
Definition: Smtp.php:118
$connection
Definition: bulk.php:13