Skip to content

Commit dbfeb36

Browse files
authored
ENGCOM-3968: [Backport][2.2] Alternative fix for Multi Store Emails issue, Fix Async Emails issues, Fix Multiple Email issues #18472
2 parents 6d41d59 + 48a2884 commit dbfeb36

File tree

6 files changed

+78
-68
lines changed

6 files changed

+78
-68
lines changed

app/code/Magento/Sales/Model/Order/Email/SenderBuilder.php

+3-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
namespace Magento\Sales\Model\Order\Email;
77

8-
use Magento\Framework\App\ObjectManager;
98
use Magento\Framework\Mail\Template\TransportBuilder;
109
use Magento\Framework\Mail\Template\TransportBuilderByStore;
1110
use Magento\Sales\Model\Order\Email\Container\IdentityInterface;
@@ -29,11 +28,8 @@ class SenderBuilder
2928
protected $transportBuilder;
3029

3130
/**
32-
* @var TransportBuilderByStore
33-
*/
34-
private $transportBuilderByStore;
35-
36-
/**
31+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
32+
*
3733
* @param Template $templateContainer
3834
* @param IdentityInterface $identityContainer
3935
* @param TransportBuilder $transportBuilder
@@ -48,9 +44,6 @@ public function __construct(
4844
$this->templateContainer = $templateContainer;
4945
$this->identityContainer = $identityContainer;
5046
$this->transportBuilder = $transportBuilder;
51-
$this->transportBuilderByStore = $transportBuilderByStore ?: ObjectManager::getInstance()->get(
52-
TransportBuilderByStore::class
53-
);
5447
}
5548

5649
/**
@@ -110,7 +103,7 @@ protected function configureEmailTemplate()
110103
$this->transportBuilder->setTemplateIdentifier($this->templateContainer->getTemplateId());
111104
$this->transportBuilder->setTemplateOptions($this->templateContainer->getTemplateOptions());
112105
$this->transportBuilder->setTemplateVars($this->templateContainer->getTemplateVars());
113-
$this->transportBuilderByStore->setFromByStore(
106+
$this->transportBuilder->setFromByScope(
114107
$this->identityContainer->getEmailIdentity(),
115108
$this->identityContainer->getStore()->getId()
116109
);

app/code/Magento/Sales/Test/Unit/Model/Order/Email/SenderBuilderTest.php

+14-13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
namespace Magento\Sales\Test\Unit\Model\Order\Email;
88

9-
use Magento\Framework\Mail\Template\TransportBuilderByStore;
109
use Magento\Sales\Model\Order\Email\SenderBuilder;
1110

1211
class SenderBuilderTest extends \PHPUnit\Framework\TestCase
@@ -36,11 +35,6 @@ class SenderBuilderTest extends \PHPUnit\Framework\TestCase
3635
*/
3736
private $storeMock;
3837

39-
/**
40-
* @var \PHPUnit_Framework_MockObject_MockObject
41-
*/
42-
private $transportBuilderByStore;
43-
4438
protected function setUp()
4539
{
4640
$templateId = 'test_template_id';
@@ -82,11 +76,10 @@ protected function setUp()
8276
'setTemplateIdentifier',
8377
'setTemplateOptions',
8478
'setTemplateVars',
79+
'setFromByScope',
8580
]
8681
);
8782

88-
$this->transportBuilderByStore = $this->createMock(TransportBuilderByStore::class);
89-
9083
$this->templateContainerMock->expects($this->once())
9184
->method('getTemplateId')
9285
->will($this->returnValue($templateId));
@@ -109,9 +102,9 @@ protected function setUp()
109102
$this->identityContainerMock->expects($this->once())
110103
->method('getEmailIdentity')
111104
->will($this->returnValue($emailIdentity));
112-
$this->transportBuilderByStore->expects($this->once())
113-
->method('setFromByStore')
114-
->with($this->equalTo($emailIdentity));
105+
$this->transportBuilder->expects($this->once())
106+
->method('setFromByScope')
107+
->with($this->equalTo($emailIdentity), 1);
115108

116109
$this->identityContainerMock->expects($this->once())
117110
->method('getEmailCopyTo')
@@ -120,15 +113,16 @@ protected function setUp()
120113
$this->senderBuilder = new SenderBuilder(
121114
$this->templateContainerMock,
122115
$this->identityContainerMock,
123-
$this->transportBuilder,
124-
$this->transportBuilderByStore
116+
$this->transportBuilder
125117
);
126118
}
127119

