{"id":589,"date":"2019-09-09T08:33:08","date_gmt":"2019-09-09T08:33:08","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/09\/09\/send-custom-emails-programmatically-in-magento-2\/"},"modified":"2025-05-22T16:01:33","modified_gmt":"2025-05-22T10:31:33","slug":"send-custom-emails-programmatically-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/send-custom-emails-programmatically-in-magento-2\/","title":{"rendered":"How to Send Custom Emails Programmatically in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Emails are a traditional but effective way of communication for business. According to <a href=\"https:\/\/blog.hubspot.com\/marketing\/email-marketing-stats\" target=\"_blank\" rel=\"noreferrer noopener\">Hubspot<\/a>,&nbsp;active Email accounts are expected to hit 5.6 billion by 2019.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This clearly shows that when there are more platforms to communicate with the audience than ever before, the Email is still a popular platform.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Magento 2 store owners can make the most out of it by sending various types of Emails such as<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Acknowledgment Emails<\/li>\n\n\n\n<li>Marketing Emails<\/li>\n\n\n\n<li>Transactional Emails<\/li>\n\n\n\n<li>Newsletters\/Informative Emails<\/li>\n\n\n\n<li>Milestone Emails<\/li>\n\n\n\n<li>Plain text Emails<\/li>\n\n\n\n<li>Lead Nurturing Emails<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">However, the default Magento 2 does not support such a wide variety of Emails. It only supports:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Welcome Email<\/li>\n\n\n\n<li>Forgot Password Email<\/li>\n\n\n\n<li><a href=\"https:\/\/meetanshi.com\/blog\/set-up-order-confirmation-email-in-magento-2\/\">Order Confirmation Email<\/a><\/li>\n\n\n\n<li>Invoice Email<\/li>\n\n\n\n<li>Shipment Email<\/li>\n\n\n\n<li>Credit Memo Email<\/li>\n\n\n\n<li>Newsletter subscription Email<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">However, when you want to send personalized emails like offers, birthday wishes to customers, etc, you can use the method given here to send custom Emails programmatically in Magento 2.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Send Custom Emails Programmatically in Magento 2:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace [Vendor]\\[Module]\\Helper;\n\nuse Magento\\Framework\\App\\Helper\\Context;\nuse Magento\\Framework\\Mail\\Template\\TransportBuilder;\nuse Magento\\Framework\\App\\Helper\\AbstractHelper;\nuse Magento\\Framework\\Translate\\Inline\\StateInterface;\nuse Magento\\Store\\Model\\StoreManagerInterface;\n\nclass Data extends AbstractHelper\n{\n    protected $transportBuilder;\n    protected $storeManager;\n    protected $inlineTranslation;\n\n    public function __construct(\n        Context $context,\n        TransportBuilder $transportBuilder,\n        StoreManagerInterface $storeManager,\n        StateInterface $state\n    )\n    {\n        $this->transportBuilder = $transportBuilder;\n        $this->storeManager = $storeManager;\n        $this->inlineTranslation = $state;\n        parent::__construct($context);\n    }\n\n    public function sendEmail()\n    {\n        \/\/ this is an example and you can change template id,fromEmail,toEmail,etc as per your need.\n        $templateId = 'my_custom_email_template'; \/\/ template id\n        $fromEmail = 'owner@domain.com';  \/\/ sender Email id\n        $fromName = 'Admin';             \/\/ sender Name\n        $toEmail = 'customer@email.com'; \/\/ receiver email id\n\n        try {\n            \/\/ template variables pass here\n            $templateVars = [\n                'msg' => 'test',\n                'msg1' => 'test1'\n            ];\n\n            $storeId = $this->storeManager->getStore()->getId();\n\n            $from = ['email' => $fromEmail, 'name' => $fromName];\n            $this->inlineTranslation->suspend();\n\n            $storeScope = \\Magento\\Store\\Model\\ScopeInterface::SCOPE_STORE;\n            $templateOptions = [\n                'area' => \\Magento\\Framework\\App\\Area::AREA_FRONTEND,\n                'store' => $storeId\n            ];\n            $transport = $this->transportBuilder->setTemplateIdentifier($templateId, $storeScope)\n                ->setTemplateOptions($templateOptions)\n                ->setTemplateVars($templateVars)\n                ->setFrom($from)\n                ->addTo($toEmail)\n                ->getTransport();\n            $transport->sendMessage();\n            $this->inlineTranslation->resume();\n        } catch (\\Exception $e) {\n            $this->_logger->info($e->getMessage());\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> The above solution is for the Magento 2.2.x and Magento 2.3.x versions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s another useful solution to <a href=\"https:\/\/meetanshi.com\/blog\/add-birthday-field-to-checkout-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">add birthday field to checkout in Magento 2<\/a>, which you can use to collect birthday information from customers for sending wishes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Take the maximum benefit of emails by sending custom emails that you can think of using this method!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the solution with fellow developers on social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.   Keep Emailing!&nbsp; <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Emails are a traditional but effective way of communication for business. According to Hubspot,&nbsp;active Email accounts are expected to hit 5.6 billion by 2019. This&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-589","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/589","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=589"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/589\/revisions"}],"predecessor-version":[{"id":15278,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/589\/revisions\/15278"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=589"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}