Skip to content

Commit a6a228c

Browse files
committed
Custom SMTP for Magento 2.0.0 - 2.4.3
1 parent b0a9803 commit a6a228c

File tree

9 files changed

+322
-77
lines changed

9 files changed

+322
-77
lines changed

Block/Adminhtml/ValidateConfig.php

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

1010
use Exception;
11-
use Laminas\Mime\Message as MineMessage;
12-
use Laminas\Mime\Part as MinePart;
1311
use Magento\Backend\Block\Template;
1412
use Magento\Backend\Block\Template\Context;
1513
use Magento\Framework\Validator\EmailAddress;
1614
use MagePal\CustomSmtp\Helper\Data;
1715
use MagePal\CustomSmtp\Model\Email;
18-
use Laminas\Mail\Message;
19-
use Laminas\Mail\Transport\Smtp;
20-
use Laminas\Mail\Transport\SmtpOptions;
16+
use Zend_Mail;
17+
use Zend_Mail_Exception;
18+
use Zend_Mail_Transport_Smtp;
19+
use Zend_Validate_Exception;
2120

2221
class ValidateConfig extends Template
2322
{
@@ -196,7 +195,7 @@ protected function init()
196195

197196
$this->toAddress = $this->getConfig('email') ? $this->getConfig('email') : $this->getConfig('username');
198197

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

201200
if (!$this->emailAddressValidator->isValid($this->fromAddress)) {
202201
$this->fromAddress = $this->toAddress;
@@ -246,7 +245,7 @@ public function verify()
246245
/**
247246
* Todo: update to new Zend Framework SMTP
248247
* @return array
249-
* @throws \Exception
248+
* @throws Zend_Mail_Exception
250249
* @throws \Magento\Framework\Exception\NoSuchEntityException
251250
*/
252251
protected function validateServerEmailSetting()
@@ -255,6 +254,7 @@ protected function validateServerEmailSetting()
255254

256255
$username = $this->getConfig('username');
257256
$password = $this->getConfig('password');
257+
258258
$auth = strtolower($this->getConfig('auth'));
259259

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

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

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

282-
$body = new MineMessage();
283-
$body->addPart($bodyMessage);
284+
$htmlBody = $this->_email->setTemplateVars(['hash' => $this->hash])->getEmailBody();
284285

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');
286+
$mail->setBodyHtml($htmlBody);
291287

292288
$result = $this->error();
293289

294290
try {
295-
$transport->send($message);
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+
}
296295
} catch (Exception $e) {
297296
$result = $this->error(true, __($e->getMessage()));
298297
}
@@ -304,35 +303,29 @@ public function getMailTransportSmtp()
304303
{
305304
$username = $this->getConfig('username');
306305
$password = $this->getConfig('password');
306+
307307
$auth = strtolower($this->getConfig('auth'));
308308

309-
$optionsArray = [
309+
//SMTP server configuration
310+
$smtpHost = $this->getConfig('smtphost');
311+
312+
$smtpConf = [
310313
'name' => $this->getConfig('name'),
311-
'host' => $this->getConfig('smtphost'),
312314
'port' => $this->getConfig('smtpport')
313315
];
314316

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

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

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

338331
/**

Mail/ZF1/Smtp.php

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
<?php
2+
/**
3+
* Copyright © MagePal LLC. All rights reserved.
4+
* See COPYING.txt for license details.
5+
* http://www.magepal.com | [email protected]
6+
*/
7+
8+
namespace MagePal\CustomSmtp\Mail\ZF1;
9+
10+
use Exception;
11+
use Magento\Framework\Exception\MailException;
12+
use Magento\Framework\Mail\MessageInterface;
13+
use Magento\Framework\Phrase;
14+
use MagePal\CustomSmtp\Helper\Data;
15+
use MagePal\CustomSmtp\Model\Store;
16+
use Zend_Mail;
17+
use Zend_Mail_Exception;
18+
use Zend_Mail_Transport_Smtp;
19+
20+
/**
21+
* Class Smtp
22+
* For Magento <= 2.2.7
23+
*/
24+
25+
class Smtp extends Zend_Mail_Transport_Smtp
26+
{
27+
/**
28+
* @var Data
29+
*/
30+
protected $dataHelper;
31+
32+
/**
33+
* @var Store
34+
*/
35+
protected $storeModel;
36+
37+
/**
38+
* @param Data $dataHelper
39+
* @param Store $storeModel
40+
*/
41+
public function __construct(
42+
Data $dataHelper,
43+
Store $storeModel
44+
) {
45+
$this->dataHelper = $dataHelper;
46+
$this->storeModel = $storeModel;
47+
}
48+
49+
/**
50+
* @param Data $dataHelper
51+
* @return Smtp
52+
*/
53+
public function setDataHelper(Data $dataHelper)
54+
{
55+
$this->dataHelper = $dataHelper;
56+
return $this;
57+
}
58+
59+
/**
60+
* @param Store $storeModel
61+
* @return Smtp
62+
*/
63+
public function setStoreModel(Store $storeModel)
64+
{
65+
$this->storeModel = $storeModel;
66+
return $this;
67+
}
68+
69+
/**
70+
* @param MessageInterface $message
71+
* @throws MailException
72+
* @throws Zend_Mail_Exception
73+
*/
74+
public function sendSmtpMessage(
75+
MessageInterface $message
76+
) {
77+
$dataHelper = $this->dataHelper;
78+
$dataHelper->setStoreId($this->storeModel->getStoreId());
79+
80+
if ($message instanceof Zend_mail) {
81+
if ($message->getDate() === null) {
82+
$message->setDate();
83+
}
84+
}
85+
86+
//Set reply-to path
87+
switch ($dataHelper->getConfigSetReturnPath()) {
88+
case 1:
89+
$returnPathEmail = $message->getFrom() ?: $this->getFromEmailAddress();
90+
break;
91+
case 2:
92+
$returnPathEmail = $dataHelper->getConfigReturnPathEmail();
93+
break;
94+
default:
95+
$returnPathEmail = null;
96+
break;
97+
}
98+
99+
if ($returnPathEmail !== null && $dataHelper->getConfigSetReturnPath()) {
100+
$message->setReturnPath($returnPathEmail);
101+
}
102+
103+
if ($message->getReplyTo() === null && $dataHelper->getConfigSetReplyTo()) {
104+
$message->setReplyTo($returnPathEmail);
105+
}
106+
107+
//Set from address
108+
switch ($dataHelper->getConfigSetFrom()) {
109+
case 1:
110+
$setFromEmail = $message->getFrom() ?: $this->getFromEmailAddress();
111+
break;
112+
case 2:
113+
$setFromEmail = $dataHelper->getConfigCustomFromEmail();
114+
break;
115+
default:
116+
$setFromEmail = null;
117+
break;
118+
}
119+
if ($setFromEmail !== null && $dataHelper->getConfigSetFrom()) {
120+
$message->clearFrom();
121+
$message->setFrom($setFromEmail);
122+
}
123+
124+
if (!$message->getFrom()) {
125+
$result = $this->storeModel->getFrom();
126+
$message->setFrom($result['email'], $result['name']);
127+
}
128+
129+
//set config
130+
$smtpConf = [
131+
'name' => $dataHelper->getConfigName(),
132+
'port' => $dataHelper->getConfigSmtpPort(),
133+
];
134+
135+
$auth = strtolower($dataHelper->getConfigAuth());
136+
if ($auth != 'none') {
137+
$smtpConf['auth'] = $auth;
138+
$smtpConf['username'] = $dataHelper->getConfigUsername();
139+
$smtpConf['password'] = $dataHelper->getConfigPassword();
140+
}
141+
142+
$ssl = $dataHelper->getConfigSsl();
143+
if ($ssl != 'none') {
144+
$smtpConf['ssl'] = $ssl;
145+
}
146+
147+
$smtpHost = $dataHelper->getConfigSmtpHost();
148+
$this->initialize($smtpHost, $smtpConf);
149+
150+
try {
151+
parent::send($message);
152+
} catch (Exception $e) {
153+
throw new MailException(
154+
new Phrase($e->getMessage()),
155+
$e
156+
);
157+
}
158+
}
159+
160+
/**
161+
* @return string
162+
*/
163+
public function getFromEmailAddress()
164+
{
165+
$result = $this->storeModel->getFrom();
166+
return $result['email'];
167+
}
168+
169+
/**
170+
* @param string $host
171+
* @param array $config
172+
*/
173+
public function initialize($host = '127.0.0.1', array $config = [])
174+
{
175+
if (isset($config['name'])) {
176+
$this->_name = $config['name'];
177+
}
178+
if (isset($config['port'])) {
179+
$this->_port = $config['port'];
180+
}
181+
if (isset($config['auth'])) {
182+
$this->_auth = $config['auth'];
183+
}
184+
185+
$this->_host = $host;
186+
$this->_config = $config;
187+
}
188+
}

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

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

8-
namespace MagePal\CustomSmtp\Mail;
8+
namespace MagePal\CustomSmtp\Mail\ZF2;
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;
1711
use Magento\Framework\Exception\MailException;
1812
use Magento\Framework\Mail\EmailMessageInterface;
1913
use Magento\Framework\Mail\MessageInterface;
2014
use Magento\Framework\Phrase;
2115
use MagePal\CustomSmtp\Helper\Data;
2216
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;
2321

2422
/**
2523
* Class Smtp
24+
* For Magento >= 2.2.8
2625
*/
2726
class Smtp
2827
{
@@ -97,9 +96,7 @@ protected function convertMessage($message)
9796
}
9897

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

129126
foreach ($message->getHeaders()->toArray() as $headerKey => $headerValue) {
130127
$mailHeader = $message->getHeaders()->get($headerKey);
131-
if ($mailHeader instanceof HeaderInterface) {
128+
if ($mailHeader instanceof \Zend\Mail\Header\HeaderInterface) {
132129
$this->updateMailHeader($mailHeader);
133130
} elseif ($mailHeader instanceof \ArrayIterator) {
134131
foreach ($mailHeader as $header) {
@@ -150,7 +147,6 @@ public function sendSmtpMessage(
150147
}
151148

152149
/**
153-
*
154150
* @param Message $message
155151
*/
156152
protected function setSender($message)
@@ -263,8 +259,8 @@ protected function getSmtpOptions()
263259
*/
264260
public function updateMailHeader($header)
265261
{
266-
if ($header instanceof HeaderInterface) {
267-
if (Mime::isPrintable($header->getFieldValue())) {
262+
if ($header instanceof \Zend\Mail\Header\HeaderInterface) {
263+
if (\Zend\Mime\Mime::isPrintable($header->getFieldValue())) {
268264
$header->setEncoding('ASCII');
269265
} else {
270266
$header->setEncoding('utf-8');

0 commit comments

Comments
 (0)