i want to send order_confirmation,order delivery templates and custom email template created in admin panel programmatically . Can anyone help me on this
Hello @tippanna_pawar ,
If you want to send emails programmatically then you can use the following code,
<?php namespace [Vendor]\[Module]\Helper; use Magento\Framework\App\Helper\Context; use Magento\Framework\Mail\Template\TransportBuilder; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\Translate\Inline\StateInterface; use Magento\Store\Model\StoreManagerInterface; class Data extends AbstractHelper { protected $transportBuilder; protected $storeManager; protected $inlineTranslation; public function __construct( Context $context, TransportBuilder $transportBuilder, StoreManagerInterface $storeManager, StateInterface $state ) { $this->transportBuilder = $transportBuilder; $this->storeManager = $storeManager; $this->inlineTranslation = $state; parent::__construct($context); } public function sendEmail() { // this is an example and you can change template id,fromEmail,toEmail,etc as per your need. $templateId = 'my_custom_email_template'; // template id $fromEmail = 'owner@domain.com'; // sender Email id $fromName = 'Admin'; // sender Name $toEmail = 'customer@email.com'; // receiver email id try { // template variables pass here $templateVars = [ 'msg' => 'test', 'msg1' => 'test1' ]; $storeId = $this->storeManager->getStore()->getId(); $from = ['email' => $fromEmail, 'name' => $fromName]; $this->inlineTranslation->suspend(); $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE; $templateOptions = [ 'area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $storeId ]; $transport = $this->transportBuilder->setTemplateIdentifier($templateId, $storeScope) ->setTemplateOptions($templateOptions) ->setTemplateVars($templateVars) ->setFrom($from) ->addTo($toEmail) ->getTransport(); $transport->sendMessage(); $this->inlineTranslation->resume(); } catch (\Exception $e) { $this->_logger->info($e->getMessage()); } } }
Thank You.
Problem solved? click kudos and accept as a solution
i want to send magento default transactional emails(order placed, invoice, shipment) programmatically..is that possible please help me?