Первым делом создаем шаблон отправки письма, для этого объявляем наш шаблон
app/code/Custom/Module/etc/email_templates.xml
<?xml version="1.0"?> <config xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:Magento:module:Magento_Email:etc/email_templates.xsd"> <template id="feedback_email_template" label="New feedback email" file="email_feedback.html" type="html" module="Custom_Module" area="frontend"/> </config>
Создадим наш шаблон по данному пути:
app/code/Custom/Module/etc/email_templates.xml
<!--@subject Обратная связь с сайта @--> <div> Обратная связь от: {{var name}} <br> Номер заказа: {{var number_order}} <br> E-mail: {{var email}} <br> Телефон: {{var phone}} <br> Сообщение: {{var message}} </div>
Создаем helper из которого будет отправляться само письмо:
app/code/Custom/Module/Helper/Email.php
<?php namespace Custom\Module\Helper; use Magento\Framework\App\Helper\Context; use Magento\Framework\Translate\Inline\StateInterface; use Magento\Framework\Escaper; use Magento\Framework\Mail\Template\TransportBuilder; class Email extends \Magento\Framework\App\Helper\AbstractHelper { /** * @var StateInterface */ protected $inlineTranslation; /** * @var Escaper */ protected $escaper; /** * @var TransportBuilder */ protected $transportBuilder; /** * @var \Psr\Log\LoggerInterface */ protected $logger; /** * @var */ protected $_scopeConfig; public function __construct( Context $context, StateInterface $inlineTranslation, Escaper $escaper, TransportBuilder $transportBuilder, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig ) { parent::__construct($context); $this->inlineTranslation = $inlineTranslation; $this->escaper = $escaper; $this->transportBuilder = $transportBuilder; $this->logger = $context->getLogger(); $this->_scopeConfig = $scopeConfig; } public function sendOrder($name, $email, $message, $phone = null, $numberOrder = null) { try { $this->inlineTranslation->suspend(); $senderEmail = $this->_scopeConfig->getValue( 'trans_email/ident_general/email', \Magento\Store\Model\ScopeInterface::SCOPE_STORE ); $sender = [ 'name' => $this->escaper->escapeHtml($name), 'email' => $this->escaper->escapeHtml($senderEmail), ]; $transport = $this->transportBuilder ->setTemplateIdentifier('feedback_email_template') ->setTemplateOptions( [ 'area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID, ] ) ->setTemplateVars([ 'name' => $name, 'email' => $email, 'message' => $message, 'phone' => $phone, 'number_order' => $numberOrder ]) ->setFrom($sender) ->addTo($senderEmail) ->getTransport(); $transport->sendMessage(); $this->inlineTranslation->resume(); } catch (\Exception $e) { $this->logger->debug($e->getMessage()); } } }