Skip to content

Commit

Permalink
Issue #125 - Add compatible with Magento 2.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
srenon committed Dec 4, 2018
1 parent 5f6fdb0 commit dc1ddbf
Show file tree
Hide file tree
Showing 11 changed files with 406 additions and 96 deletions.
1 change: 1 addition & 0 deletions Block/Adminhtml/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ public function verify()
* @return array
* @throws \Zend_Mail_Exception
* @throws \Zend_Validate_Exception
* Todo: update to new Zend Framework SMTP
*/
protected function validateServerEmailSetting()
{
Expand Down
1 change: 0 additions & 1 deletion Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
/** @var array $testConfig */
protected $testConfig = [];


/**
* @param null $key
* @return array|mixed|string
Expand Down
22 changes: 21 additions & 1 deletion Model/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ class Store
/** @var int/null */
protected $store_id = null;

protected $from = null;

/**
* @return null
* @return int|null
*/
public function getStoreId()
{
Expand All @@ -29,4 +31,22 @@ public function setStoreId($store_id)
$this->store_id = $store_id;
return $this;
}

/**
* @return string|array
*/
public function getFrom()
{
return $this->from;
}

/**
* @param string|array $from
* @return $this
*/
public function setFrom($from)
{
$this->from = $from;
return $this;
}
}
153 changes: 153 additions & 0 deletions Model/TwoDotThree/Smtp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php
/**
* Copyright © MagePal LLC. All rights reserved.
* See COPYING.txt for license details.
* http://www.magepal.com | support@magepal.com
*/

namespace MagePal\GmailSmtpApp\Model\TwoDotThree;

use Zend\Mail\Message;
use Zend\Mail\Transport\Smtp as SmtpTransport;
use Zend\Mail\Transport\SmtpOptions;

/**
* Class Smtp
* For Magento 2.3.x
* @package MagePal\GmailSmtpApp\Model\TwoDotTwo
*/

class Smtp
{
/**
* @var \MagePal\GmailSmtpApp\Helper\Data
*/
protected $dataHelper;

/**
* @var \MagePal\GmailSmtpApp\Model\Store
*/
protected $storeModel;

/**
* @param \MagePal\GmailSmtpApp\Helper\Data $dataHelper
* @param \MagePal\GmailSmtpApp\Model\Store $storeModel
*/
public function __construct(
\MagePal\GmailSmtpApp\Helper\Data $dataHelper,
\MagePal\GmailSmtpApp\Model\Store $storeModel
) {
$this->dataHelper = $dataHelper;
$this->storeModel = $storeModel;
}

/**
* @param \MagePal\GmailSmtpApp\Helper\Data $dataHelper
* @return Smtp
*/
public function setDataHelper(\MagePal\GmailSmtpApp\Helper\Data $dataHelper)
{
$this->dataHelper = $dataHelper;
return $this;
}

/**
* @param \MagePal\GmailSmtpApp\Model\Store $storeModel
* @return Smtp
*/
public function setStoreModel(\MagePal\GmailSmtpApp\Model\Store $storeModel)
{
$this->storeModel = $storeModel;
return $this;
}

/**
* @param \Magento\Framework\Mail\MessageInterface $message
* @throws \Magento\Framework\Exception\MailException
*/
public function sendSmtpMessage(
\Magento\Framework\Mail\MessageInterface $message
) {
$dataHelper = $this->dataHelper;
$dataHelper->setStoreId($this->storeModel->getStoreId());

$message = Message::fromString($message->getRawMessage());

//Set reply-to path
$setReturnPath = $dataHelper->getConfigSetReturnPath();
switch ($setReturnPath) {
case 1:
$returnPathEmail = $message->getFrom();
break;
case 2:
$returnPathEmail = $dataHelper->getConfigReturnPathEmail();
break;
default:
$returnPathEmail = null;
break;
}

if ($returnPathEmail !== null && $dataHelper->getConfigSetReturnPath()) {
foreach ($returnPathEmail as $address) {
$message->setSender($address);
}
}

if ($message->getReplyTo() === null && $dataHelper->getConfigSetReplyTo()) {
foreach ($returnPathEmail as $address) {
$message->setReplyTo($address);
}
}

if ($returnPathEmail !== null && $dataHelper->getConfigSetFrom()) {
foreach ($returnPathEmail as $address) {
$message->clearFrom();
$message->setFrom($address);
}
}

if (!$message->getFrom()->count()) {
$result = $this->storeModel->getFrom();
$message->setFrom($result['email'], $result['name']);
}

//set config
$options = new SmtpOptions([
'name' => $dataHelper->getConfigName(),
'host' => $dataHelper->getConfigSmtpHost(),
'port' => $dataHelper->getConfigSmtpPort(),
]);

$connectionConfig = [];

$auth = strtolower($dataHelper->getConfigAuth());
if ($auth != 'none') {
$options->setConnectionClass($auth);

$connectionConfig = [
'username' => $dataHelper->getConfigUsername(),
'password' => $dataHelper->getConfigPassword()
];
}

$ssl = $dataHelper->getConfigSsl();
if ($ssl != 'none') {
$connectionConfig['ssl'] = $ssl;
}

if (!empty($connectionConfig)) {
$options->setConnectionConfig($connectionConfig);
}

try {
$transport = new SmtpTransport();
$transport->setOptions($options);
$transport->send($message);
} catch (\Exception $e) {
throw new \Magento\Framework\Exception\MailException(
new \Magento\Framework\Phrase($e->getMessage()),
$e
);
}
}
}
159 changes: 159 additions & 0 deletions Model/TwoDotTwo/Smtp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php
/**
* Copyright © MagePal LLC. All rights reserved.
* See COPYING.txt for license details.
* http://www.magepal.com | support@magepal.com
*/

