Skip to content

Commit

Permalink
replace zend framework
Browse files Browse the repository at this point in the history
  • Loading branch information
karliuka committed Dec 2, 2018
1 parent 6196d13 commit 5c60d78
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 75 deletions.
12 changes: 7 additions & 5 deletions Controller/Adminhtml/Connection/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
]
];

Expand Down
12 changes: 7 additions & 5 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
44 changes: 30 additions & 14 deletions Model/LogManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}
Expand Down
98 changes: 47 additions & 51 deletions Model/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;
}
}
}

0 comments on commit 5c60d78

Please sign in to comment.