Skip to content

Commit fb49e27

Browse files
committed
Initial commit
0 parents  commit fb49e27

File tree

13 files changed

+459
-0
lines changed

13 files changed

+459
-0
lines changed

Helper/Data.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Fruitcake\EmailAdvancedConfig\Helper;
4+
5+
6+
use Fruitcake\EmailAdvancedConfig\Model\Config\Source\SmtpTransportType;
7+
use Magento\Framework\App\Helper\AbstractHelper;
8+
use Magento\Framework\App\Helper\Context;
9+
use Magento\Store\Model\ScopeInterface;
10+
11+
12+
class Data extends AbstractHelper
13+
{
14+
const CONFIG_PATH = 'fruitcake_email_advanced/';
15+
16+
public function isEnabled(): bool
17+
{
18+
return $this->getConfig('smtp/transport') === SmtpTransportType::TRANSPORT_SMTP;
19+
}
20+
21+
/**
22+
* @param string $code
23+
*
24+
* @return mixed
25+
*/
26+
public function getConfig($code = '')
27+
{
28+
return $this->scopeConfig->getValue(self::CONFIG_PATH . $code, ScopeInterface::SCOPE_STORE, null);
29+
}
30+
}

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Fruitcake
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 Fruitcake\EmailAdvancedConfig\Model\Config\Source;
9+
10+
use Fruitcake\EmailAdvancedConfig\Model\Config\AdvancedConfig;
11+
12+
/**
13+
* Option provider for custom media URL type
14+
*/
15+
class SmtpAuthType implements \Magento\Framework\Data\OptionSourceInterface
16+
{
17+
/**
18+
* The the possible Auth types
19+
*
20+
* @codeCoverageIgnore
21+
* @return array
22+
*/
23+
public function toOptionArray(): array
24+
{
25+
return [
26+
['value' => 'none', 'label' => 'NONE'],
27+
['value' => 'plain', 'label' => 'PLAIN'],
28+
['value' => 'login', 'label' => 'LOGIN'],
29+
['value' => 'crammd5', 'label' => 'CRAM-MD5 (Require laminas/laminas-crypt)']
30+
];
31+
}
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 Fruitcake\EmailAdvancedConfig\Model\Config\Source;
9+
10+
use Fruitcake\EmailAdvancedConfig\Model\Config\AdvancedConfig;
11+
12+
/**
13+
* Option provider for custom media URL type
14+
*/
15+
class SmtpSslType implements \Magento\Framework\Data\OptionSourceInterface
16+
{
17+
/**
18+
* The the possible Auth types
19+
*
20+
* @codeCoverageIgnore
21+
* @return array
22+
*/
23+
public function toOptionArray(): array
24+
{
25+
return [
26+
['value' => 'none', 'label' => 'None'],
27+
['value' => 'ssl', 'label' => 'SSL'],
28+
['value' => 'tls', 'label' => 'TLS'],
29+
];
30+
}
31+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 Fruitcake\EmailAdvancedConfig\Model\Config\Source;
9+
10+
use Fruitcake\EmailAdvancedConfig\Model\Config\AdvancedConfig;
11+
12+
/**
13+
* Option provider for custom media URL type
14+
*/
15+
class SmtpTransportType implements \Magento\Framework\Data\OptionSourceInterface
16+
{
17+
public const TRANPORT_SENDMAIL = 'sendmail';
18+
public const TRANSPORT_SMTP = 'smtp';
19+
20+
/**
21+
* The the possible Auth types
22+
*
23+
* @codeCoverageIgnore
24+
* @return array
25+
*/
26+
public function toOptionArray(): array
27+
{
28+
return [
29+
['value' => self::TRANPORT_SENDMAIL, 'label' => 'Sendmail (Magento default)'],
30+
['value' => self::TRANSPORT_SMTP, 'label' => 'SMTP Configuration'],
31+
];
32+
}
33+
}

Model/SmtpTransport.php

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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 Fruitcake\EmailAdvancedConfig\Model;
9+
10+
use Fruitcake\EmailAdvancedConfig\Helper\Data;
11+
use Laminas\Mail\Transport\Smtp;
12+
use Laminas\Mail\Transport\SmtpOptions;
13+
use Magento\Framework\App\Config\ScopeConfigInterface;
14+
use Magento\Framework\Exception\MailException;
15+
use Magento\Framework\Mail\MessageInterface;
16+
use Magento\Framework\Mail\TransportInterface;
17+
use Magento\Framework\Phrase;
18+
use Magento\Store\Model\ScopeInterface;
19+
use Laminas\Mail\Message;
20+
use Laminas\Mail\Transport\Sendmail;
21+
22+
/**
23+
* Class that responsible for filling some message data before transporting it.
24+
* @see \Laminas\Mail\Transport\Smtp is used for transport
25+
* @see \Magento\Email\Model\Transport for the original Magento implementation. This overrides the construct
26+
*/
27+
class SmtpTransport implements TransportInterface
28+
{
29+
/**
30+
* Configuration path to source of Return-Path and whether it should be set at all
31+
* @see \Magento\Config\Model\Config\Source\Yesnocustom to possible values
32+
*/
33+
const XML_PATH_SENDING_SET_RETURN_PATH = 'system/smtp/set_return_path';
34+
35+
/**
36+
* Configuration path for custom Return-Path email
37+
*/
38+
const XML_PATH_SENDING_RETURN_PATH_EMAIL = 'system/smtp/return_path_email';
39+
40+
/**
41+
* Whether return path should be set or no.
42+
*
43+
* Possible values are:
44+
* 0 - no
45+
* 1 - yes (set value as FROM address)
46+
* 2 - use custom value
47+
*
48+
* @var int
49+
*/
50+
private $isSetReturnPath;
51+
52+
/**
53+
* @var string|null
54+
*/
55+
private $returnPathValue;
56+
57+
/**
58+
* @var Sendmail
59+
*/
60+
private $laminasTransport;
61+
62+
/**
63+
* @var MessageInterface
64+
*/
65+
private $message;
66+
67+
/** @var Data */
68+
private $helper;
69+
70+
/**
71+
* @param MessageInterface $message Email message object
72+
* @param ScopeConfigInterface $scopeConfig Core store config
73+
* @param null|string|array|\Traversable $parameters Config options for sendmail parameters
74+
*/
75+
public function __construct(
76+
MessageInterface $message,
77+
ScopeConfigInterface $scopeConfig,
78+
Data $helper,
79+
$parameters = null
80+
) {
81+
$this->isSetReturnPath = (int) $scopeConfig->getValue(
82+
self::XML_PATH_SENDING_SET_RETURN_PATH,
83+
ScopeInterface::SCOPE_STORE
84+
);
85+
$this->returnPathValue = $scopeConfig->getValue(
86+
self::XML_PATH_SENDING_RETURN_PATH_EMAIL,
87+
ScopeInterface::SCOPE_STORE
88+
);
89+
$this->helper = $helper;
90+
91+
$this->message = $message;
92+
93+
// When not enabled, fall back to Sendmail transport
94+
if (! $this->helper->isEnabled()) {
95+
throw new \RuntimeException('Should be enabled');
96+
$this->laminasTransport = new Sendmail($parameters);
97+
return;
98+
}
99+
100+
$options = [
101+
'name' => 'localhost',
102+
'host' => $this->helper->getConfig('smtp/host'),
103+
'port' => $this->helper->getConfig('smtp/port'),
104+
'connection_class' => $this->helper->getConfig('smtp/auth'),
105+
'connection_config' => [
106+
'username' => $this->helper->getConfig('smtp/username'),
107+
'password' => $this->helper->getConfig('smtp/password'),
108+
]
109+
];
110+
111+
$ssl = $this->helper->getConfig('smtp/ssl');
112+
113+
if ($ssl && $ssl !== 'none') {
114+
$connectionConfig['ssl'] = $ssl;
115+
}
116+
117+
$this->laminasTransport = new Smtp(new SmtpOptions($options));
118+
}
119+
120+
/**
121+
* @inheritdoc
122+
*/
123+
public function sendMessage()
124+
{
125+
try {
126+
$laminasMessage = Message::fromString($this->message->getRawMessage())->setEncoding('utf-8');
127+
if (2 === $this->isSetReturnPath && $this->returnPathValue) {
128+
$laminasMessage->setSender($this->returnPathValue);
129+
} elseif (1 === $this->isSetReturnPath && $laminasMessage->getFrom()->count()) {
130+
$fromAddressList = $laminasMessage->getFrom();
131+
$fromAddressList->rewind();
132+
$laminasMessage->setSender($fromAddressList->current()->getEmail());
133+
}
134+
135+
$this->laminasTransport->send($laminasMessage);
136+
} catch (\Exception $e) {
137+
throw new MailException(new Phrase($e->getMessage()), $e);
138+
}
139+
}
140+
141+
/**
142+
* @inheritdoc
143+
*/
144+
public function getMessage()
145+
{
146+
return $this->message;
147+
}
148+
}

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Magento2 Advanced Email
2+
3+
Adds SMTP User/Pass/Authentication to the default Magento2 smtp, nothing else.
4+
5+
## Install
6+
7+
```
8+
composer require fruitcake/magento2-email-advanced-config
9+
php bin/magento module:enable Fruitcake_EmailAdvancedConfig
10+
php bin/magento setup:upgrade
11+
```
12+
13+
## License
14+
15+
MIT

composer.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "fruitcake/magento2-email-advanced-config",
3+
"description": "Magento2 Advanced SMTP Configuration",
4+
"keywords": [
5+
"magento",
6+
"magento2",
7+
"smtp",
8+
"email"
9+
],
10+
"license": [
11+
"MIT"
12+
],
13+
"type": "magento2-module",
14+
"version": "1.0.0",
15+
"authors": [
16+
{
17+
"name": "Fruitcake",
18+
"email": "[email protected]"
19+
}
20+
],
21+
"require": {
22+
"php": "^7"
23+
},
24+
"autoload": {
25+
"files": [
26+
"registration.php"
27+
],
28+
"psr-4": {
29+
"Fruitcake\\EmailAdvancedConfig\\": ""
30+
}
31+
},
32+
"minimum-stability": "dev",
33+
"prefer-stable": true,
34+
"extra": {
35+
"branch-alias": {
36+
"dev-master": "1.0-dev"
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)