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;
}
}