128120
public function testSend()
129121
{
130122
$customerName = 'test_name';
131123
$customerEmail = 'test_email';
124+
$identity = 'email_identity_test';
125+
132126
$transportMock = $this->createMock(
133127
\Magento\Sales\Test\Unit\Model\Order\Email\Stub\TransportInterfaceMock::class
134128
);
@@ -151,6 +145,9 @@ public function testSend()
151145
$this->storeMock->expects($this->once())
152146
->method('getId')
153147
->willReturn(1);
148+
$this->transportBuilder->expects($this->once())
149+
->method('setFromByScope')
150+
->with($identity, 1);
154151
$this->transportBuilder->expects($this->once())
155152
->method('addTo')
156153
->with($this->equalTo($customerEmail), $this->equalTo($customerName));
@@ -164,6 +161,7 @@ public function testSend()
164161

165162
public function testSendCopyTo()
166163
{
164+
$identity = 'email_identity_test';
167165
$transportMock = $this->createMock(
168166
\Magento\Sales\Test\Unit\Model\Order\Email\Stub\TransportInterfaceMock::class
169167
);
@@ -177,6 +175,9 @@ public function testSendCopyTo()
177175
$this->transportBuilder->expects($this->once())
178176
->method('addTo')
179177
->with($this->equalTo('example@mail.com'));
178+
$this->transportBuilder->expects($this->once())
179+
->method('setFromByScope')
180+
->with($identity, 1);
180181
$this->identityContainerMock->expects($this->once())
181182
->method('getStore')
182183
->willReturn($this->storeMock);

lib/internal/Magento/Framework/Mail/Message.php

+14-1
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,23 @@ public function getBody()
8989

9090
/**
9191
* @inheritdoc
92+
*
93+
* @deprecated This function is missing the from name. The
94+
* setFromAddress() function sets both from address and from name.
95+
* @see setFromAddress()
9296
*/
9397
public function setFrom($fromAddress)
9498
{
95-
$this->zendMessage->setFrom($fromAddress);
99+
$this->setFromAddress($fromAddress, null);
100+
return $this;
101+
}
102+
103+
/**
104+
* @inheritdoc
105+
*/
106+
public function setFromAddress($fromAddress, $fromName = null)
107+
{
108+
$this->zendMessage->setFrom($fromAddress, $fromName);
96109
return $this;
97110
}
98111

lib/internal/Magento/Framework/Mail/Template/TransportBuilder.php

+18-17
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,6 @@ class TransportBuilder
4848
*/
4949
protected $templateOptions;
5050

51-
/**
52-
* Mail from address
53-
*
54-
* @var string|array
55-
*/
56-
private $from;
57-
5851
/**
5952
* Mail Transport
6053
*
@@ -180,12 +173,29 @@ public function setReplyTo($email, $name = null)
180173
/**
181174
* Set mail from address
182175
*
176+
* @deprecated This function sets the from address but does not provide
177+
* a way of setting the correct from addresses based on the scope.
178+
* @see setFromByScope()
179+
*
183180
* @param string|array $from
184181
* @return $this
185182
*/
186183
public function setFrom($from)
187184
{
188-
$this->from = $from;
185+
return $this->setFromByScope($from, null);
186+
}
187+
188+
/**
189+
* Set mail from address by Scope
190+
*
191+
* @param string|array $from
192+
* @param string|int $scopeId
193+
* @return $this
194+
*/
195+
public function setFromByScope($from, $scopeId = null)
196+
{
197+
$result = $this->_senderResolver->resolve($from, $scopeId);
198+
$this->message->setFromAddress($result['email'], $result['name']);
189199
return $this;
190200
}
191201

@@ -262,7 +272,6 @@ protected function reset()
262272
$this->templateIdentifier = null;
263273
$this->templateVars = null;
264274
$this->templateOptions = null;
265-
$this->from = null;
266275
return $this;
267276
}
268277

@@ -296,14 +305,6 @@ protected function prepareMessage()
296305
->setBody($body)
297306
->setSubject(html_entity_decode($template->getSubject(), ENT_QUOTES));
298307

299-
if ($this->from) {
300-
$from = $this->_senderResolver->resolve(
301-
$this->from,
302-
$template->getDesignConfig()->getStore()
303-
);
304-
$this->message->setFrom($from['email'], $from['name']);
305-
}
306-
307308
return $this;
308309
}
309310
}

