I am trying to send the form's data to admin panel from phtml. However, it returned an error for the HTML form's class, how can I run the php function for passing data to html and send it to admin?


 namespace Myvendor\Mymodule\Controller\Index;
 use Magento\Framework\App\Action\Context;
 use Magento\Framework\Mail\Template\TransportBuilder;
 use Zend_Mail_Transport_Sendmail;

 class Send extends \Magento\Framework\App\Action\Action{

 * Recipient email config path
const XML_PATH_EMAIL_RECIPIENT = 'email_section/sendmail/email_section_sendmail_email_template';
 * @var \Magento\Framework\Mail\Template\TransportBuilder
protected $_transportBuilder;

 * @var \Magento\Framework\Translate\Inline\StateInterface
protected $inlineTranslation;

 * @var \Magento\Framework\App\Config\ScopeConfigInterface
protected $scopeConfig;

 * @var \Magento\Store\Model\StoreManagerInterface
protected $storeManager;
 * @var \Magento\Framework\Escaper
protected $_escaper;
 * @param \Magento\Framework\App\Action\Context $context
 * @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
 * @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
 * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
 * @param \Magento\Store\Model\StoreManagerInterface $storeManager
public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
    \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Framework\Escaper $escaper
) {
    $this->_transportBuilder = $transportBuilder;
    $this->inlineTranslation = $inlineTranslation;
    $this->scopeConfig = $scopeConfig;
    $this->storeManager = $storeManager;
    $this->_escaper = $escaper;

 * Post user question
 * @return void
 * @throws \Exception
public function execute()
    $post = $this->getRequest()->getParams();
    if (!$post) {


    try {
        $postObject = new \Magento\Framework\DataObject();

        $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_section_sendmail_email_template') // this code we have mentioned in the email_templates.xml
                    'area' => \Magento\Framework\App\Area::AREA_FRONTEND, // this is using frontend area to get the template file
                    'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
            ->setTemplateVars(['data' => $postObject])
            ->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope))

        $transport->sendMessage(); ;
            __('Thanks for contacting us with your comments and questions. We\'ll respond to you very soon.')

    } catch (\Exception $e) {
            __('We can\'t process your request right now. Sorry, that\'s all we know.'.$e->getMessage())


    return $this->_redirect($this->_redirect->getRefererUrl());




* @var \Myvendor\Mymodule\Controller\Index\Send $myBlock2

$block3 = $myBlock2->excute();


<div class="content" style="display:none" id="popup_content">
    class="form enquire"
    action="<?php $block3 ?>"
    <label for="name">Name:</label> <br>
    <input id="name" name="name" type="text"><br>
    <label for="email">Email:</label> <br>
    <input id="email" name="email" type="email"><br>
    <label for="message">Message:</label> <br>
    <input id="message" name="message" type="text"><br>
    <label for="product_name">Product Name:</label> <br>
    <input id="product_name" name="product_name" type="text"><br>
    <button type="submit">Submit</button>


{{template config_path="design/email/header_template"}}

   <table class="message-details">
           <td><b>{{trans "Name"}}</b></td>
    <td>{{var data.name}}</td>
    <td><b>{{trans "Email"}}</b></td>
    <td>{{var data.email}}</td>
    <td><b>{{trans "Product Name"}}</b></td>
    <td>{{var data.product_name}}</td>
  <p><b>{{trans "Comments"}}</b></p>
  <p>{{var data.message}}</p>

  {{template config_path="design/email/footer_template"}}

1 Answer 1

  1. In phtml file, only $block is predefined variable.
    $bock1, $block2,... are not predefined variables, so you can't use these variables.

  • You don't need to (and can not) call $block->execute() to execute the controller method. When the form is submitted (click the submit button) from the frontend, the corresponding controller will be called automatically by Magento.
  • In your phtml file, change your action from action="<?php $block3 ?>" to action="<?= $escaper->escapeUrl($block->getFormAction()) ?>"

And declare getFormAction() method in your block: Assume your Block file path is app/code/Myvendor/Mymodule/Block/MyBlock.php, open the block file to declare getFormAction() method:

public function getFormAction()
        return $this->getUrl('frontName/controllerName/actionName', ['_secure' => true]);


  • frontName specifies the frontName of the FrontController to use. It is declared in routes.xml. In your case, your controller work in frontend, the routers.xml file is located in app/code/Myvendor/Mymodule/etc/frontend/routes.xml
  • controllerName specifies the name of the controller
  • actionName specifies the action class to execute on the controller class

Assume your frontName is productenquiry, your controllerName is Index, your actionName is Send, your route path should be: productenquiry/index/send and your getFormAction() method should be:

public function getFormAction()
        return $this->getUrl('productenquiry/index/send', ['_secure' => true]);

Your module is related to Magento_Contact core module, so you can take a look at that module to build your module: vendor/magento/module-contact

  • Hi Tu, thanks for your answer, sorry that I am new at Magento2 and PHP. It's fine after changed to "action="<?= $block->escapUrl($block->getFormAction()) ?>"". But it has no response after clicking submit button. May I know did I miss some settings?
    – hill2011
    Commented Nov 13, 2022 at 8:49
  • Yeah, I know you are new so I've written the details instructions for you. Let's open the Browser Developer Tool (you can do it by Right click inside the page then choosing Inspect), change to the Console tab to see if there are any errors then post it here or take a screenshot and post it here, I'll debug.
    – Tu Van
    Commented Nov 13, 2022 at 9:13
  • I tested as per your advice, and no error showed in the console, as this form is a pop-up modal, it just closed after clicking submit. Does it mean the controller did not run execute function?
    – hill2011
    Commented Nov 13, 2022 at 9:48
  • I'll check and let you know.
    – Tu Van
    Commented Nov 13, 2022 at 10:07
  • Even if your form is in a popup, it still calls the controller if the form is submitted. I've tested your code, and it works. Let's right-click on the Name field and select Inspect to check whether the form action is correct or not.Take a look at my screenshot i.imgur.com/Uta4FVB.png
    – Tu Van
    Commented Nov 13, 2022 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.