3

How to send transactional email template from Magento 2 root folder?

1

1 Answer 1

6

Developed a module for email setup by referring the blog for the requirement: https://webkul.com/blog/magento-2-send-transactional-email-programmatically-in-your-custom-module/

Here I have modified the helper class according the usage as follows:

<?php
namespace VendorName\CustomModule\Helper;

use Magento\Framework\App\Area;

class Emailreport extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
    * @var \Magento\Framework\App\Config\ScopeConfigInterface
    */
    protected $_scopeConfig;
    /**
    * Store manager
    *
    * @var \Magento\Store\Model\StoreManagerInterface
    */
    protected $_storeManager;
    /**
    * @var \Magento\Framework\Translate\Inline\StateInterface
    */
    protected $inlineTranslation;
    /**
    * @var \Magento\Framework\Mail\Template\TransportBuilder
    */
    protected $_transportBuilder;

    /**
    * @param Magento\Framework\App\Helper\Context $context
    * @param Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    * @param Magento\Store\Model\StoreManagerInterface $storeManager
    * @param Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
    * @param Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
    */
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
        \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
        \Magento\Framework\App\State $state
        ) {
        $this->_scopeConfig = $context;
        parent::__construct($context);
        $this->_storeManager = $storeManager;
        $this->inlineTranslation = $inlineTranslation;
        $this->_transportBuilder = $transportBuilder;
        $state->setAreaCode('frontend');
    }
    /**
    * @param string $path
    * @param int $storeId
    * @return mixed
    */
    protected function getConfigValue($path, $storeId)
    {
        return $this->scopeConfig->getValue(
            $path,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
            $storeId
            );
    }
    /**
    * Return store
    *
    * @return Store
    */
    public function getStore()
    {
        return $this->_storeManager->getStore();
    }

    public function sendEmailReport(
        $template,
        $senderInfo,
        $receiverInfo,
        $templateParams = []
        ) {
        $this->inlineTranslation->suspend();
        $templateId = $this->getConfigValue($template, $this->getStore()->getStoreId());

        $transport = $this->_transportBuilder->setTemplateIdentifier($templateId)
        ->setTemplateOptions(
            [
            'area' => Area::AREA_FRONTEND,
            'store' => $this->getStore()->getId(),
            ]
            )
        ->setTemplateVars($templateParams)
        ->setFrom($senderInfo)
        ->addTo($receiverInfo['email'],$receiverInfo['name'])
        ->getTransport();

        $transport->sendMessage();
        $this->inlineTranslation->resume();

        return $this;
    }
}

Now using the following code we can able to trigger the email from root folder:

<?php 
use \Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();

$customEmailPath = 'contact/email/email_template'; // section_id/group_id/field_id

/* Sender Detail */
$senderInfo = [
'name' => 'sender',
'email' => 'sender@domain.com'
];

/* Receiver Detail */
$receiverInfo = [
'name' => 'receiver',
'email' => 'receiver@domain.com'
];

/* To assign the values to template variables */

$customerDetails = array();
$customerDetails['name'] = 'John Doe';
$customerDetails['email'] = 'test@example.com';



$objectManager->get('VendorName\CustomModule\Helper\Emailreport')->sendEmailReport(
$customEmailPath,
$senderInfo,
$receiverInfo,
$customerDetails = []
);
7
  • what should we configure here----$customEmailPath = 'section_id/group_id/field_id';
    – Raghu
    Jul 12, 2017 at 10:36
  • For instance if we need the email template format of new sales order email give 'sales_email/order/template' . This field can be identified from /vendor/magento/module-sales/etc/adminhtml/system.xml
    – Haritha
    Jul 12, 2017 at 10:43
  • we have given the transactional email ID there directly
    – Raghu
    Jul 12, 2017 at 10:47
  • but we are getting this error Fatal error: Uncaught Error: Call to protected method Namespace\Modulename\Helper\Emailreport::sendEmailReport() from context '' in customtest.php:33 Stack trace: #0 {main} thrown in customtest.php on line 33
    – Raghu
    Jul 12, 2017 at 10:47
  • Please check the updated code.
    – Haritha
    Jul 12, 2017 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.