Skip to content

Commit

Permalink
Custom SMTP for Magento 2.4.3+
Browse files Browse the repository at this point in the history
  • Loading branch information
srenon committed Apr 24, 2024
1 parent a6a228c commit 057b1ab
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 321 deletions.
71 changes: 39 additions & 32 deletions Block/Adminhtml/ValidateConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -254,7 +255,6 @@ protected function validateServerEmailSetting()

$username = $this->getConfig('username');
$password = $this->getConfig('password');

$auth = strtolower($this->getConfig('auth'));

//if default view
Expand All @@ -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()));
}
Expand All @@ -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;
}

/**
Expand Down
24 changes: 14 additions & 10 deletions Mail/ZF2/Smtp.php → Mail/Smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@
* http://www.magepal.com | [email protected]
*/

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
{
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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) {
Expand All @@ -147,6 +150,7 @@ public function sendSmtpMessage(
}

/**
*
* @param Message $message
*/
protected function setSender($message)
Expand Down Expand Up @@ -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');
Expand Down
188 changes: 0 additions & 188 deletions Mail/ZF1/Smtp.php

This file was deleted.

Loading

0 comments on commit 057b1ab

Please sign in to comment.