In magento 2, there is some small modifications done to send transactional emails. You can find the sample code in the git repo.
Add the files as per the blog post. First of all we have to define the email template. etc/email_templates.xml file is used to define the email template. The code below will add an email template vendorname_samplemodule_email_template_sample with the email html in view/frontend/email/sample_email.html to the system.
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd"> <template id="vendorname_samplemodule_email_template_sample" label="Sample Email" file="sample_email.html" type="html" module="VendorName_SampleModule" area="frontend"/> </config>
In etc/adminhtml/system.xml add the identity and template for sending the email. In etc/config.xml, add the default identity and template for the email to be send. The below code is for sending an email using the above email template.
<?php namespace VendorName\SampleModule\Helper; use Magento\Framework\App\Helper\Context; use Magento\Framework\App\Area; use Magento\Framework\Mail\Template\TransportBuilder; class Email extends Data { //construct params protected $_transportBuilder; public function __construct(Context $context, TransportBuilder $transportBuilder) { $this->_transportBuilder = $transportBuilder; parent::__construct($context); } public function sendEmail() { $templateId = $this->getConfig('template'); $identity = $this->getConfig('identity'); $storeid = 1; $vars = array(); $tomail = 'testing@testing.com'; $toname = 'testing testing'; if ($templateId && $identity) { $transport = $this->_transportBuilder ->setTemplateIdentifier($templateId) ->setTemplateOptions(['area' => Area::AREA_FRONTEND, 'store' => $storeid]) ->setTemplateVars($vars) ->setFrom($identity) ->addTo($tomail, $toname) ->getTransport(); $transport->sendMessage(); } return $this; } }
Comments
Post a Comment