Menu Close
    Hire us Request quote Reading list Switch to dark mode

    Magento 2- Send transactional email programmatically in your custom module

    Here we learn how to send transactional email programmatically in your Magento 2 custom module.

    1# First we create a field in configuration from where we can select which email template will use.

    You can create configuration field according to your section and group in app/code/NameSpace/ModuleName/etc/adminhtml/system.xml file as follow


    2# Now create a email template configuration file at app/code/NameSpace/ModuleName/etc location file name should be email_templates.xml


    3# Now create email template file at app/code/NameSpace/ModuleName/view/adminhtml/email as name which you given in email_template.xml file . Here we will use  “your_email_template.html” name for email template


    4# Now template is ready and we will do code for send mail.
    Write send mail code on action from where you want to send this mail.


    5# Helper file where we define our custom send mail function


    Thanks 🙂

    . . .

    Comments (5)

    Add Your Comment

  • Manncorp Accounts
    Line 113 of the Email class you have ->setFrom($senderInfo)
    But you defined $senderInfo as an Array:
    /* Sender Detail */
    $senderInfo = [
    ‘name’ => ‘Sender Name’,
    ’email’ => ‘sender@addess.com’,

    Shouldn’t that be ->sentFrom($senderInfo[’email’],$senderInfo[‘name’]) ?

    If that’s not necessary, why not do the same thing here at line 114:
    ->addTo($receiverInfo)
    instead of ->addTo($receiverInfo[’email’],$receiverInfo[‘name’]) ?

  • Idham Choudry
    Do i have to configure the header and footer again or its automatically generated when i call {{template config_path=”design/email/header_template”}} ?
  • Gurpreet Dhanju
    $this->temp_id = $this->getTemplateId(self::XML_PATH_EMAIL_ORDER_APPROVED);

    What is this self::XML_PATH_EMAIL_ORDER_APPROVED in above line. It is never defined?

    • Abhishek Singh
      Hi,
      There was error. Now code is updated. you can check it. XML_PATH_EMAIL_ORDER_APPROVED replace by XML_PATH_EMAIL_TEMPLATE_FIELD
      Thanks
  • chetan141tiwari
    After this line
    $transport = $this->_transportBuilder->getTransport();

    it shows internal server error

  • Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just received your message and our expert will get back to you shortly.
    Send Again
    Close