Skip to content

Commit 057b1ab

Browse files
committed
Custom SMTP for Magento 2.4.3+
1 parent a6a228c commit 057b1ab

File tree

9 files changed

+76
-321
lines changed

9 files changed

+76
-321
lines changed

Block/Adminhtml/ValidateConfig.php

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
namespace MagePal\CustomSmtp\Block\Adminhtml;
99

1010
use Exception;
11+
use Laminas\Mime\Message as MineMessage;
12+
use Laminas\Mime\Part as MinePart;
1113
use Magento\Backend\Block\Template;
1214
use Magento\Backend\Block\Template\Context;
1315
use Magento\Framework\Validator\EmailAddress;
1416
use MagePal\CustomSmtp\Helper\Data;
1517
use MagePal\CustomSmtp\Model\Email;
16-
use Zend_Mail;
17-
use Zend_Mail_Exception;
18-
use Zend_Mail_Transport_Smtp;
19-
use Zend_Validate_Exception;
18+
use Laminas\Mail\Message;
19+
use Laminas\Mail\Transport\Smtp;
20+
use Laminas\Mail\Transport\SmtpOptions;
2021

2122
class ValidateConfig extends Template
2223
{
@@ -195,7 +196,7 @@ protected function init()
195196

196197
$this->toAddress = $this->getConfig('email') ? $this->getConfig('email') : $this->getConfig('username');
197198

198-
$this->fromAddress = trim($this->getConfig('from_email'));
199+
$this->fromAddress = trim((string) $this->getConfig('from_email'));
199200

200201
if (!$this->emailAddressValidator->isValid($this->fromAddress)) {
201202
$this->fromAddress = $this->toAddress;
@@ -245,7 +246,7 @@ public function verify()
245246
/**
246247
* Todo: update to new Zend Framework SMTP
247248
* @return array
248-
* @throws Zend_Mail_Exception
249+
* @throws \Exception
249250
* @throws \Magento\Framework\Exception\NoSuchEntityException
250251
*/
251252
protected function validateServerEmailSetting()
@@ -254,7 +255,6 @@ protected function validateServerEmailSetting()
254255

255256
$username = $this->getConfig('username');
256257
$password = $this->getConfig('password');
257-
258258
$auth = strtolower($this->getConfig('auth'));
259259

260260
//if default view
@@ -268,30 +268,31 @@ protected function validateServerEmailSetting()
268268
}
269269
}
270270

271-
$transport = $this->getMailTransportSmtp();
272-
273-
$from = trim($this->getConfig('from_email'));
271+
$name = 'Test from MagePal SMTP';
272+
$from = trim((string) $this->getConfig('from_email'));
274273
$from = filter_var($from, FILTER_VALIDATE_EMAIL) ? $from : $username;
275274
$this->fromAddress = filter_var($username, FILTER_VALIDATE_EMAIL) ? $username : $from;
275+
$htmlBody = $this->_email->setTemplateVars(['hash' => $this->hash])->getEmailBody();
276276

277-
//Create email
278-
$name = 'Test from MagePal SMTP';
279-
$mail = new Zend_Mail();
280-
$mail->setFrom($this->fromAddress, $name);
281-
$mail->addTo($this->toAddress, 'MagePal SMTP');
282-
$mail->setSubject('Hello from MagePal SMTP (1 of 2)');
277+
$transport = $this->getMailTransportSmtp();
283278

284-
$htmlBody = $this->_email->setTemplateVars(['hash' => $this->hash])->getEmailBody();
279+
$bodyMessage = new MinePart($htmlBody);
280+
$bodyMessage->type = 'text/html';
281+
282+
$body = new MineMessage();
283+
$body->addPart($bodyMessage);
285284

286-
$mail->setBodyHtml($htmlBody);
285+
$message = new Message();
286+
$message->addTo($this->toAddress, 'MagePal SMTP')
287+
->addFrom($this->fromAddress, $name)
288+
->setSubject('Hello from MagePal SMTP (1 of 2)')
289+
->setBody($body)
290+
->setEncoding('UTF-8');
287291

288292
$result = $this->error();
289293

