Skip to content

Commit a6f5e6d

Browse files
committed
Updates to compatibility
1 parent 52cc54f commit a6f5e6d

File tree

10 files changed

+112
-98
lines changed

10 files changed

+112
-98
lines changed

Block/Adminhtml/Log/Preview.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,10 @@ protected function _toHtml()
6161
}
6262

6363
if ($logModel->getId()) {
64-
return '<iframe onload="resizeIframe(this)" srcdoc="'. $string . '" style="width: 100%; height: 100%"></iframe>';
64+
return '<iframe onload="resizeIframe(this)" srcdoc="' . $string . '" style="width: 100%; height: 100%"></iframe>';
6565
} else {
6666
throw new \Exception('Log with ID not found. Pleas try again');
6767
}
68-
69-
return $logModel;
7068
}
7169

7270
/**
@@ -80,7 +78,7 @@ public function getCurrentLog()
8078
/**
8179
* Get either default or any store view
8280
*
83-
* @return \Magento\Store\Model\Store|null
81+
* @return \Magento\Store\Api\Data\StoreInterface|null
8482
*/
8583
protected function getAnyStoreView()
8684
{

CHANGELOG.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## 1.0.6 - 2024-02-14
2+
### Fixed
3+
- Better error handling of sending scenarios.
4+
5+
## 1.0.5 - 2024-02-14
6+
### Changed
7+
- Updated module dependencies for improved compatibility (SMTP-19)
8+
- Code style improvements (SEND-16)
9+
10+
## 1.0.4 - 2022-04-26
11+
### Changed
12+
- Added PHP 8.1 compatibility
13+
- Added Magento 2.4.4 compatibility
14+
15+
## 1.0.3 - 2021-09-14
16+
### Fixed
17+
- Fixed issue with email when special character is available in customer name
18+
- Fixed the resend email content
19+
20+
## 1.0.2 - 2021-07-24
21+
### Changed
22+
- Update compatibility for Magento 2.4
23+
24+
## 1.0.1 - 2020-06-19
25+
### Fixed
26+
- Minor bug fixes
27+
28+
## 1.0.0 - 2019-11-08
29+
### Changed
30+
- Extension release

Controller/Adminhtml/Smtp/Test.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
namespace Aitoc\Smtp\Controller\Adminhtml\Smtp;
44

5-
use Exception;
5+
use Aitoc\Smtp\Controller\RegistryConstants;
66
use Magento\Backend\App\Action;
77
use Magento\Backend\App\Action\Context;
88
use Magento\Email\Model\Template\SenderResolver;
99
use Magento\Framework\App\ResponseInterface;
1010
use Magento\Framework\Controller\ResultInterface;
1111
use Magento\Framework\Exception\LocalizedException;
1212
use Psr\Log\LoggerInterface;
13-
use Aitoc\Smtp\Controller\RegistryConstants;
1413

1514
class Test extends Action
1615
{
@@ -26,10 +25,6 @@ class Test extends Action
2625
* @var LoggerInterface
2726
*/
2827
private $logger;
29-
/**
30-
* @var TransportBuilder
31-
*/
32-
private $_transportBuilder;
3328

3429
/**
3530
* @var SenderResolver
@@ -57,12 +52,12 @@ class Test extends Action
5752
private $transportInterfaceFactory;
5853

5954
public function __construct(
60-
Context $context,
61-
LoggerInterface $logger,
62-
SenderResolver $senderResolver,
63-
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
64-
\Magento\Config\Model\Config\Factory $configFactory,
65-
\Aitoc\Smtp\Model\Config $config,
55+
Context $context,
56+
LoggerInterface $logger,
57+
SenderResolver $senderResolver,
58+
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
59+
\Magento\Config\Model\Config\Factory $configFactory,
60+
\Aitoc\Smtp\Model\Config $config,
6661
\Magento\Framework\Mail\TransportInterfaceFactory $transportInterfaceFactory
6762
) {
6863
$this->logger = $logger;
@@ -84,14 +79,17 @@ public function execute()
8479
$data = $this->getRequest()->getParams();
8580

8681
try {
87-
if ($data && isset($data[self::TEST_EMAIL_TO_FIELD_NAME])) {
82+
if (!$this->config->enabled()) {
83+
$result = [
84+
'status' => false,
85+
'content' => __('Please enable the module.')
86+
];
87+
} elseif ($data && isset($data[self::TEST_EMAIL_TO_FIELD_NAME])) {
8888
$config = $this->config->convertFromPostToSmtpParams($this->removeUnusedFields($data));
8989
$config[RegistryConstants::IS_TEST_FIELD_ARRAY] = true;
9090
unset($config[self::TEST_EMAIL_TO_FIELD_NAME]);
9191

9292
$transport = $this->transportInterfaceFactory->create($config);
93-
94-
9593
$transport->testSend($data[self::TEST_EMAIL_TO_FIELD_NAME]);
9694

9795
$result = [

Model/Config.php

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
namespace Aitoc\Smtp\Model;
44

5-
use Magento\Framework\Exception\LocalizedException;
6-
use Magento\Framework\Exception\NoSuchEntityException;
7-
use Magento\Store\Model\ScopeInterface;
85
use Magento\Email\Model\Template\SenderResolver;
96
use Magento\Framework\App\ProductMetadataInterface;
10-
use Aitoc\Smtp\Controller\RegistryConstants;
7+
use Magento\Framework\Exception\LocalizedException;
8+
use Magento\Store\Model\ScopeInterface;
119

1210
/**
1311
* Class Config
@@ -120,7 +118,6 @@ public function getCurrentStoreId()
120118
$storeId = $this->storeManager->getStore()->getId();
121119
}
122120
} catch (LocalizedException $exception) {
123-
} catch (NoSuchEntityException $e) {
124121
}
125122

126123
return $storeId;
@@ -136,7 +133,7 @@ public function getCcEmails()
136133
$this->getCurrentStoreId()
137134
);
138135

139-
return $this->getDataFromString($allowedEmailsString);;
136+
return $this->getDataFromString($allowedEmailsString);
140137
}
141138

142139
/**
@@ -162,10 +159,9 @@ protected function _getExceptionalEmails()
162159
$this->getCurrentStoreId()
163160
);
164161

165-
$arrayFromString = explode(',', $allowedEmailsString);
162+
$arrayFromString = explode(',', (string)$allowedEmailsString);
166163
array_walk($arrayFromString, [$this, 'trimAllowedValues']);
167164

168-
169165
return $arrayFromString;
170166
}
171167

@@ -179,7 +175,7 @@ protected function _getExceptionalDomains()
179175
$this->getCurrentStoreId()
180176
);
181177

182-
$arrayFromString = explode(',', $allowedDomainsString);
178+
$arrayFromString = explode(',', (string)$allowedDomainsString);
183179
array_walk($arrayFromString, [$this, 'trimAllowedValues']);
184180

185181
return $arrayFromString;
@@ -229,8 +225,7 @@ public function needToBlockEmail($email)
229225
$domains = $this->_getExceptionalDomains();
230226
$emails = $this->_getExceptionalEmails();
231227

232-
if (($emails && in_array($email, $emails)) || (is_array($domains) && $this->validDomain($email, $domains)))
233-
{
228+
if (($emails && in_array($email, $emails)) || (is_array($domains) && $this->validDomain($email, $domains))) {
234229
return false;
235230
}
236231

@@ -355,19 +350,20 @@ public function prepareMessageToSend($message, $isTest = false)
355350

356351
/**
357352
* @param $emailData
358-
* @return \Zend\Mail\Address
353+
* @return \Laminas\Mail\Address
359354
*/
360-
public function getNewAddress($emailData){
361-
return new \Zend\Mail\Address($emailData['email'], $emailData['name']);
355+
public function getNewAddress($emailData)
356+
{
357+
return new \Laminas\Mail\Address($emailData['email'], $emailData['name']);
362358
}
363359

364-
365360
/**
366361
* @param $emailsData
367-
* @return \Zend\Mail\AddressList
362+
* @return \Laminas\Mail\AddressList
368363
*/
369-
public function getAddressList($emailsData) {
370-
$addressList = new \Zend\Mail\AddressList();
364+
public function getAddressList($emailsData)
365+
{
366+
$addressList = new \Laminas\Mail\AddressList();
371367

372368
foreach ($emailsData as $data) {
373369
$addressList->add($data['email'], isset($data['name']) ? $data['name'] : null);

Model/Framework/Mail/Transport.php

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,16 @@
22

33
namespace Aitoc\Smtp\Model\Framework\Mail;
44

5-
use Magento\Framework\Exception\MailException;
6-
use Magento\Framework\Phrase;
7-
use Zend\Mail\Message as ZendMessage;
8-
use Zend\Mail\Address;
9-
use Zend\Mail\Transport\Smtp;
10-
use Magento\Framework\Mail\MessageInterface;
11-
use Magento\Email\Model\Template\SenderResolver;
5+
use Aitoc\Smtp\Controller\RegistryConstants;
126
use Aitoc\Smtp\Model\Config;
13-
use Zend\Mail\Transport\SmtpOptions;
14-
use Aitoc\Smtp\Model\Resolver\From;
15-
use Aitoc\Smtp\Model\LogFactory;
167
use Aitoc\Smtp\Model\Config\Options\Status;
8+
use Aitoc\Smtp\Model\LogFactory;
9+
use Aitoc\Smtp\Model\Resolver\From;
10+
use Magento\Email\Model\Template\SenderResolver;
11+
use Magento\Framework\Exception\MailException;
12+
use Magento\Framework\Mail\MessageInterface;
13+
use Magento\Framework\Phrase;
1714
use Magento\Framework\Registry;
18-
use Aitoc\Smtp\Controller\RegistryConstants;
19-
use Zend_Mail_Transport_Smtp;
2015

2116
class Transport implements \Magento\Framework\Mail\TransportInterface
2217
{
@@ -26,9 +21,9 @@ class Transport implements \Magento\Framework\Mail\TransportInterface
2621
"Now, your store uses an Aitoc SMTP. Please, hit ‘Save Config’ to use this connection.";
2722

2823
/**
29-
* @var Sendmail
24+
* @var \Laminas\Mail\Transport\Smtp
3025
*/
31-
private $zendTransport;
26+
private $smtp;
3227

3328
/**
3429
* @var MessageInterface
@@ -80,13 +75,7 @@ public function __construct(
8075
$this->fromResolver = $from;
8176
$this->logFactory = $logFactory;
8277
$this->registry = $registry;
83-
84-
if ($this->aitConfig->isNewSender(RegistryConstants::VERSION_COMPARISON_OLD_MAIL)) {
85-
$this->zendTransport = new Smtp($this->prepareOptions($config));
86-
} else {
87-
$this->zendTransport = new Zend_Mail_Transport_Smtp($config['host'], $config);
88-
}
89-
78+
$this->smtp = new \Laminas\Mail\Transport\Smtp($this->prepareOptions($config));
9079
$this->message = $message;
9180
$this->setFrom();
9281
}
@@ -97,13 +86,13 @@ public function __construct(
9786
public function setFrom()
9887
{
9988
$fromData = $this->fromResolver->getFrom();
100-
$message = ZendMessage::fromString($this->message->getRawMessage());
89+
$message = \Laminas\Mail\Message::fromString($this->message->getRawMessage());
10190

10291
if ($fromData) {
103-
if (($message instanceof ZendMessage && !$message->getFrom()->count())
104-
|| ((is_array($message->getHeaders()) && !array_key_exists("From", $message->getHeaders())))
92+
if (($message instanceof \Laminas\Mail\Message && !$message->getFrom()->count())
93+
|| (!array_key_exists("From", $message->getHeaders()->toArray()))
10594
) {
106-
$this->message->setFrom($this->aitConfig->getNewAddress($fromData));
95+
$this->message->setFromAddress($this->aitConfig->getNewAddress($fromData));
10796
}
10897
}
10998

@@ -112,23 +101,20 @@ public function setFrom()
112101

113102
/**
114103
* @param $config
115-
* @return SmtpOptions
104+
* @return \Laminas\Mail\Transport\SmtpOptions
116105
*/
117106
private function prepareOptions($config)
118107
{
119108
if (!isset($config['name']) || !$config['name']) {
120109
$config['name'] = self::DEFAULT_LOCAL_CLIENT_HOSTNAME;
121110
}
122111

123-
$options = new SmtpOptions([
124-
'name' => isset($config['name']) ? $config['name'] : '',
125-
'host' => isset($config['host']) ? $config['host'] : '',
126-
'port' => isset($config['port']) ? $config['port'] : 465,
127-
]);
128-
112+
$options = new \Laminas\Mail\Transport\SmtpOptions();
113+
$options->setName($config['name'] ?? '');
114+
$options->setHost($config['host'] ?? '');
115+
$options->setPort($config['port'] ?? 465);
129116
$connectionConfig = [];
130117

131-
132118
if (isset($config['auth']) && $config['auth'] != '') {
133119
$options->setConnectionClass($config['auth']);
134120
$connectionConfig = [
@@ -159,7 +145,7 @@ public function sendMessage()
159145
$this->message = $this->aitConfig->prepareMessageToSend($this->getMessage());
160146

161147
if ($this->aitConfig->isNewSender(RegistryConstants::VERSION_COMPARISON_OLD_MAIL)) {
162-
$message = ZendMessage::fromString($this->message->getRawMessage())->setEncoding('utf-8');
148+
$message = \Laminas\Mail\Message::fromString($this->message->getRawMessage())->setEncoding('utf-8');
163149
} else {
164150
$message = $this->message;
165151
}
@@ -182,7 +168,9 @@ public function sendMessage()
182168
$message = $modifiedRecipient;
183169
}
184170

185-
$this->zendTransport->send($message);
171+
if ($message) {
172+
$this->smtp->send($message);
173+
}
186174

187175
if (!$logDisabled) {
188176
$this->getLoger()->log($message);
@@ -209,12 +197,12 @@ private function getLoger()
209197
}
210198

211199
/**
212-
* @return bool|ZendMessage
200+
* @return bool|\Laminas\Mail\Message
213201
*/
214202
public function modifyTo()
215203
{
216204
if ($this->aitConfig->isNewSender(RegistryConstants::VERSION_COMPARISON_OLD_MAIL)) {
217-
$message = ZendMessage::fromString($this->message->getRawMessage())->setEncoding('utf-8');
205+
$message = \Laminas\Mail\Message::fromString($this->message->getRawMessage())->setEncoding('utf-8');
218206
} else {
219207
$message = $this->message;
220208
}
@@ -226,7 +214,7 @@ public function modifyTo()
226214
if ($toEmails) {
227215
foreach ($toEmails as $email) {
228216
$name = '';
229-
if ($email instanceof Address) {
217+
if ($email instanceof \Laminas\Mail\Address\AddressInterface) {
230218
$name = $email->getName();
231219
$email = $email->getEmail();
232220
}
@@ -268,12 +256,12 @@ public function testSend($to)
268256
->setBodyText(__(self::TEST_MESSAGE_BODY));
269257

270258
if ($this->aitConfig->isNewSender(RegistryConstants::VERSION_COMPARISON_OLD_MAIL)) {
271-
$message = ZendMessage::fromString($this->message->getRawMessage())->setEncoding('utf-8');
259+
$message = \Laminas\Mail\Message::fromString($this->message->getRawMessage())->setEncoding('utf-8');
272260
} else {
273261
$message = $this->message;
274262
}
275263

276-
$this->zendTransport->send($message);
264+
$this->smtp->send($message);
277265

278266
return $result;
279267
} catch (\Exception $e) {

0 commit comments

Comments
 (0)