diff --git a/Block/Adminhtml/ValidateConfig.php b/Block/Adminhtml/ValidateConfig.php index 49d08da..e239df1 100755 --- a/Block/Adminhtml/ValidateConfig.php +++ b/Block/Adminhtml/ValidateConfig.php @@ -8,16 +8,15 @@ 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 Laminas\Mail\Message; -use Laminas\Mail\Transport\Smtp; -use Laminas\Mail\Transport\SmtpOptions; +use Zend_Mail; +use Zend_Mail_Exception; +use Zend_Mail_Transport_Smtp; +use Zend_Validate_Exception; class ValidateConfig extends Template { @@ -196,7 +195,7 @@ protected function init() $this->toAddress = $this->getConfig('email') ? $this->getConfig('email') : $this->getConfig('username'); - $this->fromAddress = trim((string) $this->getConfig('from_email')); + $this->fromAddress = trim($this->getConfig('from_email')); if (!$this->emailAddressValidator->isValid($this->fromAddress)) { $this->fromAddress = $this->toAddress; @@ -246,7 +245,7 @@ public function verify() /** * Todo: update to new Zend Framework SMTP * @return array - * @throws \Exception + * @throws Zend_Mail_Exception * @throws \Magento\Framework\Exception\NoSuchEntityException */ protected function validateServerEmailSetting() @@ -255,6 +254,7 @@ protected function validateServerEmailSetting() $username = $this->getConfig('username'); $password = $this->getConfig('password'); + $auth = strtolower($this->getConfig('auth')); //if default view @@ -268,31 +268,30 @@ protected function validateServerEmailSetting() } } - $name = 'Test from MagePal SMTP'; - $from = trim((string) $this->getConfig('from_email')); + $transport = $this->getMailTransportSmtp(); + + $from = trim($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(); - - $transport = $this->getMailTransportSmtp(); - $bodyMessage = new MinePart($htmlBody); - $bodyMessage->type = 'text/html'; + //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)'); - $body = new MineMessage(); - $body->addPart($bodyMessage); + $htmlBody = $this->_email->setTemplateVars(['hash' => $this->hash])->getEmailBody(); - $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'); + $mail->setBodyHtml($htmlBody); $result = $this->error(); try { - $transport->send($message); + //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')); + } } catch (Exception $e) { $result = $this->error(true, __($e->getMessage())); } @@ -304,35 +303,29 @@ public function getMailTransportSmtp() { $username = $this->getConfig('username'); $password = $this->getConfig('password'); + $auth = strtolower($this->getConfig('auth')); - $optionsArray = [ + //SMTP server configuration + $smtpHost = $this->getConfig('smtphost'); + + $smtpConf = [ 'name' => $this->getConfig('name'), - 'host' => $this->getConfig('smtphost'), 'port' => $this->getConfig('smtpport') ]; if ($auth != 'none') { - $optionsArray['connection_class'] = $auth; - $optionsArray['connection_config'] = [ - 'username' => $username, - 'password' => $password, - ]; + $smtpConf['auth'] = $auth; + $smtpConf['username'] = $username; + $smtpConf['password'] = $password; } $ssl = $this->getConfig('ssl'); if ($ssl != 'none') { - $optionsArray = array_merge_recursive( - ['connection_config' => ['ssl' => $ssl]], - $optionsArray - ); + $smtpConf['ssl'] = $ssl; } - $options = new SmtpOptions($optionsArray); - $transport = new Smtp(); - $transport->setOptions($options); - - return $transport; + return new Zend_Mail_Transport_Smtp($smtpHost, $smtpConf); } /** diff --git a/Mail/ZF1/Smtp.php b/Mail/ZF1/Smtp.php new file mode 100644 index 0000000..dc8e84d --- /dev/null +++ b/Mail/ZF1/Smtp.php @@ -0,0 +1,188 @@ +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/Mail/Smtp.php b/Mail/ZF2/Smtp.php similarity index 93% rename from Mail/Smtp.php rename to Mail/ZF2/Smtp.php index 481554f..8011bfb 100644 --- a/Mail/Smtp.php +++ b/Mail/ZF2/Smtp.php @@ -5,24 +5,23 @@ * http://www.magepal.com | support@magepal.com */ -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'); diff --git a/Model/Transport.php b/Model/Transport.php new file mode 100755 index 0000000..9bb003b --- /dev/null +++ b/Model/Transport.php @@ -0,0 +1,62 @@ +_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 a50c2da..0f70887 100644 --- a/Plugin/Mail/TransportPlugin.php +++ b/Plugin/Mail/TransportPlugin.php @@ -13,9 +13,11 @@ use Magento\Framework\Mail\Message; use Magento\Framework\Mail\TransportInterface; use MagePal\CustomSmtp\Helper\Data; -use MagePal\CustomSmtp\Mail\SmtpFactory; -use MagePal\CustomSmtp\Mail\Smtp; +use MagePal\CustomSmtp\Mail\ZF1\Smtp as ZF1Smtp; +use MagePal\CustomSmtp\Mail\ZF2\Smtp as ZF2Smtp; use MagePal\CustomSmtp\Model\Store; +use Zend_Mail; +use Zend_Mail_Exception; class TransportPlugin { @@ -28,10 +30,6 @@ class TransportPlugin * @var Store */ protected $storeModel; - /** - * @var Smtp - */ - private SmtpFactory $smtpFactory; /** * @param Data $dataHelper @@ -39,18 +37,17 @@ class TransportPlugin */ public function __construct( Data $dataHelper, - Store $storeModel, - SmtpFactory $smtpFactory + Store $storeModel ) { $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, @@ -63,11 +60,13 @@ public function aroundSendMessage( $message = $subject->getMessage(); - if ($message instanceof Message || $message instanceof EmailMessageInterface) { - /** @var Smtp $smtp */ - $smtp = $this->smtpFactory->create( - ['dataHelper' => $this->dataHelper, 'storeModel' => $this->storeModel] - ); + //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); $smtp->sendSmtpMessage($message); } else { $proceed(); diff --git a/composer.json b/composer.json index f3192b8..bd19551 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { - "name": "magepal/custom-smtp", - "version": "3.5.0", + "name": "magepal/magento2-custom-smtp", + "version": "3.0.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": "~7.4.0||~8.1.0||~8.2.0", - "magento/module-backend": "102.0.*", - "magento/framework": "103.0.*", - "magepal/magento2-core":">=1.1.11" + "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" }, "license": [ "proprietary" diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 453abf4..f8569ee 100755 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -21,7 +21,7 @@ - Copyright © 2023 MagePal, LLC + Copyright © 2022 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 Email Promotional Products extension. + Learn more about our new Enhanced Transactional Email extension.

]]> diff --git a/etc/di.xml b/etc/di.xml index a6f219d..9f58a34 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -7,6 +7,13 @@ */ --> + + + + + + + diff --git a/view/adminhtml/web/js/validate-config.js b/view/adminhtml/web/js/validate-config.js index 8d574fd..b1b28d5 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[general][groups][debug][fields][email][value] - * groups[general][fields][password][value] + * groups[custom_smtp][groups][debug][fields][email][value] + * groups[custom_smtp][fields][password][value] */ if (name && name.length === 3) {