Skip to content

Commit 9668008

Browse files
committed
ACP2E-2734: Emails are failing to send
1 parent 3b30eba commit 9668008

File tree

4 files changed

+27
-28
lines changed

4 files changed

+27
-28
lines changed

app/code/Magento/Sales/Model/EmailSenderHandler.php

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ class EmailSenderHandler
2626
/**
2727
* Email sender model.
2828
*
29-
* @var \Magento\Sales\Model\Order\Email\Sender
29+
* @var Sender
3030
*/
3131
protected $emailSender;
3232

3333
/**
3434
* Entity resource model.
3535
*
36-
* @var \Magento\Sales\Model\ResourceModel\EntityAbstract
36+
* @var EntityAbstract
3737
*/
3838
protected $entityResource;
3939

@@ -47,7 +47,7 @@ class EmailSenderHandler
4747
/**
4848
* Global configuration storage.
4949
*
50-
* @var \Magento\Framework\App\Config\ScopeConfigInterface
50+
* @var ScopeConfigInterface
5151
*/
5252
protected $globalConfig;
5353

@@ -57,7 +57,7 @@ class EmailSenderHandler
5757
private $identityContainer;
5858

5959
/**
60-
* @var \Magento\Store\Model\StoreManagerInterface
60+
* @var StoreManagerInterface
6161
*/
6262
private $storeManager;
6363

@@ -73,11 +73,6 @@ class EmailSenderHandler
7373
*/
7474
private $modifyStartFromDate;
7575

76-
/**
77-
* @var int
78-
*/
79-
private int $maxSendAttempts;
80-
8176
/**
8277
* @param Sender $emailSender
8378
* @param EntityAbstract $entityResource
@@ -87,18 +82,16 @@ class EmailSenderHandler
8782
* @param StoreManagerInterface|null $storeManager
8883
* @param ValueFactory|null $configValueFactory
8984
* @param string|null $modifyStartFromDate
90-
* @param int $maxSendAttempts
9185
*/
9286
public function __construct(
93-
\Magento\Sales\Model\Order\Email\Sender $emailSender,
94-
\Magento\Sales\Model\ResourceModel\EntityAbstract $entityResource,
95-
AbstractCollection $entityCollection,
96-
\Magento\Framework\App\Config\ScopeConfigInterface $globalConfig,
97-
IdentityInterface $identityContainer = null,
98-
\Magento\Store\Model\StoreManagerInterface $storeManager = null,
99-
?ValueFactory $configValueFactory = null,
100-
?string $modifyStartFromDate = null,
101-
int $maxSendAttempts = null
87+
Sender $emailSender,
88+
EntityAbstract $entityResource,
89+
AbstractCollection $entityCollection,
90+
ScopeConfigInterface $globalConfig,
91+
IdentityInterface $identityContainer = null,
92+
StoreManagerInterface $storeManager = null,
93+
?ValueFactory $configValueFactory = null,
94+
?string $modifyStartFromDate = null,
10295
) {
10396
$this->emailSender = $emailSender;
10497
$this->entityResource = $entityResource;
@@ -108,11 +101,10 @@ public function __construct(
108101
$this->identityContainer = $identityContainer ?: ObjectManager::getInstance()
109102
->get(\Magento\Sales\Model\Order\Email\Container\NullIdentity::class);
110103
$this->storeManager = $storeManager ?: ObjectManager::getInstance()
111-
->get(\Magento\Store\Model\StoreManagerInterface::class);
104+
->get(StoreManagerInterface::class);
112105

113106
$this->configValueFactory = $configValueFactory ?: ObjectManager::getInstance()->get(ValueFactory::class);
114107
$this->modifyStartFromDate = $modifyStartFromDate ?: $this->modifyStartFromDate;
115-
$this->maxSendAttempts = $maxSendAttempts ?? 3;
116108
}
117109

118110
/**
@@ -139,6 +131,8 @@ public function sendEmails()
139131
/** @var \Magento\Store\Api\Data\StoreInterface[] $stores */
140132
$stores = $this->getStores(clone $this->entityCollection);
141133

134+
$maxSendAttempts = $this->globalConfig->getValue('sales_email/general/async_sending_attempts');
135+
142136
/** @var \Magento\Store\Model\Store $store */
143137
foreach ($stores as $store) {
144138
$this->identityContainer->setStore($store);
@@ -150,7 +144,7 @@ public function sendEmails()
150144

151145
/** @var \Magento\Sales\Model\AbstractModel $item */
152146
foreach ($entityCollection->getItems() as $item) {
153-
$sendAttempts = $item->getEmailSent() ?? -$this->maxSendAttempts;
147+
$sendAttempts = $item->getEmailSent() ?? -$maxSendAttempts;
154148
$isEmailSent = $this->emailSender->send($item, true);
155149

156150
if ($isEmailSent) {
@@ -183,7 +177,7 @@ private function getStores(
183177
$entityCollection->addAttributeToSelect('store_id')->getSelect()->group('store_id');
184178
/** @var \Magento\Sales\Model\EntityInterface $item */
185179
foreach ($entityCollection->getItems() as $item) {
186-
/** @var \Magento\Store\Model\StoreManagerInterface $store */
180+
/** @var StoreManagerInterface $store */
187181
$store = $this->storeManager->getStore($item->getStoreId());
188182
$stores[$item->getStoreId()] = $store;
189183
}

app/code/Magento/Sales/Test/Unit/Model/EmailSenderHandlerTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,17 @@ public function testExecute(
163163
?bool $emailSendingResult,
164164
?int $expectedIsEmailSent
165165
): void {
166-
$path = 'sales_email/general/async_sending';
167-
168166
$this->globalConfig
169167
->method('getValue')
170-
->withConsecutive([$path])
171-
->willReturnOnConsecutiveCalls($configValue);
168+
->willReturnCallback(function ($path) use ($configValue) {
169+
if ($path === 'sales_email/general/async_sending') {
170+
return $configValue;
171+
}
172+
if ($path === 'sales_email/general/async_sending_attempts') {
173+
return 3;
174+
}
175+
return null;
176+
});
172177

173178
if ($configValue) {
174179
$nowDate = date('Y-m-d H:i:s');

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<sales_email>
3737
<general>
3838
<async_sending>0</async_sending>
39+
<async_sending_attempts>3</async_sending_attempts>
3940
<sending_limit>50</sending_limit>
4041
</general>
4142
<order>

app/code/Magento/Sales/etc/di.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@
220220
<type name="Magento\Sales\Model\EmailSenderHandler">
221221
<arguments>
222222
<argument name="modifyStartFromDate" xsi:type="string">-1 day</argument>
223-
<argument name="maxSendAttempts" xsi:type="number">3</argument>
224223
</arguments>
225224
</type>
226225
<virtualType name="SalesOrderIndexGridSyncRemove" type="Magento\Sales\Observer\GridSyncRemoveObserver">

0 commit comments

Comments
 (0)