Skip to content

Commit f4844e5

Browse files
committed
MAGETWO-71349: Email Return-Path Setting is not used as Return-Path in Mail Transport
1 parent e669b04 commit f4844e5

File tree

7 files changed

+346
-63
lines changed

7 files changed

+346
-63
lines changed

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

Lines changed: 0 additions & 55 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
@@ -42,16 +42,23 @@
4242
class Template extends AbstractTemplate implements \Magento\Framework\Mail\TemplateInterface
4343
{
4444
/**
45-
* Configuration path for default email templates
45+
* Configuration path to source of Return-Path and whether it should be set at all
46+
* @deprecated
47+
* @see \Magento\Email\Model\Transport::XML_PATH_SENDING_SET_RETURN_PATH
4648
*/
4749
const XML_PATH_SENDING_SET_RETURN_PATH = 'system/smtp/set_return_path';
4850

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

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

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
* @throws \InvalidArgumentException when $message is not instance of \Zend_Mail
64+
*/
65+
public function __construct(
66+
\Zend_Mail_Transport_Sendmail $transport,
67+
MessageInterface $message,
68+
ScopeConfigInterface $scopeConfig
69+
) {
70+
if (!$message instanceof \Zend_Mail) {
71+
throw new \InvalidArgumentException('The message should be an instance of \Zend_Mail');
72+
}
73+
$this->transport = $transport;
74+
$this->message = $message;
75+
$this->scopeConfig = $scopeConfig;
76+
}
77+
78+
/**
79+
* Sets Return-Path to email if necessary, and sends email if it is allowed by System Configurations
80+
*
81+
* @return void
82+
* @throws \Magento\Framework\Exception\MailException
83+
*/
84+
public function sendMessage()
85+
{
86+
try {
87+
if (!$this->scopeConfig->isSetFlag(self::XML_PATH_SYSTEM_SMTP_DISABLE, ScopeInterface::SCOPE_STORE)) {
88+
/* configuration of whether return path should be set or no. Possible values are:
89+
* 0 - no
90+
* 1 - yes (set value as FROM address)
91+
* 2 - use custom value
92+
* @see Magento\Config\Model\Config\Source\Yesnocustom
93+
*/
94+
$isSetReturnPath = $this->scopeConfig->getValue(
95+
self::XML_PATH_SENDING_SET_RETURN_PATH,
96+
ScopeInterface::SCOPE_STORE
97+
);
98+
$returnPathValue = $this->scopeConfig->getValue(
99+
self::XML_PATH_SENDING_RETURN_PATH_EMAIL,
100+
ScopeInterface::SCOPE_STORE
101+
);
102+
103+
if ($isSetReturnPath == '1') {
104+
$this->message->setReturnPath($this->message->getFrom());
105+
} elseif ($isSetReturnPath == '2' && $returnPathValue !== null) {
106+
$this->message->setReturnPath($returnPathValue);
107+
}
108+
$this->transport->send($this->message);
109+
}
110+
} catch (\Exception $e) {
111+
throw new MailException(__($e->getMessage()), $e);
112+
}
113+
}
114+
115+
/**
116+
* @inheritdoc
117+
*/
118+
public function getMessage()
119+
{
120+
return $this->message;
121+
}
122+
}

0 commit comments

Comments
 (0)