-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom SMTP for Magento 2.0.0 - 2.4.3
- Loading branch information
Showing
9 changed files
with
322 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
<?php | ||
/** | ||
* Copyright © MagePal LLC. All rights reserved. | ||
* See COPYING.txt for license details. | ||
* http://www.magepal.com | [email protected] | ||
*/ | ||
|
||
namespace MagePal\CustomSmtp\Mail\ZF1; | ||
|
||
use Exception; | ||
use Magento\Framework\Exception\MailException; | ||
use Magento\Framework\Mail\MessageInterface; | ||
use Magento\Framework\Phrase; | ||
use MagePal\CustomSmtp\Helper\Data; | ||
use MagePal\CustomSmtp\Model\Store; | ||
use Zend_Mail; | ||
use Zend_Mail_Exception; | ||
use Zend_Mail_Transport_Smtp; | ||
|
||
/** | ||
* Class Smtp | ||
* For Magento <= 2.2.7 | ||
*/ | ||
|
||
class Smtp extends Zend_Mail_Transport_Smtp | ||
{ | ||
/** | ||
* @var Data | ||
*/ | ||
protected $dataHelper; | ||
|
||
/** | ||
* @var Store | ||
*/ | ||
protected $storeModel; | ||
|
||
/** | ||
* @param Data $dataHelper | ||
* @param Store $storeModel | ||
*/ | ||
public function __construct( | ||
Data $dataHelper, | ||
Store $storeModel | ||
) { | ||
$this->dataHelper = $dataHelper; | ||
$this->storeModel = $storeModel; | ||
} | ||
|
||
/** | ||
* @param Data $dataHelper | ||
* @return Smtp | ||
*/ | ||
public function setDataHelper(Data $dataHelper) | ||
{ | ||
$this->dataHelper = $dataHelper; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param Store $storeModel | ||
* @return Smtp | ||
*/ | ||
public function setStoreModel(Store $storeModel) | ||
{ | ||
$this->storeModel = $storeModel; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param MessageInterface $message | ||
* @throws MailException | ||
* @throws Zend_Mail_Exception | ||
*/ | ||
public function sendSmtpMessage( | ||
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 | ||
switch ($dataHelper->getConfigSetReturnPath()) { | ||
case 1: | ||
$returnPathEmail = $message->getFrom() ?: $this->getFromEmailAddress(); | ||
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); | ||
} | ||
|
||
//Set from address | ||
switch ($dataHelper->getConfigSetFrom()) { | ||
case 1: | ||
$setFromEmail = $message->getFrom() ?: $this->getFromEmailAddress(); | ||
break; | ||
case 2: | ||
$setFromEmail = $dataHelper->getConfigCustomFromEmail(); | ||
break; | ||
default: | ||
$setFromEmail = null; | ||
break; | ||
} | ||
if ($setFromEmail !== null && $dataHelper->getConfigSetFrom()) { | ||
$message->clearFrom(); | ||
$message->setFrom($setFromEmail); | ||
} | ||
|
||
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 MailException( | ||
new Phrase($e->getMessage()), | ||
$e | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getFromEmailAddress() | ||
{ | ||
$result = $this->storeModel->getFrom(); | ||
return $result['email']; | ||
} | ||
|
||
/** | ||
* @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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,24 +5,23 @@ | |
* http://www.magepal.com | [email protected] | ||
*/ | ||
|
||
namespace MagePal\CustomSmtp\Mail; | ||
namespace MagePal\CustomSmtp\Mail\ZF2; | ||
|
||
use Exception; | ||
use Laminas\Mail\AddressList; | ||
use Laminas\Mail\Header\HeaderInterface; | ||
use Laminas\Mail\Message; | ||
use Laminas\Mail\Transport\Smtp as SmtpTransport; | ||
use Laminas\Mail\Transport\SmtpOptions; | ||
use Laminas\Mime\Mime; | ||
use Magento\Framework\Exception\MailException; | ||
use Magento\Framework\Mail\EmailMessageInterface; | ||
use Magento\Framework\Mail\MessageInterface; | ||
use Magento\Framework\Phrase; | ||
use MagePal\CustomSmtp\Helper\Data; | ||
use MagePal\CustomSmtp\Model\Store; | ||
use Zend\Mail\AddressList; | ||
use Zend\Mail\Message; | ||
use Zend\Mail\Transport\Smtp as SmtpTransport; | ||
use Zend\Mail\Transport\SmtpOptions; | ||
|
||
/** | ||
* Class Smtp | ||
* For Magento >= 2.2.8 | ||
*/ | ||
class Smtp | ||
{ | ||
|
@@ -97,9 +96,7 @@ protected function convertMessage($message) | |
} | ||
|
||
if (!$zendMessage instanceof Message) { | ||
throw new MailException( | ||
__('Not instance of Message') | ||
); | ||
throw new MailException('Not instance of Message'); | ||
} | ||
} catch (Exception $e) { | ||
$zendMessage = Message::fromString($message->getRawMessage()); | ||
|
@@ -128,7 +125,7 @@ public function sendSmtpMessage( | |
|
||
foreach ($message->getHeaders()->toArray() as $headerKey => $headerValue) { | ||
$mailHeader = $message->getHeaders()->get($headerKey); | ||
if ($mailHeader instanceof HeaderInterface) { | ||
if ($mailHeader instanceof \Zend\Mail\Header\HeaderInterface) { | ||
$this->updateMailHeader($mailHeader); | ||
} elseif ($mailHeader instanceof \ArrayIterator) { | ||
foreach ($mailHeader as $header) { | ||
|
@@ -150,7 +147,6 @@ public function sendSmtpMessage( | |
} | ||
|
||
/** | ||
* | ||
* @param Message $message | ||
*/ | ||
protected function setSender($message) | ||
|
@@ -263,8 +259,8 @@ protected function getSmtpOptions() | |
*/ | ||
public function updateMailHeader($header) | ||
{ | ||
if ($header instanceof HeaderInterface) { | ||
if (Mime::isPrintable($header->getFieldValue())) { | ||
if ($header instanceof \Zend\Mail\Header\HeaderInterface) { | ||
if (\Zend\Mime\Mime::isPrintable($header->getFieldValue())) { | ||
$header->setEncoding('ASCII'); | ||
} else { | ||
$header->setEncoding('utf-8'); | ||
|
Oops, something went wrong.