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="http://www.w3.org/2001/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd">
	<template id="w3solver_invoice_email_template" label="Invoice"
		file="invoice.html" type="html" module="W3solver_Invoice" area="adminhtml" />
</config>

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="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <invoice>
            <send_email>
                <identity>general</identity>
                <receiver>general</receiver>
                <template>w3solver_invoice_email_template</template>
            </send_email>
        </invoice>
    </default>
</config>

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 {
            $this->_inlineTranslation->suspend();
            $transport = $this->_transportBuilder
            ->setTemplateIdentifier($this->_scopeConfig->getValue(
            'invoice/send_email/template'))
            ->setTemplateOptions([
                        'area' => \Magento\Framework\App\Area::AREA_ADMINHTML,
                        'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                    ])
                    ->setTemplateVars(array())
                    ->setFrom(array("name" => "arushi", "email" => "arushi.bansal@jispl.com"))
                    ->addTo("arushi.bansal@jispl.com")
                    ->getTransport();

            $transport->sendMessage();
            $this->_inlineTranslation->resume();
            return true;
        } catch (\Exception $e) {
            echo $e;
            exit();
            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.

Leave a Comment.