From 057b1abec1931e37b3e39d1cb3bb9dd2b5a64ebc Mon Sep 17 00:00:00 2001 From: Renon Stewart Date: Wed, 24 Apr 2024 11:30:37 -0400 Subject: [PATCH] Custom SMTP for Magento 2.4.3+ --- Block/Adminhtml/ValidateConfig.php | 71 +++++---- Mail/{ZF2 => }/Smtp.php | 24 +-- Mail/ZF1/Smtp.php | 188 ----------------------- Model/Transport.php | 62 -------- Plugin/Mail/TransportPlugin.php | 27 ++-- composer.json | 10 +- etc/adminhtml/system.xml | 4 +- etc/di.xml | 7 - view/adminhtml/web/js/validate-config.js | 4 +- 9 files changed, 76 insertions(+), 321 deletions(-) rename Mail/{ZF2 => }/Smtp.php (93%) delete mode 100644 Mail/ZF1/Smtp.php delete mode 100755 Model/Transport.php diff --git a/Block/Adminhtml/ValidateConfig.php b/Block/Adminhtml/ValidateConfig.php index e239df1..49d08da 100755 --- a/Block/Adminhtml/ValidateConfig.php +++ b/Block/Adminhtml/ValidateConfig.php @@ -8,15 +8,16 @@ namespace MagePal\CustomSmtp\Block\Adminhtml; use Exception; +use Laminas\Mime\Message as MineMessage; +use Laminas\Mime\Part as MinePart; use Magento\Backend\Block\Template; use Magento\Backend\Block\Template\Context; use Magento\Framework\Validator\EmailAddress; use MagePal\CustomSmtp\Helper\Data; use MagePal\CustomSmtp\Model\Email; -use Zend_Mail; -use Zend_Mail_Exception; -use Zend_Mail_Transport_Smtp; -use Zend_Validate_Exception; +use Laminas\Mail\Message; +use Laminas\Mail\Transport\Smtp; +use Laminas\Mail\Transport\SmtpOptions; class ValidateConfig extends Template { @@ -195,7 +196,7 @@ protected function init() $this->toAddress = $this->getConfig('email') ? $this->getConfig('email') : $this->getConfig('username'); - $this->fromAddress = trim($this->getConfig('from_email')); + $this->fromAddress = trim((string) $this->getConfig('from_email')); if (!$this->emailAddressValidator->isValid($this->fromAddress)) { $this->fromAddress = $this->toAddress; @@ -245,7 +246,7 @@ public function verify() /** * Todo: update to new Zend Framework SMTP * @return array - * @throws Zend_Mail_Exception + * @throws \Exception * @throws \Magento\Framework\Exception\NoSuchEntityException */ protected function validateServerEmailSetting() @@ -254,7 +255,6 @@ protected function validateServerEmailSetting() $username = $this->getConfig('username'); $password = $this->getConfig('password'); - $auth = strtolower($this->getConfig('auth')); //if default view @@ -268,30 +268,31 @@ protected function validateServerEmailSetting() } } - $transport = $this->getMailTransportSmtp(); - - $from = trim($this->getConfig('from_email')); + $name = 'Test from MagePal SMTP'; + $from = trim((string) $this->getConfig('from_email')); $from = filter_var($from, FILTER_VALIDATE_EMAIL) ? $from : $username; $this->fromAddress = filter_var($username, FILTER_VALIDATE_EMAIL) ? $username : $from; + $htmlBody = $this->_email->setTemplateVars(['hash' => $this->hash])->getEmailBody(); - //Create email - $name = 'Test from MagePal SMTP'; - $mail = new Zend_Mail(); - $mail->setFrom($this->fromAddress, $name); - $mail->addTo($this->toAddress, 'MagePal SMTP'); - $mail->setSubject('Hello from MagePal SMTP (1 of 2)'); + $transport = $this->getMailTransportSmtp(); - $htmlBody = $this->_email->setTemplateVars(['hash' => $this->hash])->getEmailBody(); + $bodyMessage = new MinePart($htmlBody); + $bodyMessage->type = 'text/html'; + + $body = new MineMessage(); + $body->addPart($bodyMessage); - $mail->setBodyHtml($htmlBody); + $message = new Message(); + $message->addTo($this->toAddress, 'MagePal SMTP') + ->addFrom($this->fromAddress, $name) + ->setSubject('Hello from MagePal SMTP (1 of 2)') + ->setBody($body) + ->setEncoding('UTF-8'); $result = $this->error(); try { - //only way to prevent zend from giving an error - if (!$mail->send($transport) instanceof Zend_Mail) { - $result = $this->error(true, __('Invalid class, not instance of Zend Mail')); - } + $transport->send($message); } catch (Exception $e) { $result = $this->error(true, __($e->getMessage())); } @@ -303,29 +304,35 @@ public function getMailTransportSmtp() { $username = $this->getConfig('username'); $password = $this->getConfig('password'); - $auth = strtolower($this->getConfig('auth')); - //SMTP server configuration - $smtpHost = $this->getConfig('smtphost'); - - $smtpConf = [ + $optionsArray = [ 'name' => $this->getConfig('name'), + 'host' => $this->getConfig('smtphost'), 'port' => $this->getConfig('smtpport') ]; if ($auth != 'none') { - $smtpConf['auth'] = $auth; - $smtpConf['username'] = $username; - $smtpConf['password'] = $password; + $optionsArray['connection_class'] = $auth; + $optionsArray['connection_config'] = [ + 'username' => $username, + 'password' => $password, + ]; } $ssl = $this->getConfig('ssl'); if ($ssl != 'none') { - $smtpConf['ssl'] = $ssl; + $optionsArray = array_merge_recursive( + ['connection_config' => ['ssl' => $ssl]], + $optionsArray + ); } - return new Zend_Mail_Transport_Smtp($smtpHost, $smtpConf); + $options = new SmtpOptions($optionsArray); + $transport = new Smtp(); + $transport->setOptions($options); + + return $transport; } /** diff --git a/Mail/ZF2/Smtp.php b/Mail/Smtp.php similarity index 93% rename from Mail/ZF2/Smtp.php rename to Mail/Smtp.php index 8011bfb..481554f 100644 --- a/Mail/ZF2/Smtp.php +++ b/Mail/Smtp.php @@ -5,23 +5,24 @@ * http://www.magepal.com | support@magepal.com */ -namespace MagePal\CustomSmtp\Mail\ZF2; +namespace MagePal\CustomSmtp\Mail; 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 { @@ -96,7 +97,9 @@ 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()); @@ -125,7 +128,7 @@ public function sendSmtpMessage( foreach ($message->getHeaders()->toArray() as $headerKey => $headerValue) { $mailHeader = $message->getHeaders()->get($headerKey); - if ($mailHeader instanceof \Zend\Mail\Header\HeaderInterface) { + if ($mailHeader instanceof HeaderInterface) { $this->updateMailHeader($mailHeader); } elseif ($mailHeader instanceof \ArrayIterator) { foreach ($mailHeader as $header) { @@ -147,6 +150,7 @@ public function sendSmtpMessage( } /** + * * @param Message $message */ protected function setSender($message) @@ -259,8 +263,8 @@ protected function getSmtpOptions() */ public function updateMailHeader($header) { - if ($header instanceof \Zend\Mail\Header\HeaderInterface) { - if (\Zend\Mime\Mime::isPrintable($header->getFieldValue())) { + if ($header instanceof HeaderInterface) { + if (Mime::isPrintable($header->getFieldValue())) { $header->setEncoding('ASCII'); } else { $header->setEncoding('utf-8'); diff --git a/Mail/ZF1/Smtp.php b/Mail/ZF1/Smtp.php deleted file mode 100644 index dc8e84d..0000000 --- a/Mail/ZF1/Smtp.php +++ /dev/null @@ -1,188 +0,0 @@ -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; - } -} diff --git a/Model/Transport.php b/Model/Transport.php deleted file mode 100755 index 9bb003b..0000000 --- a/Model/Transport.php +++ /dev/null @@ -1,62 +0,0 @@ -_message = $message; - } - - /** - * Send a mail using this transport - * - * @return void - * @throws MailException - */ - public function sendMessage() - { - try { - parent::send($this->_message); - } catch (Exception $e) { - throw new MailException(new Phrase($e->getMessage()), $e); - } - } - - /** - * @return MessageInterface|Zend_Mail - */ - public function getMessage() - { - return $this->_message; - } -} diff --git a/Plugin/Mail/TransportPlugin.php b/Plugin/Mail/TransportPlugin.php index 0f70887..a50c2da 100644 --- a/Plugin/Mail/TransportPlugin.php +++ b/Plugin/Mail/TransportPlugin.php @@ -13,11 +13,9 @@ use Magento\Framework\Mail\Message; use Magento\Framework\Mail\TransportInterface; use MagePal\CustomSmtp\Helper\Data; -use MagePal\CustomSmtp\Mail\ZF1\Smtp as ZF1Smtp; -use MagePal\CustomSmtp\Mail\ZF2\Smtp as ZF2Smtp; +use MagePal\CustomSmtp\Mail\SmtpFactory; +use MagePal\CustomSmtp\Mail\Smtp; use MagePal\CustomSmtp\Model\Store; -use Zend_Mail; -use Zend_Mail_Exception; class TransportPlugin { @@ -30,6 +28,10 @@ class TransportPlugin * @var Store */ protected $storeModel; + /** + * @var Smtp + */ + private SmtpFactory $smtpFactory; /** * @param Data $dataHelper @@ -37,17 +39,18 @@ class TransportPlugin */ public function __construct( Data $dataHelper, - Store $storeModel + Store $storeModel, + SmtpFactory $smtpFactory ) { $this->dataHelper = $dataHelper; $this->storeModel = $storeModel; + $this->smtpFactory = $smtpFactory; } /** * @param TransportInterface $subject * @param Closure $proceed * @throws MailException - * @throws Zend_Mail_Exception */ public function aroundSendMessage( TransportInterface $subject, @@ -60,13 +63,11 @@ public function aroundSendMessage( $message = $subject->getMessage(); - //ZendMail1 - Magento <= 2.2.7 - //ZendMail2 - Magento >= 2.2.8 - if ($message instanceof Zend_Mail) { - $smtp = new ZF1Smtp($this->dataHelper, $this->storeModel); - $smtp->sendSmtpMessage($message); - } elseif ($message instanceof Message || $message instanceof EmailMessageInterface) { - $smtp = new ZF2Smtp($this->dataHelper, $this->storeModel); + if ($message instanceof Message || $message instanceof EmailMessageInterface) { + /** @var Smtp $smtp */ + $smtp = $this->smtpFactory->create( + ['dataHelper' => $this->dataHelper, 'storeModel' => $this->storeModel] + ); $smtp->sendSmtpMessage($message); } else { $proceed(); diff --git a/composer.json b/composer.json index bd19551..0e6b08d 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "magepal/magento2-custom-smtp", - "version": "3.0.0", + "version": "3.5.0", "description":"Magento 2 SMTP Extension - Configure Magento 2 to send all transactional email using Gmail, G Suite, Amazon SES, Office360, Mailgun, SendGrid, Mandrill or any other SMTP servers", "type": "magento2-module", "keywords": [ @@ -18,10 +18,10 @@ "how to setup email magento2" ], "require": { - "php": "~5.6.0|7.0.2|7.0.4|~7.0.6|~7.1.0|~7.1.3|~7.2.0|~7.3.0|~7.4.0", - "magento/module-backend": "100.0.*|100.1.*|100.2.*|101.0.*|102.0.*", - "magento/framework": "100.0.*|100.1.*|101.0.*|102.0.*|103.0.*", - "magepal/magento2-core": ">=1.1.11" + "php": "~7.4.0|~8.1.0|~8.2.0|~8.3.0", + "magento/module-backend": "102.0.*", + "magento/framework": "103.0.*", + "magepal/magento2-core":">=1.1.11" }, "license": [ "proprietary" diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index f8569ee..453abf4 100755 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -21,7 +21,7 @@ - Copyright © 2022 MagePal, LLC + Copyright © 2023 MagePal, LLC Documentation Support Latest Version @@ -30,7 +30,7 @@
Get more from your order confirmation emails by promoting other complementary products! - Learn more about our new Enhanced Transactional Email extension. + Learn more about our new Email Promotional Products extension.

]]> diff --git a/etc/di.xml b/etc/di.xml index 9f58a34..a6f219d 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -7,13 +7,6 @@ */ --> - - - - - - - diff --git a/view/adminhtml/web/js/validate-config.js b/view/adminhtml/web/js/validate-config.js index b1b28d5..8d574fd 100755 --- a/view/adminhtml/web/js/validate-config.js +++ b/view/adminhtml/web/js/validate-config.js @@ -19,8 +19,8 @@ define([ var name = field.name.match(/groups\[general\]?(\[groups\]\[debug\])?\[fields\]\[(.*)\]\[value]/); /** - * groups[custom_smtp][groups][debug][fields][email][value] - * groups[custom_smtp][fields][password][value] + * groups[general][groups][debug][fields][email][value] + * groups[general][fields][password][value] */ if (name && name.length === 3) {