diff --git a/Controller/Adminhtml/Connection/Test.php b/Controller/Adminhtml/Connection/Test.php index 425d4d8..80ac35c 100644 --- a/Controller/Adminhtml/Connection/Test.php +++ b/Controller/Adminhtml/Connection/Test.php @@ -102,13 +102,15 @@ public function execute() } $data = [ - 'host' => $request->getParam('host'), 'config' => [ + 'host' => $request->getParam('host'), 'port' => $request->getParam('port'), - 'auth' => $request->getParam('auth'), - 'ssl' => $request->getParam('ssl'), - 'username' => $request->getParam('user'), - 'password' => $password + 'connection_class' => $request->getParam('auth'), + 'connection_config' => [ + 'ssl' => $request->getParam('ssl'), + 'username' => $request->getParam('user'), + 'password' => $password + ] ] ]; diff --git a/Helper/Data.php b/Helper/Data.php index 4d468d0..4965f43 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -126,13 +126,15 @@ public function getDays($store = null) */ public function getConfig(array $config = []) { - $config['host'] = $this->_getConfig(self::XML_CONFIG_HOST); $config['config'] = [ + 'host' => $this->_getConfig(self::XML_CONFIG_HOST), 'port' => $this->_getConfig(self::XML_CONFIG_PORT), - 'auth' => $this->_getConfig(self::XML_CONFIG_AUTH), - 'ssl' => $this->_getConfig(self::XML_CONFIG_SSL), - 'username' => $this->_getConfig(self::XML_CONFIG_USER), - 'password' => $this->_getConfig(self::XML_CONFIG_PASS) + 'connection_class' => $this->_getConfig(self::XML_CONFIG_AUTH), + 'connection_config' => [ + 'ssl' => $this->_getConfig(self::XML_CONFIG_SSL), + 'username' => $this->_getConfig(self::XML_CONFIG_USER), + 'password' => $this->_getConfig(self::XML_CONFIG_PASS) + ] ]; return $config; } diff --git a/Model/LogManagement.php b/Model/LogManagement.php index 4be23a2..f5b5431 100644 --- a/Model/LogManagement.php +++ b/Model/LogManagement.php @@ -5,7 +5,8 @@ */ namespace Faonni\Smtp\Model; -use Magento\Framework\Mail\MessageInterface; +use Zend\Mail\Message; +use Zend\Mail\AddressList; use Faonni\Smtp\Helper\Data as SmtpHelper; use Faonni\Smtp\Model\ResourceModel\Log\CollectionFactory; @@ -41,31 +42,46 @@ public function __construct( $this->_helper = $helper; $this->_collection = $collectionFactory->create(); } - + + /** + * Retrieve emails + * + * @param AddressList $addressList + * @return string + */ + protected function _getEmail(AddressList $addressList) + { + $emails = []; + $addressList->rewind(); + while ($addressList->valid()) { + $address = $addressList->current(); + $emails[] = $address->getEmail(); + $addressList->next(); + } + return implode(',', $emails); + } + /** * Add Log Record * - * @param MessageInterface $message + * @param Message $message * @param string $error * @return void */ - public function add(MessageInterface $message, $error) + public function add(Message $message, $error) { if (!$this->_helper->isLogEnabled()) { return; } - $log = $this->_collection->getNewEmptyItem(); - $recipients = $message->getRecipients(); - $recipient = reset($recipients); - + $log = $this->_collection->getNewEmptyItem(); $log->setData([ - 'subject' => $message->getSubject(), - 'message_body' => $message->getBody()->getRawContent(), - 'from' => $message->getFrom(), - 'recipient_email' => $recipient, - 'error' => $error, - 'status' => $error ? 0 : 1 + 'subject' => $message->getSubject(), + 'message_body' => $message->getBodyText(), + 'from' => $this->_getEmail($message->getFrom()), + 'recipient_email' => $this->_getEmail($message->getTo()), + 'error' => $error, + 'status' => $error ? 0 : 1 ]); $log->save(); } diff --git a/Model/Transport.php b/Model/Transport.php index b296d8e..846b91a 100644 --- a/Model/Transport.php +++ b/Model/Transport.php @@ -9,12 +9,16 @@ use Magento\Framework\Mail\MessageInterface; use Magento\Framework\Exception\MailException; use Magento\Framework\Phrase; +use Zend\Mail\Message as ZendMessage; +use Zend\Mail\Transport\Smtp; +use Zend\Mail\Transport\SmtpOptions; +use Zend\Mail\Protocol\Smtp as SmtpProtocol; use Faonni\Smtp\Model\LogManagement; /** * Smtp Transport */ -class Transport extends \Zend_Mail_Transport_Smtp implements TransportInterface +class Transport implements TransportInterface { /** * Message @@ -24,14 +28,21 @@ class Transport extends \Zend_Mail_Transport_Smtp implements TransportInterface protected $_message; /** - * Log Management + * Log management * * @var \Faonni\Smtp\Model\LogManagement */ protected $_logManager; /** - * Initialize Transport + * Smtp transport + * + * @var \Zend\Mail\Transport\Smtp + */ + private $_smtpTransport; + + /** + * Initialize transport * * @param MessageInterface $message * @param LogManagement $logManager @@ -41,87 +52,72 @@ class Transport extends \Zend_Mail_Transport_Smtp implements TransportInterface public function __construct( MessageInterface $message, LogManagement $logManager, - $host = '127.0.0.1', - array $config = [] + array $config ) { - if (!$message instanceof \Zend_Mail) { - throw new \InvalidArgumentException( - 'The message should be an instance of \Zend_Mail' - ); - } - $this->_message = $message; $this->_logManager = $logManager; - - parent::__construct( - $host, - $config + $this->_smtpTransport = new Smtp( + new SmtpOptions($config) ); } /** - * Send a Mail Using this Transport + * Send a mail using smtp transport * * @return void * @throws \Magento\Framework\Exception\MailException */ public function sendMessage() { - $error = null; - try { - parent::send($this->_message); - } - catch (\Exception $e) { + $error = null; + $message = ZendMessage::fromString( + $this->_message->getRawMessage() + ); + + try { + $this->_smtpTransport->send($message); + } catch (\Exception $e) { $error = new Phrase($e->getMessage()); - throw new MailException($error, $e); - } + throw new MailException($error, $e); + } finally { - $this->_logManager->add($this->_message, $error); + $this->_logManager->add($message, $error); } } /** - * Test the Smtp Connection Protocol + * Test the smtp connection protocol * * @return bool */ public function testConnection() { $result = false; - if (!($this->_connection instanceof Zend_Mail_Protocol_Smtp)) { - // Check if authentication is required and determine required class - $connectionClass = 'Zend_Mail_Protocol_Smtp'; - if ($this->_auth) { - $connectionClass .= '_Auth_' . ucwords($this->_auth); - } - if (!class_exists($connectionClass)) { - #require_once 'Zend/Loader.php'; - Zend_Loader::loadClass($connectionClass); - } - $this->setConnection( - new $connectionClass( - $this->_host, - $this->_port, - $this->_config - ) - ); - $this->_connection->connect(); - $this->_connection->helo($this->_name); - $result = true; - } - // Reset connection transaction - $this->_connection->rset(); - + $option = $this->_smtpTransport->getOptions(); + $connection = $this->_smtpTransport->getConnection(); + if (!($connection instanceof SmtpProtocol) || !$connection->hasSession()) { + $config = $option->getConnectionConfig(); + $config['host'] = $option->getHost(); + $config['port'] = $option->getPort(); + $connection = $this->_smtpTransport->plugin($option->getConnectionClass(), $config); + } + + $connection->connect(); + $connection->helo($option->getName()); + $result = true; + // Reset connection to ensure reliable transaction + $connection->rset(); + return $result; } /** - * Retrieve Message + * Retrieve message * * @return \Magento\Framework\Mail\MessageInterface */ public function getMessage() { return $this->_message; - } + } }