Skip to content

Commit 5b82d1e

Browse files
committed
Add SMTP configuration + transport
1 parent 2e2af0a commit 5b82d1e

File tree

7 files changed

+273
-29
lines changed

7 files changed

+273
-29
lines changed

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

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -311,34 +311,6 @@
311311
<label>System</label>
312312
<tab>advanced</tab>
313313
<resource>Magento_Config::config_system</resource>
314-
<group id="smtp" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
315-
<label>Mail Sending Settings</label>
316-
<field id="disable" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
317-
<label>Disable Email Communications</label>
318-
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
319-
</field>
320-
<field id="host" translate="label comment" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
321-
<label>Host</label>
322-
<comment>For Windows server only.</comment>
323-
</field>
324-
<field id="port" translate="label comment" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
325-
<label>Port (25)</label>
326-
<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>
328-
</field>
329-
<field id="set_return_path" translate="label" type="select" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
330-
<label>Set Return-Path</label>
331-
<source_model>Magento\Config\Model\Config\Source\Yesnocustom</source_model>
332-
</field>
333-
<field id="return_path_email" translate="label" type="text" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="1">
334-
<label>Return-Path Email</label>
335-
<validate>validate-email</validate>
336-
<backend_model>Magento\Config\Model\Config\Backend\Email\Address</backend_model>
337-
<depends>
338-
<field id="set_return_path">2</field>
339-
</depends>
340-
</field>
341-
</group>
342314
<group id="upload_configuration" translate="label" type="text" sortOrder="1000" showInDefault="1" showInWebsite="1" showInStore="1">
343315
<label>Images Upload Configuration</label>
344316
<field id="enable_resize" translate="label comment" type="select" sortOrder="200" showInDefault="1" canRestore="1">
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 custom media URL type
12+
*/
13+
class SmtpAuthType implements \Magento\Framework\Data\OptionSourceInterface
14+
{
15+
/**
16+
* The 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 custom media URL type
12+
*/
13+
class SmtpSslType implements \Magento\Framework\Data\OptionSourceInterface
14+
{
15+
/**
16+
* The 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' => '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 custom media URL type
12+
*/
13+
class SmtpTransportType implements \Magento\Framework\Data\OptionSourceInterface
14+
{
15+
16+
/**
17+
* The the possible Auth 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: 111 additions & 1 deletion
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\Exception\MailException;
1214
use Magento\Framework\Mail\MessageInterface;
@@ -33,6 +35,41 @@ class Transport implements TransportInterface
3335
*/
3436
const XML_PATH_SENDING_RETURN_PATH_EMAIL = 'system/smtp/return_path_email';
3537

38+
/**
39+
* Configuration path for custom Transport
40+
*/
41+
const XML_PATH_TRANSPORT = 'system/smtp/transport';
42+
43+
/**
44+
* Configuration path for SMTP Host
45+
*/
46+
const XML_PATH_HOST = 'system/smtp/host';
47+
48+
/**
49+
* Configuration path for SMTP Port
50+
*/
51+
const XML_PATH_PORT = 'system/smtp/port';
52+
53+
/**
54+
* Configuration path for SMTP Username
55+
*/
56+
const XML_PATH_USERNAME = 'system/smtp/username';
57+
58+
/**
59+
* Configuration path for SMTP Password
60+
*/
61+
const XML_PATH_PASSWORD = 'system/smtp/password';
62+
63+
/**
64+
* Configuration path for SMTP Auth type
65+
*/
66+
const XML_PATH_AUTH = 'system/smtp/auth';
67+
68+
/**
69+
* Configuration path for SMTP SSL
70+
*/
71+
const XML_PATH_SSL = 'system/smtp/ssl';
72+
3673
/**
3774
* Whether return path should be set or no.
3875
*
@@ -79,7 +116,17 @@ public function __construct(
79116
ScopeInterface::SCOPE_STORE
80117
);
81118

82-
$this->laminasTransport = new Sendmail($parameters);
119+
$transport = $scopeConfig->getValue(
120+
self::XML_PATH_TRANSPORT,
121+
ScopeInterface::SCOPE_STORE
122+
);
123+
124+
if ($transport === 'smtp') {
125+
$this->laminasTransport = $this->createSmtpTransport($scopeConfig);
126+
} else {
127+
$this->laminasTransport = $this->createSendmailTransport($parameters);
128+
}
129+
83130
$this->message = $message;
84131
}
85132

@@ -111,4 +158,67 @@ public function getMessage()
111158
{
112159
return $this->message;
113160
}
161+
162+
/**
163+
* @param $parameters
164+
* @return Smtp
165+
*/
166+
private function createSmtpTransport($scopeConfig)
167+
{
168+
$host = $scopeConfig->getValue(
169+
self::XML_PATH_HOST,
170+
ScopeInterface::SCOPE_STORE
171+
);
172+
173+
$port = $scopeConfig->getValue(
174+
self::XML_PATH_PORT,
175+
ScopeInterface::SCOPE_STORE
176+
);
177+
178+
$username = $scopeConfig->getValue(
179+
self::XML_PATH_USERNAME,
180+
ScopeInterface::SCOPE_STORE
181+
);
182+
183+
$password = $scopeConfig->getValue(
184+
self::XML_PATH_PASSWORD,
185+
ScopeInterface::SCOPE_STORE
186+
);
187+
188+
$auth = $scopeConfig->getValue(
189+
self::XML_PATH_AUTH,
190+
ScopeInterface::SCOPE_STORE
191+
);
192+
193+
$ssl = $scopeConfig->getValue(
194+
self::XML_PATH_SSL,
195+
ScopeInterface::SCOPE_STORE
196+
);
197+
198+
$options = [
199+
'name' => 'localhost',
200+
'host' => $host,
201+
'port' => $port,
202+
'connection_class' => $auth,
203+
'connection_config' => [
204+
'username' => $username,
205+
'password' => $password,
206+
]
207+
];
208+
209+
if ($ssl && $ssl !== 'none') {
210+
$connectionConfig['ssl'] = $ssl;
211+
}
212+
213+
return new Smtp(new SmtpOptions($options));
214+
}
215+
216+
/**
217+
* @param $parameters
218+
* @return Sendmail
219+
*/
220+
private function createSendmailTransport($parameters)
221+
{
222+
return new Sendmail($parameters);
223+
}
114224
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
9+
<system>
10+
<section id="system">
11+
<group id="smtp" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
12+
<label>Mail Sending Settings</label>
13+
<field id="disable" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
14+
<label>Disable Email Communications</label>
15+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
16+
</field>
17+
<field id="transport" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
18+
<label>Transport</label>
19+
<source_model>Magento\Email\Model\Config\Source\SmtpTransportType</source_model>
20+
</field>
21+
<field id="host" translate="label comment" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
22+
<label>Host</label>
23+
<comment>For SMTP and Windows server only.</comment>
24+
</field>
25+
<field id="port" translate="label comment" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
26+
<label>Port (25)</label>
27+
<validate>validate-digits validate-digits-range digits-range-0-65535</validate>
28+
<comment>Please enter at least 0 and at most 65535 (For SMTP and Windows server only).</comment>
29+
</field>
30+
<field id="set_return_path" translate="label" type="select" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
31+
<label>Set Return-Path</label>
32+
<source_model>Magento\Config\Model\Config\Source\Yesnocustom</source_model>
33+
</field>
34+
<field id="return_path_email" translate="label" type="text" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="1">
35+
<label>Return-Path Email</label>
36+
<validate>validate-email</validate>
37+
<backend_model>Magento\Config\Model\Config\Backend\Email\Address</backend_model>
38+
<depends>
39+
<field id="set_return_path">2</field>
40+
</depends>
41+
</field>
42+
<field id="username" translate="label comment" type="text" sortOrder="90" showInDefault="1" showInWebsite="1" showInStore="1">
43+
<label>Username</label>
44+
<comment>Username</comment>
45+
<depends>
46+
<field id="transport">smtp</field>
47+
</depends>
48+
</field>
49+
<field id="password" translate="label comment" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
50+
<label>Password</label>
51+
<comment>Username</comment>
52+
<depends>
53+
<field id="transport">smtp</field>
54+
</depends>
55+
</field>
56+
<field id="auth" translate="label" type="select" sortOrder="110" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
57+
<label>Auth</label>
58+
<source_model>Magento\Email\Model\Config\Source\SmtpAuthType</source_model>
59+
<depends>
60+
<field id="transport">smtp</field>
61+
</depends>
62+
</field>
63+
<field id="ssl" translate="label" type="select" sortOrder="120" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
64+
<label>SSL</label>
65+
<source_model>Magento\Email\Model\Config\Source\SmtpSslType</source_model>
66+
<depends>
67+
<field id="transport">smtp</field>
68+
</depends>
69+
</field>
70+
</group>
71+
</section>
72+
</system>
73+
</config>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<host>localhost</host>
2929
<port>25</port>
3030
<set_return_path>0</set_return_path>
31+
<transport>sendmail</transport>
3132
</smtp>
3233
</system>
3334
<trans_email>

0 commit comments

Comments
 (0)