Skip to content

Commit a4c37be

Browse files
authored
Merge pull request magento#1412 from magento-thunder/MAGETWO-71349
MAGETWO-71349: Email Return-Path Setting is not used as Return-Path in Mail Transport
2 parents 18372e3 + 9a35e94 commit a4c37be

File tree

7 files changed

+346
-59
lines changed

7 files changed

+346
-59
lines changed

app/code/Magento/Email/Model/Mail/TransportInterfacePlugin.php

Lines changed: 0 additions & 51 deletions
This file was deleted.

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,23 @@
4141
class Template extends AbstractTemplate implements \Magento\Framework\Mail\TemplateInterface
4242
{
4343
/**
44-
* Configuration path for default email templates
44+
* Configuration path to source of Return-Path and whether it should be set at all
45+
* @deprecated
46+
* @see \Magento\Email\Model\Transport::XML_PATH_SENDING_SET_RETURN_PATH
4547
*/
4648
const XML_PATH_SENDING_SET_RETURN_PATH = 'system/smtp/set_return_path';
4749

50+
/**
51+
* Configuration path for custom Return-Path email
52+
* @deprecated
53+
* @see \Magento\Email\Model\Transport::XML_PATH_SENDING_RETURN_PATH_EMAIL
54+
*/
4855
const XML_PATH_SENDING_RETURN_PATH_EMAIL = 'system/smtp/return_path_email';
4956

5057
/**
5158
* Config path to mail sending setting that shows if email communications are disabled
5259
* @deprecated
53-
* @see \Magento\Email\Model\Mail\TransportInterfacePlugin::XML_PATH_SYSTEM_SMTP_DISABLE
60+
* @see \Magento\Email\Model\Transport::XML_PATH_SYSTEM_SMTP_DISABLE
5461
*/
5562
const XML_PATH_SYSTEM_SMTP_DISABLE = 'system/smtp/disable';
5663

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Email\Model;
7+
8+
use Magento\Framework\App\Config\ScopeConfigInterface;
9+
use Magento\Framework\Exception\MailException;
10+
use Magento\Framework\Mail\MessageInterface;
11+
use Magento\Framework\Mail\TransportInterface;
12+
use Magento\Store\Model\ScopeInterface;
13+
14+
/**
15+
* Class that responsible for filling some message data before transporting it.
16+
* This class checks whether this email should be sent in the first place, based on System Configurations.
17+
* @see Zend_Mail_Transport_Sendmail is used for transport
18+
*/
19+
class Transport implements TransportInterface
20+
{
21+
/**
22+
* Config path to mail sending setting that shows if email communications are disabled
23+
*/
24+
const XML_PATH_SYSTEM_SMTP_DISABLE = 'system/smtp/disable';
25+
26+
/**
27+
* Configuration path to source of Return-Path and whether it should be set at all
28+
* @see \Magento\Config\Model\Config\Source\Yesnocustom to possible values
29+
*/
30+
const XML_PATH_SENDING_SET_RETURN_PATH = 'system/smtp/set_return_path';
31+
32+
/**
33+
* Configuration path for custom Return-Path email
34+
*/
35+
const XML_PATH_SENDING_RETURN_PATH_EMAIL = 'system/smtp/return_path_email';
36+
37+
/**
38+
* Object for sending eMails
39+
*
40+
* @var \Zend_Mail_Transport_Sendmail
41+
*/
42+
private $transport;
43+
44+
/**
45+
* Email message object that should be instance of \Zend_Mail
46+
*
47+
* @var MessageInterface
48+
*/
49+
private $message;
50+
51+
/**
52+
* Core store config
53+
*
54+
* @var ScopeConfigInterface
55+
*/
56+
private $scopeConfig;
57+
58+
/**
59+
* @param \Zend_Mail_Transport_Sendmail $transport
60+
* @param MessageInterface $message Email message object
61+
* @param ScopeConfigInterface $scopeConfig Core store config
62+
* @param string|array|\Zend_Config|null $parameters Config options for sendmail parameters
63+
*
64+
* @throws \InvalidArgumentException when $message is not an instance of \Zend_Mail
65+
*/
66+
public function __construct(
67+
\Zend_Mail_Transport_Sendmail $transport,
68+
MessageInterface $message,
69+
ScopeConfigInterface $scopeConfig
70+
) {
71+
if (!$message instanceof \Zend_Mail) {
72+
throw new \InvalidArgumentException('The message should be an instance of \Zend_Mail');
73+
}
74+
$this->transport = $transport;
75+
$this->message = $message;
76+
$this->scopeConfig = $scopeConfig;
77+
}
78+
79+
/**
80+
* Sets Return-Path to email if necessary, and sends email if it is allowed by System Configurations
81+
*
82+
* @return void
83+
* @throws MailException
84+
*/
85+
public function sendMessage()
86+
{
87+
try {
88+
if (!$this->scopeConfig->isSetFlag(self::XML_PATH_SYSTEM_SMTP_DISABLE, ScopeInterface::SCOPE_STORE)) {
89+
/* configuration of whether return path should be set or no. Possible values are:
90+
* 0 - no
91+
* 1 - yes (set value as FROM address)
92+
* 2 - use custom value
93+
* @see Magento\Config\Model\Config\Source\Yesnocustom
94+
*/
95+
$isSetReturnPath = $this->scopeConfig->getValue(
96+
self::XML_PATH_SENDING_SET_RETURN_PATH,
97+
ScopeInterface::SCOPE_STORE
98+
);
99+
$returnPathValue = $this->scopeConfig->getValue(
100+
self::XML_PATH_SENDING_RETURN_PATH_EMAIL,
101+
ScopeInterface::SCOPE_STORE
102+
);
103+
104+
if ($isSetReturnPath == '1') {
105+
$this->message->setReturnPath($this->message->getFrom());
106+
} elseif ($isSetReturnPath == '2' && $returnPathValue !== null) {
107+
$this->message->setReturnPath($returnPathValue);
108+
}
109+
$this->transport->send($this->message);
110+
}
111+
} catch (\Exception $e) {
112+
throw new MailException(__($e->getMessage()), $e);
113+
}
114+
}
115+
116+
/**
117+
* @inheritdoc
118+
*/
119+
public function getMessage()
120+
{
121+
return $this->message;
122+
}
123+
}

0 commit comments

Comments
 (0)