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?
Myvendor/Mymodule/Controller/Index/Send.php
<?php
namespace CleargoProductEnquiryControllerIndex;
use MagentoFrameworkAppActionContext;
use MagentoFrameworkMailTemplateTransportBuilder;
use Zend_Mail_Transport_Sendmail;
class Send extends MagentoFrameworkAppActionAction{
/**
* Recipient email config path
*/
const XML_PATH_EMAIL_RECIPIENT = 'email_section/sendmail/email_section_sendmail_email_template';
/**
* @var MagentoFrameworkMailTemplateTransportBuilder
*/
protected $_transportBuilder;
/**
* @var MagentoFrameworkTranslateInlineStateInterface
*/
protected $inlineTranslation;
/**
* @var MagentoFrameworkAppConfigScopeConfigInterface
*/
protected $scopeConfig;
/**
* @var MagentoStoreModelStoreManagerInterface
*/
protected $storeManager;
/**
* @var MagentoFrameworkEscaper
*/
protected $_escaper;
/**
* @param MagentoFrameworkAppActionContext $context
* @param MagentoFrameworkMailTemplateTransportBuilder $transportBuilder
* @param MagentoFrameworkTranslateInlineStateInterface $inlineTranslation
* @param MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig
* @param MagentoStoreModelStoreManagerInterface $storeManager
*/
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoFrameworkMailTemplateTransportBuilder $transportBuilder,
MagentoFrameworkTranslateInlineStateInterface $inlineTranslation,
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoFrameworkEscaper $escaper
) {
parent::__construct($context);
$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) {
$this->_redirect('*/*/');
return;
}
$this->inlineTranslation->suspend();
try {
$postObject = new MagentoFrameworkDataObject();
$postObject->setData($post);
$error = false;
$sender = [
'name' => $this->_escaper->escapeHtml($post['name']),
'email' => $this->_escaper->escapeHtml($post['email']),
];
$storeScope = MagentoStoreModelScopeInterface::SCOPE_STORE;
$transport = $this->_transportBuilder
->setTemplateIdentifier('email_section_sendmail_email_template') // this code we have mentioned in the email_templates.xml
->setTemplateOptions(
[
'area' => MagentoFrameworkAppArea::AREA_FRONTEND, // this is using frontend area to get the template file
'store' => MagentoStoreModelStore::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.')
);
} 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())
);
}
return $this->_redirect($this->_redirect->getRefererUrl());
}
}
Myvendor/Mymodule/view/frontend/templates/popup.phtml
<?php
/**
* @var MyvendorMymoduleControllerIndexSend $myBlock2
*/
$block3 = $myBlock2->excute();
?>
<div class="content" style="display:none" id="popup_content">
<form
class="form enquire"
id="enquire-form"
action="<?php $block3 ?>"
method="post">
<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>
<br>
<button type="submit">Submit</button>
</form>
Myvendor/Mymodule/view/frontend/email/email_file.html
{{template config_path="design/email/header_template"}}
<table class="message-details">
<tr>
<td><b>{{trans "Name"}}</b></td>
<td>{{var data.name}}</td>
</tr>
<tr>
<td><b>{{trans "Email"}}</b></td>
<td>{{var data.email}}</td>
</tr>
<tr>
<td><b>{{trans "Product Name"}}</b></td>
<td>{{var data.product_name}}</td>
</tr>
</table>
<p><b>{{trans "Comments"}}</b></p>
<p>{{var data.message}}</p>
{{template config_path="design/email/footer_template"}}