1

I'm trying to send programmatically transitional emails that I created in the backend.

I did some research and saw many examples of sending emails programmatically only for those in html format.

How can I send my emails created in the backend programmatically?

Thank you

1

This an example of how Magento handles the shipment email.

Path to file: \vendor\magento\module-sales\Model\Order\Email\Sender\ShipmentSender.php

public function send(Shipment $shipment, $forceSyncMode = false)
    {
        $shipment->setSendEmail($this->identityContainer->isEnabled());

        if (!$this->globalConfig->getValue('sales_email/general/async_sending') || $forceSyncMode) {
            $order = $shipment->getOrder();
            $this->identityContainer->setStore($order->getStore());

            $transport = [
                'order' => $order,
                'shipment' => $shipment,
                'comment' => $shipment->getCustomerNoteNotify() ? $shipment->getCustomerNote() : '',
                'billing' => $order->getBillingAddress(),
                'payment_html' => $this->getPaymentHtml($order),
                'store' => $order->getStore(),
                'formattedShippingAddress' => $this->getFormattedShippingAddress($order),
                'formattedBillingAddress' => $this->getFormattedBillingAddress($order),
                'order_data' => [
                    'customer_name' => $order->getCustomerName(),
                    'is_not_virtual' => $order->getIsNotVirtual(),
                    'email_customer_note' => $order->getEmailCustomerNote(),
                    'frontend_status_label' => $order->getFrontendStatusLabel()
                ]
            ];
            $transportObject = new DataObject($transport);

            /**
             * Event argument `transport` is @deprecated. Use `transportObject` instead.
             */
            $this->eventManager->dispatch(
                'email_shipment_set_template_vars_before',
                ['sender' => $this, 'transport' => $transportObject->getData(), 'transportObject' => $transportObject]
            );

            $this->templateContainer->setTemplateVars($transportObject->getData());

            if ($this->checkAndSend($order)) {
                $shipment->setEmailSent(true);
                $this->shipmentResource->saveAttribute($shipment, ['send_email', 'email_sent']);
                return true;
            }
        } else {
            $shipment->setEmailSent(null);
            $this->shipmentResource->saveAttribute($shipment, 'email_sent');
        }

        $this->shipmentResource->saveAttribute($shipment, 'send_email');

        return false;
    }

I think you have to do something similar to this. Let me know if you need any further help.

UPDATE:

The class Magento\Sales\Model\Order\Email\Container is responsible to provide the template info so you need to create an instance of this class and pass it on to the send function above.

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Sales\Model\Order\Email\Container;

class Template
{
    /**
     * @var array
     */
    protected $vars;

    /**
     * @var array
     */
    protected $options;

    /**
     * @var string
     */
    protected $templateId;

    /**
     * @var int
     */
    protected $id;

    /**
     * Set email template variables
     *
     * @param array $vars
     * @return void
     */
    public function setTemplateVars(array $vars)
    {
        $this->vars = $vars;
    }

    /**
     * Set email template options
     *
     * @param array $options
     * @return void
     */
    public function setTemplateOptions(array $options)
    {
        $this->options = $options;
    }

    /**
     * Get email template variables
     *
     * @return array
     */
    public function getTemplateVars()
    {
        return $this->vars;
    }

    /**
     * Get email template options
     *
     * @return array
     */
    public function getTemplateOptions()
    {
        return $this->options;
    }

    /**
     * Set email template id
     *
     * @param int $id
     * @return void
     */
    public function setTemplateId($id)
    {
        $this->id = $id;
    }

    /**
     * Get email template id
     *
     * @return int
     */
    public function getTemplateId()
    {
        return $this->id;
    }
}
7
  • Hi man! Thanks for the reply. What I would like is to understand how to send a transitional email in my custom module by taking the id of the email created in the backend. In this example Magento takes template in html right?
    – Jackom
    Jul 15 '20 at 9:24
  • just updated my answer, let me know if it solved your problem.
    – Joao71
    Jul 15 '20 at 14:19
  • Okay, I'll let you know as soon as I tried. Thank you
    – Jackom
    Jul 15 '20 at 14:43
  • Don't mean to nag you but please, if this solved your issue mark it as answer, if not let me know how I can help ;)
    – Joao71
    Jul 22 '20 at 8:15
  • Sorry man, you're right, but I still haven't been able to test your code. As soon as I have done it, I will update you :)
    – Jackom
    Jul 22 '20 at 12:38

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.