0

How to send the form data through email to admin I have created a form in the template and on submit click it should send data to the admin.

0

Try this,

In your controller

<?php                                                                 
 namespace Vendor\module\Controller\Index;                                
 use Magento\Framework\Mail\Template\TransportBuilder;                      
 use Magento\Framework\Translate\Inline\StateInterface;                     
 use Magento\Framework\Escaper;                                             
 use Magento\Framework\App\Area;                                            
 use Magento\Framework\App\Config\ScopeConfigInterface;                     
 use Magento\Store\Model\StoreManagerInterface;                             
 use Magento\Framework\App\Action\Context;                                
 class Save extends \Magento\Framework\App\Action\Action                      
 {
  const XML_PATH_EMAIL_RECIPIENT = 'trans_email/ident_general/email';
  private $_transportBuilder;
  protected $_escaper;
  protected $scopeConfig;
  protected $storeManager;
  private $inlineTranslation;
  public function __construct(
    TransportBuilder $transportBuilder,
    StateInterface $inlineTranslation,
    Escaper $escaper,
    ScopeConfigInterface $scopeConfig,
    StoreManagerInterface $storeManager,
    Context $context
)  {
     $this->_transportBuilder = $transportBuilder;
     $this->inlineTranslation = $inlineTranslation;
     $this->_escaper = $escaper;
     $this->scopeConfig = $scopeConfig;
     $this->storeManager = $storeManager;
     parent::__construct($context);
}

public function execute()
{
    $post = $this->getRequest()->getPostValue();
    if (!$post) {
        $this->_redirect('*/*/');
        return;
    }

    $this->inlineTranslation->suspend();
    try {
        $postObject = new \Magento\Framework\DataObject();
        $postObject->setData($post);

        $error = false;

        $sender = [
        'name' => $this->_escaper->escapeHtml($post['name']),
        'email' => $this->_escaper->escapeHtml($post['email']),
        ];

        $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
        $transport = $this->_transportBuilder
        ->setTemplateIdentifier('email_template')
        ->setTemplateOptions(
            [
        'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
        'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
        ]
        )
        ->setTemplateVars(['data' => $postObject])
        ->setFrom($sender)
        ->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope))
        ->getTransport();

        $transport->sendMessage();
        ;
        $this->inlineTranslation->resume();
        $this->messageManager->addSuccess(
            __('Thanks for contacting us with your comments and questions. We\'ll respond to you very soon.')
        );
        $this->_redirect('*/*/');
        return;
    } catch (\Exception $e) {
        $this->inlineTranslation->resume();
        $this->messageManager->addError(
            __('We can\'t process your request right now. Sorry, that\'s all we know.'.$e->getMessage())
        );
        $this->_redirect('*/*/');
        return;
    }
}}

add email_templates.xml in

vendor/module/etc/email_templates.xml

<?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="email_template" label="Custom Email Template" file="custom_template.html" type="html" module="Vendor_Module" area="frontend" />

then add custom_template.html in

vendor/module/view/frontend/email/custom_template.html

<!--@subject Test Form@--><!--@vars {"var data.comment":"Comment","var data.email":"Sender Email", "var data.name":"Sender Name"} @-->{{trans "Name: %name" name=$data.name}} {{trans "Email: %email" email=$data.email}} {{trans "Comment: %comment" test=$data.comment}}

then add your form action as follows in your phtml file

 action="<?= $block->getUrl('form/index/save') ?>"

form - It's your frontname.

index - It's your controller name.

save - It's your action name.

NOTE : Stores->General->Store Email Address->General Contact->Sender Email (your recipient email address should have to be there)

Hope this helps. Peace :)

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.