Skip to content

Commit

Permalink
Merge pull request #3 from mageplaza/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
imsamthomas authored Aug 22, 2017
2 parents bcffb37 + a527918 commit 7d61914
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 69 deletions.
153 changes: 153 additions & 0 deletions Application/Resource/Mail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_Smtp
* @copyright Copyright (c) 2017 Mageplaza (https://www.mageplaza.com/)
* @license http://mageplaza.com/LICENSE.txt
*/

namespace Mageplaza\Smtp\Application\Resource;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Encryption\EncryptorInterface;
use Magento\Framework\Registry;
use Mageplaza\Smtp\Helper\Data;

/**
* Class Mail
* @package Mageplaza\Smtp\Application\Resource
*/
class Mail extends \Zend_Application_Resource_Mail
{
const CONFIGURATION_GROUP_SMTP = 'configuration_option';
const DEVELOPER_GROUP_SMTP = 'developer';
const GENERAL_GROUP_SMTP = 'general';

/**
* @var \Mageplaza\Smtp\Helper\Data
*/
protected $smtpHelper;

/**
* @var \Magento\Framework\Registry
*/
protected $registry;

/**
* @var boolean is module enable
*/
protected $_moduleEnable;

/**
* @var boolean is developer mode
*/
protected $_developerMode;

/**
* @var boolean is enable email log
*/
protected $_emailLog;

/**
* Mail constructor.
* @param null $options
*/
public function __construct($options = null)
{
$options = ['type' => '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;
}
}
87 changes: 19 additions & 68 deletions Plugin/Magento/Framework/Mail/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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) {
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "[email protected]",
Expand Down

0 comments on commit 7d61914

Please sign in to comment.