290294
try {
291-
//only way to prevent zend from giving an error
292-
if (!$mail->send($transport) instanceof Zend_Mail) {
293-
$result = $this->error(true, __('Invalid class, not instance of Zend Mail'));
294-
}
295+
$transport->send($message);
295296
} catch (Exception $e) {
296297
$result = $this->error(true, __($e->getMessage()));
297298
}
@@ -303,29 +304,35 @@ public function getMailTransportSmtp()
303304
{
304305
$username = $this->getConfig('username');
305306
$password = $this->getConfig('password');
306-
307307
$auth = strtolower($this->getConfig('auth'));
308308

309-
//SMTP server configuration
310-
$smtpHost = $this->getConfig('smtphost');
311-
312-
$smtpConf = [
309+
$optionsArray = [
313310
'name' => $this->getConfig('name'),
311+
'host' => $this->getConfig('smtphost'),
314312
'port' => $this->getConfig('smtpport')
315313
];
316314

317315
if ($auth != 'none') {
318-
$smtpConf['auth'] = $auth;
319-
$smtpConf['username'] = $username;
320-
$smtpConf['password'] = $password;
316+
$optionsArray['connection_class'] = $auth;
317+
$optionsArray['connection_config'] = [
318+
'username' => $username,
319+
'password' => $password,
320+
];
321321
}
322322

323323
$ssl = $this->getConfig('ssl');
324324
if ($ssl != 'none') {
325-
$smtpConf['ssl'] = $ssl;
325+
$optionsArray = array_merge_recursive(
326+
['connection_config' => ['ssl' => $ssl]],
327+
$optionsArray
328+
);
326329
}
327330

328-
return new Zend_Mail_Transport_Smtp($smtpHost, $smtpConf);
331+
$options = new SmtpOptions($optionsArray);
332+
$transport = new Smtp();
333+
$transport->setOptions($options);
334+
335+
return $transport;
329336
}
330337

331338
/**

Mail/ZF2/Smtp.php renamed to Mail/Smtp.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,24 @@
55
* http://www.magepal.com | [email protected]
66
*/
77

8-
namespace MagePal\CustomSmtp\Mail\ZF2;
8+
namespace MagePal\CustomSmtp\Mail;
99

1010
use Exception;
11+
use Laminas\Mail\AddressList;
12+
use Laminas\Mail\Header\HeaderInterface;
13+
use Laminas\Mail\Message;
14+
use Laminas\Mail\Transport\Smtp as SmtpTransport;
15+
use Laminas\Mail\Transport\SmtpOptions;
16+
use Laminas\Mime\Mime;
1117
use Magento\Framework\Exception\MailException;
1218
use Magento\Framework\Mail\EmailMessageInterface;
1319
use Magento\Framework\Mail\MessageInterface;
1420
use Magento\Framework\Phrase;
1521
use MagePal\CustomSmtp\Helper\Data;
1622
use MagePal\CustomSmtp\Model\Store;
17-
use Zend\Mail\AddressList;
18-
use Zend\Mail\Message;
19-
use Zend\Mail\Transport\Smtp as SmtpTransport;
20-
use Zend\Mail\Transport\SmtpOptions;
2123

2224
/**
2325
* Class Smtp
24-
* For Magento >= 2.2.8
2526
*/
2627
class Smtp
2728
{
@@ -96,7 +97,9 @@ protected function convertMessage($message)
9697
}
9798

9899
if (!$zendMessage instanceof Message) {
99-
throw new MailException('Not instance of Message');
100+
throw new MailException(
101+
__('Not instance of Message')
102+
);
100103
}
101104
} catch (Exception $e) {
102105
$zendMessage = Message::fromString($message->getRawMessage());
@@ -125,7 +128,7 @@ public function sendSmtpMessage(
125128

126129
foreach ($message->getHeaders()->toArray() as $headerKey => $headerValue) {
127130
$mailHeader = $message->getHeaders()->get($headerKey);
128-
if ($mailHeader instanceof \Zend\Mail\Header\HeaderInterface) {
131+
if ($mailHeader instanceof HeaderInterface) {
129132
$this->updateMailHeader($mailHeader);
130133
} elseif ($mailHeader instanceof \ArrayIterator) {
131134
foreach ($mailHeader as $header) {
@@ -147,6 +150,7 @@ public function sendSmtpMessage(
147150
}
148151

149152
/**
153+
*
150154
* @param Message $message
151155
*/
152156
protected function setSender($message)
@@ -259,8 +263,8 @@ protected function getSmtpOptions()
259263
*/
260264
public function updateMailHeader($header)
261265
{
262-
if ($header instanceof \Zend\Mail\Header\HeaderInterface) {
263-
if (\Zend\Mime\Mime::isPrintable($header->getFieldValue())) {
266+
if ($header instanceof HeaderInterface) {
267+
if (Mime::isPrintable($header->getFieldValue())) {
264268
$header->setEncoding('ASCII');
265269
} else {
266270
$header->setEncoding('utf-8');

Mail/ZF1/Smtp.php

Lines changed: 0 additions & 188 deletions
This file was deleted.

0 commit comments

Comments
 (0)