Tutorialsplane

Magento 2 Send email programmatically


Magento 2 Send email programmatically transactional emails– Sending emails is the important functionality of any framework, Magento2 has its own to handle the mail functionality. Here in this post we are going to explain how you can send transactional emails programmatically with or without template from your custom module.


Magento 2 Send email programmatically | Transactional Emails Example

You can use the following steps to send emails easily-

1. Create Template

Now create template with some sample text located at the below specified path.

/app/code/VendorName/ModuleName/view/frontend/email/modulename/myTemplate.html

Magento 2 Send email programmatically Template:

<!---email template--->
<div>
 <h2>Welcome To Tutorialsplane.com !!</h2>
 <p>Hello, Learn More @ tutorialsplane.com.</p> 
</div>

2. Create Config

Now create configuration file to declare template-

/app/code/VendorName/ModuleName/etc/email_templates.xml

Magento 2 Send email configuration Example:


<?xml version="1.0"??>
 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nonamespaceschemalocation="urn:Magento:module:Magento_Email:etc/email_templates.xsd">
    <template id="modulename_mytemplate" label="Test email sample" file="modulename/myTemplate.html" type="html" module="VendorName_ModuleName" area="frontend"></template>
</config>

3. Now Create Function To Send Email

Now create controller function to send email-

/app/code/VendorName/ModuleName/Controller/Index/SendCustomMail.php

Magento 2 Mail Function Example:

<?php namespace VendorName\ModuleName\Controller;
 
use Magento\Framework\App\RequestInterface;
 
class SendCustomMail extends \Magento\Framework\App\Action\Action
{
    /**
     * @var \Magento\Framework\App\Request\Http
     */
    protected $_request;
    /**
     * @var \Magento\Framework\Mail\Template\TransportBuilder
     */
    protected $_transportBuilder;
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;
 
    public function __construct(
        \Magento\Framework\App\Action\Context $context
        , \Magento\Framework\App\Request\Http $request
        , \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
        , \Magento\Store\Model\StoreManagerInterface $storeManager
    )
    {
        $this-?>_request = $request;
        $this->_transportBuilder = $transportBuilder;
        $this->_storeManager = $storeManager;
        parent::__constr
    parent::__construct($context);
    }
 
    public function execute()
    {
        $store = $this->_storeManager->getStore()->getId();
        $transport = $this->_transportBuilder->setTemplateIdentifier('modulename_mytemplate')
            ->setTemplateOptions(['area' => 'frontend', 'store' => $store])
            ->setTemplateVars(
                [
                    'store' => $this->_storeManager->getStore(),
                ]
            )
            ->setFrom('general')
            // you can config general email address in Store -> Configuration -> General -> Store Email Addresses
            ->addTo('john@example.com', 'John Dee')
            ->getTransport();
        $transport->sendMessage();
        return $this;
    }
}

So you set your preferences in email template such as – set email template identifier, customer email, store, set from etc.