namespace MagePal\GmailSmtpApp\Model\TwoDotTwo;

/**
* Class Smtp
* For Magento 2.0.x, 2.1.x, 2.2.x
* @package MagePal\GmailSmtpApp\Model\TwoDotTwo
*/

class Smtp extends \Zend_Mail_Transport_Smtp
{
/**
* @var \MagePal\GmailSmtpApp\Helper\Data
*/
protected $dataHelper;

/**
* @var \MagePal\GmailSmtpApp\Model\Store
*/
protected $storeModel;

/**
* @param \MagePal\GmailSmtpApp\Helper\Data $dataHelper
* @param \MagePal\GmailSmtpApp\Model\Store $storeModel
*/
public function __construct(
\MagePal\GmailSmtpApp\Helper\Data $dataHelper,
\MagePal\GmailSmtpApp\Model\Store $storeModel
) {
$this->dataHelper = $dataHelper;
$this->storeModel = $storeModel;
}

/**
* @param \MagePal\GmailSmtpApp\Helper\Data $dataHelper
* @return Smtp
*/
public function setDataHelper(\MagePal\GmailSmtpApp\Helper\Data $dataHelper)
{
$this->dataHelper = $dataHelper;
return $this;
}

/**
* @param \MagePal\GmailSmtpApp\Model\Store $storeModel
* @return Smtp
*/
public function setStoreModel(\MagePal\GmailSmtpApp\Model\Store $storeModel)
{
$this->storeModel = $storeModel;
return $this;
}

/**
* @param \Magento\Framework\Mail\MessageInterface $message
* @throws \Magento\Framework\Exception\MailException
* @throws \Zend_Mail_Exception
*/
public function sendSmtpMessage(
\Magento\Framework\Mail\MessageInterface $message
) {
$dataHelper = $this->dataHelper;
$dataHelper->setStoreId($this->storeModel->getStoreId());

if ($message instanceof \Zend_mail) {
if ($message->getDate() === null) {
$message->setDate();
}
}

//Set reply-to path
$setReturnPath = $dataHelper->getConfigSetReturnPath();
switch ($setReturnPath) {
case 1:
$returnPathEmail = $message->getFrom();
break;
case 2:
$returnPathEmail = $dataHelper->getConfigReturnPathEmail();
break;
default:
$returnPathEmail = null;
break;
}

if ($returnPathEmail !== null && $dataHelper->getConfigSetReturnPath()) {
$message->setReturnPath($returnPathEmail);
}

if ($message->getReplyTo() === null && $dataHelper->getConfigSetReplyTo()) {
$message->setReplyTo($returnPathEmail);
}

if ($returnPathEmail !== null && $dataHelper->getConfigSetFrom()) {
$message->clearFrom();
$message->setFrom($returnPathEmail);
}

if (!$message->getFrom()) {
$result = $this->storeModel->getFrom();
$message->setFrom($result['email'], $result['name']);
}

//set config
$smtpConf = [
'name' => $dataHelper->getConfigName(),
'port' => $dataHelper->getConfigSmtpPort(),
];

$auth = strtolower($dataHelper->getConfigAuth());
if ($auth != 'none') {
$smtpConf['auth'] = $auth;
$smtpConf['username'] = $dataHelper->getConfigUsername();
$smtpConf['password'] = $dataHelper->getConfigPassword();
}

$ssl = $dataHelper->getConfigSsl();
if ($ssl != 'none') {
$smtpConf['ssl'] = $ssl;
}

$smtpHost = $dataHelper->getConfigSmtpHost();
$this->initialize($smtpHost, $smtpConf);

try {
parent::send($message);
} catch (\Exception $e) {
throw new \Magento\Framework\Exception\MailException(
new \Magento\Framework\Phrase($e->getMessage()),
$e
);
}
}

/**
* @param string $host
* @param array $config
*/
public function initialize($host = '127.0.0.1', array $config = [])
{
if (isset($config['name'])) {
$this->_name = $config['name'];
}
if (isset($config['port'])) {
$this->_port = $config['port'];
}
if (isset($config['auth'])) {
$this->_auth = $config['auth'];
}

$this->_host = $host;
$this->_config = $config;
}
}
Loading

0 comments on commit dc1ddbf

Please sign in to comment.