Magento 2 send email through admin panel

There are several requirements when we need to send an email from admin or front section, below the code explanation for the same. Firstly we need to create an email_templates.xml file under etc folder which will tell us about the area of our email and template path.

<config xmlns:xsi=""
	<template id="w3solver_invoice_email_template" label="Invoice"
		file="invoice.html" type="html" module="W3solver_Invoice" area="adminhtml" />

In above code area is adminhtml, if you are sending email from frontend then you need top mark it as area=’frontend’. We will be creating an entry in config.xml file to get the email via scopeConfig Interface. Create config.xml in etc folder and add below code.

<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">

There is another way to add the configuration, we can replace above code in config.xml by creating a direct configuration through system.xml which will let us to select the template also. After this we need to create a model file that will be extending \Magento\Sales\Model\Order\Email\Sender to send the email, below is the code for the same:-

namespace W3solver\Invoice\Model\Email;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\Translate\Inline\StateInterface;

class Sender extends \Magento\Sales\Model\Order\Email\Sender {

    protected $paymentHelper;
    protected $invoiceResource;
    protected $_pdfModel;
    protected $_customerModel;
    protected $_scopeConfig;
    protected $_transportBuilder;
    protected $_inlineTranslation;

    public function __construct(
    \Magento\Customer\Model\Customer $customerModel, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, TransportBuilder $transportBuilder, StateInterface $inlineTranslation
    ) {
        $this->_customerModel = $customerModel;
        $this->_transportBuilder = $transportBuilder;
        $this->_scopeConfig = $scopeConfig;
        $this->_inlineTranslation = $inlineTranslation;

    public function send($increment_id, $customerId) {

        if (isset($customerId)) {
            $customerData = $this->_customerModel->load($customerId);
            $name = $customerData['firstname'];
            $email = $customerData['email'];

/*This is the code to send email */
        try {
            $transport = $this->_transportBuilder
                        'area' => \Magento\Framework\App\Area::AREA_ADMINHTML,
                        'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                    ->setFrom(array("name" => "arushi", "email" => ""))

            return true;
        } catch (\Exception $e) {
            echo $e;
            throw new \Magento\Framework\Exception\MailException(__('Error in sending email'));


At the end don’t forget to have a template html file under view/adminhtml/email/invoice.html.


  1. It will show header.html not found error in email even email is sent

    ‘area’ => \Magento\Framework\App\Area::AREA_ADMINHTML, /* here you can defile area and
    store of template for which you prepare it */
    ‘store’ => $this->_storeManager->getStore()->getId(),


Leave a Comment.