From c0a4f02a6004062bf604ba819e8e6f3d52984cab Mon Sep 17 00:00:00 2001 From: haitv282 Date: Tue, 22 Aug 2017 00:06:59 +0700 Subject: [PATCH 1/2] Use transport source --- Application/Resource/Mail.php | 153 ++++++++++++++++++++ Plugin/Magento/Framework/Mail/Transport.php | 87 +++-------- 2 files changed, 172 insertions(+), 68 deletions(-) create mode 100644 Application/Resource/Mail.php diff --git a/Application/Resource/Mail.php b/Application/Resource/Mail.php new file mode 100644 index 0000000..17005c4 --- /dev/null +++ b/Application/Resource/Mail.php @@ -0,0 +1,153 @@ + 'smtp']; + + $smtpHelper = ObjectManager::getInstance()->get(Data::class); + if ($host = $smtpHelper->getConfig(self::CONFIGURATION_GROUP_SMTP, 'host')) { + $options['host'] = $host; + } + + if ($protocol = $smtpHelper->getConfig(self::CONFIGURATION_GROUP_SMTP, 'protocol')) { + $options['ssl'] = $protocol; + } + + if ($port = $smtpHelper->getConfig(self::CONFIGURATION_GROUP_SMTP, 'port')) { + $options['port'] = $port; + } + + $options['auth'] = $smtpHelper->getConfig(self::CONFIGURATION_GROUP_SMTP, 'authentication'); + $options['username'] = $smtpHelper->getConfig(self::CONFIGURATION_GROUP_SMTP, 'username'); + + $encryptor = ObjectManager::getInstance()->get(EncryptorInterface::class); + $options['password'] = $encryptor->decrypt($smtpHelper->getConfig(self::CONFIGURATION_GROUP_SMTP, 'password')); + + $this->smtpHelper = $smtpHelper; + $this->registry = ObjectManager::getInstance()->get(Registry::class); + + parent::__construct(['transport' => $options]); + } + + /** + * @return \Magento\Framework\Mail\Message + */ + public function getMessage() + { + $message = $this->registry->registry('mageplaza_smtp_message'); + if ($returnPath = $this->smtpHelper->getConfig(self::CONFIGURATION_GROUP_SMTP, 'return_path_email')) { + $message->setReturnPath($returnPath); + } + + $headers = $message->getHeaders(); + $senderName = strip_tags($headers['From'][0], $message->getFrom()); + $userName = $this->smtpHelper->getConfig(self::CONFIGURATION_GROUP_SMTP, 'username'); + if ($userName && $senderName) { + $message->clearFrom(); + $message->setFrom($userName, $senderName); + } + + return $message; + } + + /** + * @return bool|mixed + */ + public function isModuleEnable() + { + if (is_null($this->_moduleEnable)) { + $this->_moduleEnable = $this->smtpHelper->getConfig(self::GENERAL_GROUP_SMTP, 'enabled'); + } + + return $this->_moduleEnable; + } + + /** + * @return bool|mixed + */ + public function isDeveloperMode() + { + if (is_null($this->_developerMode)) { + $this->_developerMode = $this->smtpHelper->getConfig(self::DEVELOPER_GROUP_SMTP, 'developer_mode'); + } + + return $this->_developerMode; + } + + /** + * @return bool|mixed + */ + public function isEnableEmailLog() + { + if (is_null($this->_emailLog)) { + $this->_emailLog = $this->smtpHelper->getConfig(self::DEVELOPER_GROUP_SMTP, 'log_email'); + } + + return $this->_emailLog; + } +} \ No newline at end of file diff --git a/Plugin/Magento/Framework/Mail/Transport.php b/Plugin/Magento/Framework/Mail/Transport.php index 7ac0154..0ac11ae 100644 --- a/Plugin/Magento/Framework/Mail/Transport.php +++ b/Plugin/Magento/Framework/Mail/Transport.php @@ -21,55 +21,37 @@ namespace Mageplaza\Smtp\Plugin\Magento\Framework\Mail; +use Mageplaza\Smtp\Application\Resource\Mail; +use Mageplaza\Smtp\Model\LogFactory; + /** * Class Transport * @package Mageplaza\Smtp\Plugin\Magento\Framework\Mail */ -class Transport extends \Zend_Mail_Transport_Smtp +class Transport { - const CONFIGURATION_GROUP_SMTP = 'configuration_option'; - const DEVELOPER_GROUP_SMTP = 'developer'; - const GENERAL_GROUP_SMTP = 'general'; - - /** - * @var \Mageplaza\Smtp\Helper\Data - */ - private $smtpDataHelper; - /** * @var \Mageplaza\Smtp\Model\LogFactory */ private $logFactory; /** - * @var \Magento\Framework\Registry + * @var \Mageplaza\Smtp\Application\Resource\Mail */ - private $registry; + private $resourceMail; /** - * @var \Magento\Framework\Encryption\EncryptorInterface - */ - private $encryptor; - - /** - * Constructor - * - * @param \Magento\Framework\Registry $registry - * @param \Magento\Framework\Encryption\EncryptorInterface $encryptor + * Transport constructor. + * @param \Mageplaza\Smtp\Application\Resource\Mail $resourceMail * @param \Mageplaza\Smtp\Model\LogFactory $logFactory - * @param \Mageplaza\Smtp\Helper\Data $smtpDataHelper */ public function __construct( - \Magento\Framework\Registry $registry, - \Magento\Framework\Encryption\EncryptorInterface $encryptor, - \Mageplaza\Smtp\Model\LogFactory $logFactory, - \Mageplaza\Smtp\Helper\Data $smtpDataHelper + Mail $resourceMail, + LogFactory $logFactory ) { - $this->registry = $registry; - $this->smtpDataHelper = $smtpDataHelper; - $this->logFactory = $logFactory; - $this->encryptor = $encryptor; + $this->resourceMail = $resourceMail; + $this->logFactory = $logFactory; } /** @@ -79,44 +61,12 @@ public function __construct( */ public function aroundSendMessage(\Magento\Framework\Mail\TransportInterface $subject, \Closure $proceed) { - $config = []; - if ($this->smtpDataHelper->getConfig(self::GENERAL_GROUP_SMTP, 'enabled')) { - $message = $this->registry->registry('mageplaza_smtp_message'); - if ($host = $this->smtpDataHelper->getConfig(self::CONFIGURATION_GROUP_SMTP, 'host')) { - $this->_host = $host; - } - if ($returnPath = $this->smtpDataHelper->getConfig(self::CONFIGURATION_GROUP_SMTP, 'return_path_email')) { - $message->setReturnPath($returnPath); - } - if ($protocol = $this->smtpDataHelper->getConfig(self::CONFIGURATION_GROUP_SMTP, 'protocol')) { - $config['ssl'] = $protocol; - } - - $port = $this->smtpDataHelper->getConfig(self::CONFIGURATION_GROUP_SMTP, 'port'); - if ($port) { - $config['port'] = $port; - $this->_port = $port; - } - - $auth = $this->smtpDataHelper->getConfig(self::CONFIGURATION_GROUP_SMTP, 'authentication'); - $this->_auth = $auth; - - $config['auth'] = $auth; - $config['username'] = $this->smtpDataHelper->getConfig(self::CONFIGURATION_GROUP_SMTP, 'username'); - $config['password'] = $this->encryptor->decrypt($this->smtpDataHelper->getConfig(self::CONFIGURATION_GROUP_SMTP, 'password')); - - $headers = $message->getHeaders(); - $senderName = strip_tags($headers['From'][0], $message->getFrom()); - if ($config['username'] && $senderName) { - $message->clearFrom(); - $message->setFrom($config['username'], $senderName); - } - if (!empty($config)) { - $this->_config = $config; - } + if ($this->resourceMail->isModuleEnable()) { + $message = $this->resourceMail->getMessage(); + $transport = $this->resourceMail->init(); try { - if (!$this->smtpDataHelper->getConfig(self::DEVELOPER_GROUP_SMTP, 'developer_mode')) { - parent::send($message); + if (!$this->resourceMail->isDeveloperMode()) { + $transport->send($message); } $this->emailLog($message); } catch (\Exception $e) { @@ -137,7 +87,8 @@ public function aroundSendMessage(\Magento\Framework\Mail\TransportInterface $su */ private function emailLog($message, $status = true) { - if ($this->smtpDataHelper->getConfig(self::DEVELOPER_GROUP_SMTP, 'log_email')) { + if ($this->resourceMail->isEnableEmailLog()) { + /** @var \Mageplaza\Smtp\Model\Log $log */ $log = $this->logFactory->create(); try { $log->saveLog($message, $status); From a527918c16e18ee763fad16bdd727b5e85f16913 Mon Sep 17 00:00:00 2001 From: Sam Thomas Date: Tue, 22 Aug 2017 09:11:31 +0700 Subject: [PATCH 2/2] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 3b12b2f..698e564 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "SMTP Extension for Magento 2 helps the owner of store simply install SMTP (Simple Mail Transfer Protocol) server which transmits the messages into codes or numbers", "license": "Mageplaza License", "type": "magento2-module", - "version": "1.0.1", + "version": "1.0.2", "authors": [ { "email": "hi@mageplaza.com",