Skip to content

Commit

Permalink
Add support for Magento 2.4.4
Browse files Browse the repository at this point in the history
  • Loading branch information
srenon committed Apr 12, 2022
1 parent b3b8226 commit e6e17cf
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 244 deletions.
65 changes: 35 additions & 30 deletions Block/Adminhtml/ValidateConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
namespace MagePal\GmailSmtpApp\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\GmailSmtpApp\Helper\Data;
use MagePal\GmailSmtpApp\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
{
Expand Down Expand Up @@ -233,9 +234,7 @@ public function verify()
}

/**
* Todo: update to new Zend Framework SMTP
* @return array
* @throws Zend_Mail_Exception
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
protected function validateServerEmailSetting()
Expand All @@ -258,49 +257,55 @@ protected function validateServerEmailSetting()
}
}

//SMTP server configuration
$smtpHost = $this->getConfig('smtphost');
$name = 'Test from MagePal SMTP';
$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();

$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
);
}

$transport = new Zend_Mail_Transport_Smtp($smtpHost, $smtpConf);
$options = new SmtpOptions($optionsArray);
$transport = new Smtp();
$transport->setOptions($options);

$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;

//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)');
$bodyMessage = new MinePart($htmlBody);
$bodyMessage->type = 'text/html';

$htmlBody = $this->_email->setTemplateVars(['hash' => $this->hash])->getEmailBody();
$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 a 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()));
}
Expand Down
21 changes: 12 additions & 9 deletions Model/ZendMailTwo/Smtp.php → Mail/Smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
* http://www.magepal.com | support@magepal.com
*/

namespace MagePal\GmailSmtpApp\Model\ZendMailTwo;
namespace MagePal\GmailSmtpApp\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\GmailSmtpApp\Helper\Data;
use MagePal\GmailSmtpApp\Model\Store;
use Zend\Mail\AddressList;
use Zend\Mail\Message;
use Zend\Mail\Transport\Smtp as SmtpTransport;
use Zend\Mail\Transport\SmtpOptions;
use Magento\Framework\Mail\EmailMessageInterface;

/**
* Class Smtp
Expand Down Expand Up @@ -125,7 +127,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) {
Expand All @@ -147,6 +149,7 @@ public function sendSmtpMessage(
}

/**
*
* @param Message $message
*/
protected function setSender($message)
Expand Down Expand Up @@ -259,8 +262,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');
Expand Down
188 changes: 0 additions & 188 deletions Model/ZendMailOne/Smtp.php

This file was deleted.

Loading

0 comments on commit e6e17cf

Please sign in to comment.