lib/internal/Magento/Framework/Mail/Template/TransportBuilderByStore.php

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88

99
use Magento\Framework\Mail\MessageInterface;
1010

11+
/**
12+
* Class TransportBuilderByStore
13+
*
14+
* @deprecated The ability to set From address based on store is now available
15+
* in the \Magento\Framework\Mail\Template\TransportBuilder class
16+
* @see \Magento\Framework\Mail\Template\TransportBuilder::setFromByScope
17+
*/
1118
class TransportBuilderByStore
1219
{
1320
/**

lib/internal/Magento/Framework/Mail/Test/Unit/Template/TransportBuilderTest.php

+22-27
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
namespace Magento\Framework\Mail\Test\Unit\Template;
88

99
use Magento\Framework\App\TemplateTypesInterface;
10-
use Magento\Framework\DataObject;
1110
use Magento\Framework\Mail\MessageInterface;
1211

1312
/**
@@ -100,37 +99,17 @@ protected function setUp()
10099
*/
101100
public function testGetTransport($templateType, $messageType, $bodyText, $templateNamespace)
102101
{
102+
$this->builder->setTemplateModel($templateNamespace);
103+
103104
$vars = ['reason' => 'Reason', 'customer' => 'Customer'];
104105
$options = ['area' => 'frontend', 'store' => 1];
105-
$from = 'email_from';
106-
$sender = ['email' => 'from@example.com', 'name' => 'name'];
107-
108-
$this->builder->setTemplateModel($templateNamespace);
109-
$this->builder->setFrom($from);
110106

111-
$template = $this->createPartialMock(
112-
\Magento\Framework\Mail\TemplateInterface::class,
113-
[
114-
'setVars',
115-
'isPlain',
116-
'setOptions',
117-
'getSubject',
118-
'getType',
119-
'processTemplate',
120-
'getDesignConfig',
121-
]
122-
);
107+
$template = $this->createMock(\Magento\Framework\Mail\TemplateInterface::class);
123108
$template->expects($this->once())->method('setVars')->with($this->equalTo($vars))->willReturnSelf();
124109
$template->expects($this->once())->method('setOptions')->with($this->equalTo($options))->willReturnSelf();
125110
$template->expects($this->once())->method('getSubject')->willReturn('Email Subject');
126111
$template->expects($this->once())->method('getType')->willReturn($templateType);
127112
$template->expects($this->once())->method('processTemplate')->willReturn($bodyText);
128-
$template->method('getDesignConfig')->willReturn(new DataObject($options));
129-
130-
$this->senderResolverMock->expects($this->once())
131-
->method('resolve')
132-
->with($from, 1)
133-
->willReturn($sender);
134113

135114
$this->templateFactoryMock->expects($this->once())
136115
->method('get')
@@ -149,9 +128,6 @@ public function testGetTransport($templateType, $messageType, $bodyText, $templa
149128
->method('setBody')
150129
->with($this->equalTo($bodyText))
151130
->willReturnSelf();
152-
$this->messageMock->method('setFrom')
153-
->with($sender['email'], $sender['name'])
154-
->willReturnSelf();
155131

156132
$transport = $this->createMock(\Magento\Framework\Mail\TransportInterface::class);
157133

@@ -184,6 +160,25 @@ public function getTransportDataProvider()
184160
]
185161
];
186162
}
163+
164+
/**
165+
* @return void
166+
*/
167+
public function testSetFromByScope()
168+
{
169+
$sender = ['email' => 'from@example.com', 'name' => 'name'];
170+
$scopeId = 1;
171+
$this->senderResolverMock->expects($this->once())
172+
->method('resolve')
173+
->with($sender, $scopeId)
174+
->willReturn($sender);
175+
$this->messageMock->expects($this->once())
176+
->method('setFromAddress')
177+
->with('from@example.com', 'name')
178+
->willReturnSelf();
179+
180+
$this->builder->setFromByScope($sender, $scopeId);
181+
}
187182

188183
/**
189184
* @return void

0 commit comments

Comments
 (0)