cancel
Showing results for 
Search instead for 
Did you mean: 

Send email from custom module in Magento2

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

Send email from custom module in Magento2

Hello All,

First Create a file in etc folder email_templates.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../Email/etc/email_templates.xsd">
<template id="template id" label="Email Form" file="file path" type="html" module="CompanyName_ModuleName" area="frontend"/>
</config>

Then create a controller file or helper file where you want to write a code for sending email

<?php

/**
 *
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace CompanyName\ModuleName\Controller\Adminhtml\Index;

class Sendmail extends \Magento\Framework\App\Action\Action
{

    /**
     * Recipient email config path
     */
    const XML_PATH_EMAIL_RECIPIENT = 'test/email/send_email';

    /**
     * @var \Magento\Framework\Mail\Template\TransportBuilder
     */
    protected $_transportBuilder;

    /**
     * @var \Magento\Framework\Translate\Inline\StateInterface
     */
    protected $inlineTranslation;

    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    protected $scopeConfig;

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;
    protected $_encryptor;

    /**
     * @var \Magento\Framework\Escaper
     */
    protected $_escaper;
    protected $_userFactory;

    public function __construct(
    \Magento\Framework\App\Action\Context $context, \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
        \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    )
    {
        parent::__construct($context);
        $this->_transportBuilder = $transportBuilder;
        $this->inlineTranslation = $inlineTranslation;
        $this->scopeConfig = $scopeConfig;
        $this->_escaper = $escaper;
    }

    public function execute()
    {
            $this->inlineTranslation->suspend();
            try {
                 /*Here we prepare data for our email*/
/* Receiver Info */
$receiverInfo =['Receiver Name'=>'Receiver Name','Receiver Email'=>'Receiver Email'];
/* Sender Info */
$senderInfo =['Sender Name'=>'Sender Name','Sender Email'=>'Sender Email'];
$templateVars =array('name'=>$name,'email'=>$email); $templateOptions = array( 'area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $this->_storeManager->getStore()->getId() ); $transport = $this->_transportBuilder ->setTemplateIdentifier('send_email_email_template') ->setTemplateOptions($templateOptions) ->setTemplateVars($templateVars) ->setFrom($senderInfo) ->addTo($receiverInfo['email'],$receiverInfo['name']) ->getTransport(); $transport->sendMessage(); $this->inlineTranslation->resume(); $this->messageManager->addSuccess(__('Mail Sent Successfully!')); $resultRedirect = $this->resultRedirectFactory->create(); $resultRedirect->setRefererOrBaseUrl(); return $resultRedirect; } catch (\Exception $e) { $this->inlineTranslation->resume(); $this->messageManager->addError(__($e->getMessage())); $resultRedirect = $this->resultRedirectFactory->create(); $resultRedirect->setRefererOrBaseUrl(); return $resultRedirect; } } } }

 

Certified Magento 2 Developer
Vivek Singh
3 REPLIES 3

Re: Send email in Magento2

Hello @Vivek Singh

 

To send an email we should use SMTP, so why we have to create this module?

Manish Mittal
https://www.manishmittal.com/

Re: Send email in Magento2

Hello @Manish Mittal

This is useful when we want to send email from particular module or creating a custom button for sending email.

 

Certified Magento 2 Developer
Vivek Singh

Re: Send email in Magento2

@Vivek Singh

 

Right, then please correct your description so this post will help to users. Thanks for sharing this useful post.

Manish Mittal
https://www.manishmittal.com/