Skip to content

Commit 8a8b44b

Browse files
committed
ACP2E-2734: Emails are failing to send
1 parent e70af16 commit 8a8b44b

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

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

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
*/
66
namespace Magento\Sales\Model;
77

8+
use Magento\Framework\App\Config\ScopeConfigInterface;
89
use Magento\Framework\App\Config\ValueFactory;
910
use Magento\Framework\App\Config\ValueInterface;
1011
use Magento\Framework\App\ObjectManager;
1112
use Magento\Sales\Model\Order\Email\Container\IdentityInterface;
13+
use Magento\Sales\Model\Order\Email\Sender;
1214
use Magento\Sales\Model\ResourceModel\Collection\AbstractCollection;
15+
use Magento\Sales\Model\ResourceModel\EntityAbstract;
16+
use Magento\Store\Model\StoreManagerInterface;
1317

1418
/**
1519
* Sales emails sending
@@ -68,16 +72,18 @@ class EmailSenderHandler
6872
* @var string
6973
*/
7074
private $modifyStartFromDate;
75+
private int $maxSendAttempts;
7176

7277
/**
73-
* @param \Magento\Sales\Model\Order\Email\Sender $emailSender
74-
* @param \Magento\Sales\Model\ResourceModel\EntityAbstract $entityResource
78+
* @param Sender $emailSender
79+
* @param EntityAbstract $entityResource
7580
* @param AbstractCollection $entityCollection
76-
* @param \Magento\Framework\App\Config\ScopeConfigInterface $globalConfig
81+
* @param ScopeConfigInterface $globalConfig
7782
* @param IdentityInterface|null $identityContainer
78-
* @param \Magento\Store\Model\StoreManagerInterface|null $storeManager
83+
* @param StoreManagerInterface|null $storeManager
7984
* @param ValueFactory|null $configValueFactory
8085
* @param string|null $modifyStartFromDate
86+
* @param int $maxSendAttempts
8187
*/
8288
public function __construct(
8389
\Magento\Sales\Model\Order\Email\Sender $emailSender,
@@ -87,7 +93,8 @@ public function __construct(
8793
IdentityInterface $identityContainer = null,
8894
\Magento\Store\Model\StoreManagerInterface $storeManager = null,
8995
?ValueFactory $configValueFactory = null,
90-
?string $modifyStartFromDate = null
96+
?string $modifyStartFromDate = null,
97+
int $maxSendAttempts = null
9198
) {
9299
$this->emailSender = $emailSender;
93100
$this->entityResource = $entityResource;
@@ -101,6 +108,7 @@ public function __construct(
101108

102109
$this->configValueFactory = $configValueFactory ?: ObjectManager::getInstance()->get(ValueFactory::class);
103110
$this->modifyStartFromDate = $modifyStartFromDate ?: $this->modifyStartFromDate;
111+
$this->maxSendAttempts = $maxSendAttempts ?? 3;
104112
}
105113

106114
/**
@@ -112,7 +120,13 @@ public function sendEmails()
112120
{
113121
if ($this->globalConfig->getValue('sales_email/general/async_sending')) {
114122
$this->entityCollection->addFieldToFilter('send_email', ['eq' => 1]);
115-
$this->entityCollection->addFieldToFilter('email_sent', ['null' => true]);
123+
$this->entityCollection->addFieldToFilter(
124+
'email_sent',
125+
[
126+
['null' => true],
127+
['lteq' => -1]
128+
]
129+
);
116130
$this->filterCollectionByStartFromDate($this->entityCollection);
117131
$this->entityCollection->setPageSize(
118132
$this->globalConfig->getValue('sales_email/general/sending_limit')
@@ -132,9 +146,17 @@ public function sendEmails()
132146

133147
/** @var \Magento\Sales\Model\AbstractModel $item */
134148
foreach ($entityCollection->getItems() as $item) {
149+
$sendAttempts = $item->getEmailSent() ?? -$this->maxSendAttempts;
135150
$isEmailSent = $this->emailSender->send($item, true);
151+
152+
if ($isEmailSent) {
153+
$sendAttempts = 1;
154+
} else {
155+
$sendAttempts++;
156+
}
157+
136158
$this->entityResource->saveAttribute(
137-
$item->setEmailSent($isEmailSent),
159+
$item->setEmailSent($sendAttempts),
138160
'email_sent'
139161
);
140162
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
<column xsi:type="int" name="customer_group_id" unsigned="false" nullable="true" identity="false"/>
150150
<column xsi:type="int" name="edit_increment" unsigned="false" nullable="true" identity="false"
151151
comment="Edit Increment"/>
152-
<column xsi:type="smallint" name="email_sent" unsigned="true" nullable="true" identity="false"
152+
<column xsi:type="smallint" name="email_sent" unsigned="false" nullable="true" identity="false"
153153
comment="Email Sent"/>
154154
<column xsi:type="smallint" name="send_email" unsigned="true" nullable="true" identity="false"
155155
comment="Send Email"/>
@@ -708,7 +708,7 @@
708708
comment="Total Weight"/>
709709
<column xsi:type="decimal" name="total_qty" scale="4" precision="12" unsigned="false" nullable="true"
710710
comment="Total Qty"/>
711-
<column xsi:type="smallint" name="email_sent" unsigned="true" nullable="true" identity="false"
711+
<column xsi:type="smallint" name="email_sent" unsigned="false" nullable="true" identity="false"
712712
comment="Email Sent"/>
713713
<column xsi:type="smallint" name="send_email" unsigned="true" nullable="true" identity="false"
714714
comment="Send Email"/>
@@ -981,7 +981,7 @@
981981
identity="false" comment="Is Used For Refund"/>
982982
<column xsi:type="int" name="order_id" unsigned="true" nullable="false" identity="false"
983983
comment="Order ID"/>
984-
<column xsi:type="smallint" name="email_sent" unsigned="true" nullable="true" identity="false"
984+
<column xsi:type="smallint" name="email_sent" unsigned="false" nullable="true" identity="false"
985985
comment="Email Sent"/>
986986
<column xsi:type="smallint" name="send_email" unsigned="true" nullable="true" identity="false"
987987
comment="Send Email"/>
@@ -1278,7 +1278,7 @@
12781278
comment="Tax Amount"/>
12791279
<column xsi:type="int" name="order_id" unsigned="true" nullable="false" identity="false"
12801280
comment="Order ID"/>
1281-
<column xsi:type="smallint" name="email_sent" unsigned="true" nullable="true" identity="false"
1281+
<column xsi:type="smallint" name="email_sent" unsigned="false" nullable="true" identity="false"
12821282
comment="Email Sent"/>
12831283
<column xsi:type="smallint" name="send_email" unsigned="true" nullable="true" identity="false"
12841284
comment="Send Email"/>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@
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>
223224
</arguments>
224225
</type>
225226
<virtualType name="SalesOrderIndexGridSyncRemove" type="Magento\Sales\Observer\GridSyncRemoveObserver">

0 commit comments

Comments
 (0)