Magento 2 Send Email Programmatically

Magento 2 send email programmatically – In this post, let’s find out how to send email programmatically in Magento 2. It will be useful for you in your custom module or new feature. What we need to do:

  • Create your email template in a html file.
  • Declare your email template.
  • Create a function to send your email.

We’re doing step by step:

Create new html file


with simple content:

Declare your email template, create xml file


with content:

Finally, create a function to send your email.

In this tutorial, we’re using a controller action. Create file:


with content:

You also can create this function in helper, model …

If you have any question, just leave a comment or contact us.


Magento 2 Send Email Programmatically
5 (100%) 1 vote

Written By Metagento

3 Comments on “Magento 2 Send Email Programmatically

  1. G. Krang Reply

    August 31, 2016 at 4:59


    Thank you very much for posting this tutorial.
    I am a Magento 2 newbie, and I have been looking for a complete and working solution like this to send an email programmatically.

    I know that this isn’t a question and answer forum, but using the code you posted as an example, could you tell me how it might be possible to send an email from a .phtml file? I made a custom success.phtml file which works fine except for the email part, which I included in it. Since I am a newbie, I assumed that that is where I should have placed it. Should I place the Sendemail.php in the .phtml file or call it somehow? If I need to call it from the .phtml file, what command do I need to use?

    Thank you very much.

    • HDD Reply

      September 20, 2016 at 4:07

      I think you already declared your template file and block using a layout file.
      So you should create send email function in your block and call it using: $block->sendEmail();
      Hope this could help.

  2. Pavlo Reply

    May 22, 2018 at 11:07

    Thanks ! All working well!

Leave a Reply

Your email address will not be published.