0

How To send Order Email Programmatically In Magento 2.2.8

2

You can try this code:

    $email = 'example@example.com';

    $orderid = 100;

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $order = $objectManager->create('Magento\Sales\Model\Order')->load($orderid);

    $order->setCustomerEmail($email);
    if ($order) {
        try {
            $objectManager->create('\Magento\Sales\Model\OrderNotifier')->notify($order);
            echo 'You sent the order email.';
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            echo($e->getMessage());
        } catch (\Exception $e) {
            echo(__('We can\'t send the email order right now.'));
            echo($e->getMessage());
        }
    }
1

Something like this


use Magento\Sales\Model\Order\Email\Sender\OrderSender;



{
    /**
     * @var \Psr\Log\LoggerInterface
     */
    private $logger;
    /**
     * @var OrderSender
     */
    private $orderSender;
    /**
     * @param \Psr\Log\LoggerInterface $logger
     * @param OrderSender $orderSender
     */
    public function __construct(
        \Psr\Log\LoggerInterface $logger,
        OrderSender $orderSender
    ) {
        $this->logger = $logger;
        $this->orderSender = $orderSender;
    }
    /**
     * @return void
     */
    public function doStuff()
    {
        // load order however you would like
        $order = [load order logic]
        try {
            $this->orderSender->send($order);
        } catch (\Exception $e) {
            $this->logger->critical($e);
        }
    }
}
  • 1+ for response – Rk Rathod Jul 9 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.