First You Need to Create Custom module then follow this steps. You Can Also Used in your module. I have shared a step by step guide to sending a custom mail from Magento 2 using a module.
Step 1.Create system.xml file
app/code/[NameSpace]/[ModuleName]/etc/adminhtml/system.xml
1 |
<section id="email_user" translate="label"><label>Label name</label> ana Namespace_Modulename::analinknumber_api_user_config <label>Send Email To Customer Configuration</label> <label>Select Email Sender</label> Magento\Config\Model\Config\Source\Email\Identity <label>Select Email Template</label> Email template chosen based on theme fallback when "Default" option is selected. Magento\Config\Model\Config\Source\Email\Template</section> |
Step 2. Create file xml file
app/code/[NameSpace]/[ModuleName]/etc/ location file name should be email_templates.xml
1 |
<!--?xml version="1.0"?--> |
3# Now create email template file at app/code/[NameSpace]/[ModuleName]/view/frontend/email as name which you given in email_template.xml file . Here we will use “email_template.html” name for email template
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<!--@subject {{trans "Your Message"}} @--> <!--@vars { "var store.frontend_name":"Store Frontend Name", "var customer_name":"Customer Name", "var mobile_no":"Customer Mobile", "var customer_email":"Cusetomer Email" } @--> {{template config_path="design/email/header_template"}} <table> <tr class="email-intro"> <td> <p class="greeting">{{trans "%customer_name," customer_name=$customer_name}}</p> <p>{{trans "Your Mobile #%mobile_no" mobile_no=$mobile_no}} </p> <p>{{trans "Your Email #%customer_email." customer_email=$customer_email}} </p> </td> </tr> </table> {{template config_path="design/email/footer_template"}} |
Mageto 2 Backend Login then go Store->Configuration->then Select your Tab
4.Now Create Email Send Helper Code.
app/code/[NameSpace]/[ModuleName]/Helper/Email.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<?php namespace [NameSpace]\[ModuleName]\Helper; use Magento\Framework\App\Helper\Context; use Magento\Framework\Translate\Inline\StateInterface; use Magento\Framework\Mail\Template\TransportBuilder; use Magento\Store\Model\ScopeInterface; use Magento\Store\Model\StoreManagerInterface; use Psr\Log\LoggerInterface; class Email extends \Magento\Framework\App\Helper\AbstractHelper { const EMAIL_TEMPLATE = 'email_user/email_configuration/email_template'; const EMAIL_SENDER = 'email_user/email_configuration/sender'; protected $inlineTranslation; protected $transportBuilder; protected $_scopeConfig; private $storeManager; private $logger; public function __construct(Context $context, StateInterface $inlineTranslation, TransportBuilder $transportBuilder, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, StoreManagerInterface $storeManager, LoggerInterface $logger) { $this->inlineTranslation = $inlineTranslation; $this->transportBuilder = $transportBuilder; $this->_scopeConfig = $scopeConfig; $this->storeManager = $storeManager; $this->logger = $logger; } public function sendEmail($customerdetails) { $customerName = $customerdetails['firstname'] . ' ' . $customerdetails['firstname']; $email = $customerdetails['email']; $mobilenumber = $customerdetails['mobilenumber']; $this->logger->debug('Sim activation sendEmail start'); $data = ['customer_name' => $customerName, 'customer_email' => $email, 'mobile_no' => $mobilenumber, 'store' => $this->getStore() ]; $this->logger->debug('toEmail == ' . $email); $this->inlineTranslation->suspend(); $storeId = $this->getStoreId(); $template = $this->_scopeConfig->getValue(self::EMAIL_TEMPLATE, ScopeInterface::SCOPE_STORE, $storeId); $sender = $this->_scopeConfig->getValue(self::EMAIL_SENDER, ScopeInterface::SCOPE_STORE, $storeId); $transport = $this->transportBuilder->setTemplateIdentifier($template)->setTemplateOptions(['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $storeId])->setTemplateVars($data)->setFrom($sender)->addTo($email)->getTransport(); try { $transport->sendMessage(); } catch(\Exception $e) { $this->logger->critical($e->getMessage()); } $this->inlineTranslation->resume(); $this->logger->debug('sendEmail end'); } public function getStoreId() { return $this->storeManager->getStore()->getId(); } public function getStore() { return $this->storeManager->getStore(); } } |
Very help full content
2022-07-11 at 12:38 pm