Skip to content

Commit e0f083e

Browse files
committed
Merge branch 'pr-32241' into 2.4-develop-prs
2 parents 672b4f3 + 00676ad commit e0f083e

File tree

6 files changed

+275
-10
lines changed

6 files changed

+275
-10
lines changed

app/code/Magento/Backend/etc/adminhtml/system.xml

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,14 +317,18 @@
317317
<label>Disable Email Communications</label>
318318
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
319319
</field>
320-
<field id="host" translate="label comment" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
320+
<field id="transport" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
321+
<label>Transport</label>
322+
<source_model>Magento\Email\Model\Config\Source\SmtpTransportType</source_model>
323+
</field>
324+
<field id="host" translate="label comment" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
321325
<label>Host</label>
322-
<comment>For Windows server only.</comment>
326+
<comment>For SMTP and Windows server only.</comment>
323327
</field>
324-
<field id="port" translate="label comment" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
328+
<field id="port" translate="label comment" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
325329
<label>Port (25)</label>
326330
<validate>validate-digits validate-digits-range digits-range-0-65535</validate>
327-
<comment>Please enter at least 0 and at most 65535 (For Windows server only).</comment>
331+
<comment>Please enter at least 0 and at most 65535 (For SMTP and Windows server only).</comment>
328332
</field>
329333
<field id="set_return_path" translate="label" type="select" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
330334
<label>Set Return-Path</label>
@@ -338,6 +342,34 @@
338342
<field id="set_return_path">2</field>
339343
</depends>
340344
</field>
345+
<field id="username" translate="label comment" type="text" sortOrder="90" showInDefault="1" showInWebsite="1" showInStore="1">
346+
<label>Username</label>
347+
<comment>Username</comment>
348+
<depends>
349+
<field id="transport">smtp</field>
350+
</depends>
351+
</field>
352+
<field id="password" translate="label comment" type="password" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
353+
<label>Password</label>
354+
<comment>Username</comment>
355+
<depends>
356+
<field id="transport">smtp</field>
357+
</depends>
358+
</field>
359+
<field id="auth" translate="label" type="select" sortOrder="110" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
360+
<label>Auth</label>
361+
<source_model>Magento\Email\Model\Config\Source\SmtpAuthType</source_model>
362+
<depends>
363+
<field id="transport">smtp</field>
364+
</depends>
365+
</field>
366+
<field id="ssl" translate="label" type="select" sortOrder="120" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
367+
<label>SSL</label>
368+
<source_model>Magento\Email\Model\Config\Source\SmtpSslType</source_model>
369+
<depends>
370+
<field id="transport">smtp</field>
371+
</depends>
372+
</field>
341373
</group>
342374
<group id="upload_configuration" translate="label" type="text" sortOrder="1000" showInDefault="1" showInWebsite="1" showInStore="1">
343375
<label>Images Upload Configuration</label>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Email\Model\Config\Source;
9+
10+
/**
11+
* Option provider for the SMTP Auth type
12+
*/
13+
class SmtpAuthType implements \Magento\Framework\Data\OptionSourceInterface
14+
{
15+
/**
16+
* The possible Auth types
17+
*
18+
* @codeCoverageIgnore
19+
* @return array
20+
*/
21+
public function toOptionArray(): array
22+
{
23+
return [
24+
['value' => 'none', 'label' => __('NONE')],
25+
['value' => 'plain', 'label' => __('PLAIN')],
26+
['value' => 'login', 'label' => __('LOGIN')],
27+
// ['value' => 'crammd5', 'label' => __('CRAM-MD5) '], // Requires laminas/laminas-crypt
28+
];
29+
}
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Email\Model\Config\Source;
9+
10+
/**
11+
* Option provider for SMTP SSL Type
12+
*/
13+
class SmtpSslType implements \Magento\Framework\Data\OptionSourceInterface
14+
{
15+
/**
16+
* The possible SSL types
17+
*
18+
* @codeCoverageIgnore
19+
* @return array
20+
*/
21+
public function toOptionArray(): array
22+
{
23+
return [
24+
['value' => 'none', 'label' => __('None')],
25+
['value' => 'ssl', 'label' => __('SSL')],
26+
['value' => 'tls', 'label' => __('TLS')],
27+
];
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Email\Model\Config\Source;
9+
10+
/**
11+
* Option provider for the SMTP Transport Type
12+
*/
13+
class SmtpTransportType implements \Magento\Framework\Data\OptionSourceInterface
14+
{
15+
16+
/**
17+
* The possible Transport types
18+
*
19+
* @codeCoverageIgnore
20+
* @return array
21+
*/
22+
public function toOptionArray(): array
23+
{
24+
return [
25+
['value' => 'sendmail', 'label' => __('Sendmail')],
26+
['value' => 'smtp', 'label' => __('SMTP')],
27+
];
28+
}
29+
}

app/code/Magento/Email/Model/Transport.php

Lines changed: 149 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Magento\Email\Model;
99

10+
use Laminas\Mail\Transport\Smtp;
11+
use Laminas\Mail\Transport\SmtpOptions;
1012
use Magento\Framework\App\Config\ScopeConfigInterface;
1113
use Magento\Framework\App\ObjectManager;
1214
use Magento\Framework\Exception\MailException;
@@ -16,24 +18,61 @@
1618
use Magento\Store\Model\ScopeInterface;
1719
use Laminas\Mail\Message;
1820
use Laminas\Mail\Transport\Sendmail;
21+
use Laminas\Mail\Transport\TransportInterface as LaminasTransportInterface;
1922
use Psr\Log\LoggerInterface;
2023

2124
/**
2225
* Class that responsible for filling some message data before transporting it.
2326
* @see \Laminas\Mail\Transport\Sendmail is used for transport
27+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2428
*/
2529
class Transport implements TransportInterface
2630
{
2731
/**
2832
* Configuration path to source of Return-Path and whether it should be set at all
2933
* @see \Magento\Config\Model\Config\Source\Yesnocustom to possible values
3034
*/
31-
const XML_PATH_SENDING_SET_RETURN_PATH = 'system/smtp/set_return_path';
35+
public const XML_PATH_SENDING_SET_RETURN_PATH = 'system/smtp/set_return_path';
3236

3337
/**
3438
* Configuration path for custom Return-Path email
3539
*/
36-
const XML_PATH_SENDING_RETURN_PATH_EMAIL = 'system/smtp/return_path_email';
40+
public const XML_PATH_SENDING_RETURN_PATH_EMAIL = 'system/smtp/return_path_email';
41+
42+
/**
43+
* Configuration path for custom Transport
44+
*/
45+
private const XML_PATH_TRANSPORT = 'system/smtp/transport';
46+
47+
/**
48+
* Configuration path for SMTP Host
49+
*/
50+
private const XML_PATH_HOST = 'system/smtp/host';
51+
52+
/**
53+
* Configuration path for SMTP Port
54+
*/
55+
private const XML_PATH_PORT = 'system/smtp/port';
56+
57+
/**
58+
* Configuration path for SMTP Username
59+
*/
60+
private const XML_PATH_USERNAME = 'system/smtp/username';
61+
62+
/**
63+
* Configuration path for SMTP Password
64+
*/
65+
private const XML_PATH_PASSWORD = 'system/smtp/password';
66+
67+
/**
68+
* Configuration path for SMTP Auth type
69+
*/
70+
private const XML_PATH_AUTH = 'system/smtp/auth';
71+
72+
/**
73+
* Configuration path for SMTP SSL value
74+
*/
75+
private const XML_PATH_SSL = 'system/smtp/ssl';
3776

3877
/**
3978
* Whether return path should be set or no.
@@ -53,10 +92,20 @@ class Transport implements TransportInterface
5392
private $returnPathValue;
5493

5594
/**
56-
* @var Sendmail
95+
* @var ScopeConfigInterface
96+
*/
97+
private $scopeConfig;
98+
99+
/**
100+
* @var LaminasTransportInterface|null
57101
*/
58102
private $laminasTransport;
59103

104+
/**
105+
* @var null|string|array|\Traversable
106+
*/
107+
private $parameters;
108+
60109
/**
61110
* @var MessageInterface
62111
*/
@@ -87,12 +136,35 @@ public function __construct(
87136
self::XML_PATH_SENDING_RETURN_PATH_EMAIL,
88137
ScopeInterface::SCOPE_STORE
89138
);
90-
91-
$this->laminasTransport = new Sendmail($parameters);
92139
$this->message = $message;
140+
$this->scopeConfig = $scopeConfig;
141+
$this->parameters = $parameters;
93142
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
94143
}
95144

145+
/**
146+
* Get the LaminasTransport based on the configuration.
147+
*
148+
* @return LaminasTransportInterface
149+
*/
150+
public function getTransport(): LaminasTransportInterface
151+
{
152+
if ($this->laminasTransport === null) {
153+
$transport = $this->scopeConfig->getValue(
154+
self::XML_PATH_TRANSPORT,
155+
ScopeInterface::SCOPE_STORE
156+
);
157+
158+
if ($transport === 'smtp') {
159+
$this->laminasTransport = $this->createSmtpTransport();
160+
} else {
161+
$this->laminasTransport = $this->createSendmailTransport();
162+
}
163+
}
164+
165+
return $this->laminasTransport;
166+
}
167+
96168
/**
97169
* @inheritdoc
98170
*/
@@ -108,7 +180,7 @@ public function sendMessage()
108180
$laminasMessage->setSender($fromAddressList->current()->getEmail());
109181
}
110182

111-
$this->laminasTransport->send($laminasMessage);
183+
$this->getTransport()->send($laminasMessage);
112184
} catch (\Exception $e) {
113185
$this->logger->error($e);
114186
throw new MailException(new Phrase('Unable to send mail. Please try again later.'), $e);
@@ -122,4 +194,75 @@ public function getMessage()
122194
{
123195
return $this->message;
124196
}
197+
198+
/**
199+
* Create a Smtp LaminasTransport.
200+
*
201+
* @return Smtp
202+
*/
203+
private function createSmtpTransport(): Smtp
204+
{
205+
$host = $this->scopeConfig->getValue(
206+
self::XML_PATH_HOST,
207+
ScopeInterface::SCOPE_STORE
208+
);
209+
210+
$port = $this->scopeConfig->getValue(
211+
self::XML_PATH_PORT,
212+
ScopeInterface::SCOPE_STORE
213+
);
214+
215+
$username = $this->scopeConfig->getValue(
216+
self::XML_PATH_USERNAME,
217+
ScopeInterface::SCOPE_STORE
218+
);
219+
220+
$password = $this->scopeConfig->getValue(
221+
self::XML_PATH_PASSWORD,
222+
ScopeInterface::SCOPE_STORE
223+
);
224+
225+
$auth = $this->scopeConfig->getValue(
226+
self::XML_PATH_AUTH,
227+
ScopeInterface::SCOPE_STORE
228+
);
229+
230+
$ssl = $this->scopeConfig->getValue(
231+
self::XML_PATH_SSL,
232+
ScopeInterface::SCOPE_STORE
233+
);
234+
235+
$options = [
236+
'name' => 'localhost',
237+
'host' => $host,
238+
'port' => $port,
239+
'connection_config' => [
240+
'username' => $username,
241+
'password' => $password,
242+
]
243+
];
244+
245+
if ($auth && $auth !== 'none') {
246+
$options['connection_class'] = $auth;
247+
}
248+
249+
if ($ssl && $ssl !== 'none') {
250+
$options['connection_config']['ssl'] = $ssl;
251+
}
252+
253+
$transport = new Smtp();
254+
$transport->setOptions(new SmtpOptions($options));
255+
256+
return $transport;
257+
}
258+
259+
/**
260+
* Create a Sendmail Laminas Transport
261+
*
262+
* @return Sendmail
263+
*/
264+
private function createSendmailTransport(): Sendmail
265+
{
266+
return new Sendmail($this->parameters);
267+
}
125268
}

app/code/Magento/Email/etc/config.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
<host>localhost</host>
2929
<port>25</port>
3030
<set_return_path>0</set_return_path>
31+
<transport>sendmail</transport>
32+
<auth>none</auth>
3133
</smtp>
3234
</system>
3335
<trans_email>

0 commit comments

Comments
 (0)