I'm wondering if there is any simpler solution in sending specific order state emails.
Currently i use the following code to send e.g. the "order delivery shipped" email again.
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('orderNumber', $orderNumber));
$criteria->addAssociation('orderCustomer.salutation');
$criteria->addAssociation('orderCustomer.customer');
$criteria->addAssociation('stateMachineState');
$criteria->addAssociation('deliveries.shippingMethod');
$criteria->addAssociation('deliveries.shippingOrderAddress.country');
$criteria->addAssociation('deliveries.shippingOrderAddress.countryState');
$criteria->addAssociation('salesChannel');
$criteria->addAssociation('language.locale');
$criteria->addAssociation('transactions.paymentMethod');
$criteria->addAssociation('lineItems');
$criteria->addAssociation('currency');
$criteria->addAssociation('addresses.country');
$criteria->addAssociation('addresses.countryState');
/** @var OrderEntity|null $order */
$order = $this->orderRepository->search($criteria, $context)->first();
$context = new Context(new SystemSource());
$salesChannelContext = $this->orderConverter->assembleSalesChannelContext($order, $context)->getContext();
$salesChannelContext->addExtension(
MailSendSubscriber::MAIL_CONFIG_EXTENSION,
new MailSendSubscriberConfig(false)
);
$event = new OrderStateMachineStateChangeEvent(
'state_enter.order_delivery.state.shipped',
$order,
$salesChannelContext
);
$flowEvent = new FlowEvent('action.mail.send', new FlowState($event), [
'recipient' => [
'data' => [],
'type' => 'default',
],
'mailTemplateId' => $mailTemplateId,
]);
$this->sendMailAction->handle($flowEvent);
