I have created a pop up html form for sending mail to admin panel, it can pop up and keys in data but somehow it showed error while submitting.
app/code/Myvendor/Mymodule/Block/Popup.php
<?php
namespace MyvendorMymoduleBlock;
use MagentoFrameworkViewElementTemplate;
class Popup extends MagentoFrameworkViewElementTemplate
{
protected $_registry;
public function __construct(TemplateContext $context, array $data = [], MagentoFrameworkRegistry $registry)
{
$this->_registry = $registry;
parent::__construct($context, $data);
}
public function _prepareLayout()
{
return parent::_prepareLayout(); // TODO: Change the autogenerated stub
}
public function getCurrentCategory() {
return $this->_registry->registry('current_category');
}
public function getCurrentProduct() {
return $this->_registry->registry('current_product');
}
/**
* @return string
*/
public function getFormAction()
{
return $this->getUrl('productenquiry/index/send', ['_secure' => true]);
}
}
app/code/Myvendor/Mymodule/Controller/Index/Send.php
<?php
namespace MyvendorMymoduleControllerIndex;
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());
}
}
app/code/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 "Message"}}</b></p>
<p>{{var data.message}}</p>
{{template config_path="design/email/footer_template"}}
app/code/Myvendor/Mymodule/view/frontend/templates/popup.phtml
<?php
$myBlock = MagentoFrameworkAppObjectManager::getInstance()->get('MyvendorMymoduleBlockPopup');
?>
<div class="content" style="display:none" id="popup_content">
<form
class="form enquire"
id="enquire-form"
action="<?= $myBlock->escapeUrl($myBlock->getFormAction()) ?>"
method="post">
<div class="form-group">
<Label for="#name">Name</Label>
<input name="name" id="name" title="Name" class="form-control input-text" type="text"/>
</div>
<div class="form-group">
<Label for="#email">Email</Label>
<input id="email" name="email" title="Email" class="form-control input-text" type="email">
</div>
<div class="form-group">
<Label for="#message">Message</Label>
<input id="message" name="message" title="Message" type="text" class="form-control input-text">
</div>
<div class="form-group">
<Label for="#product_name">Product Name</Label>
<input id="product_name" name="product_name" title="Product Name" type="text" value="<?php if ($currentProduct = $myBlock->getCurrentProduct()) { echo $currentProduct->getName();}; ?>" class="form-control input-text" readonly><br>
</div>
<button type="submit">Submit</button>
</form>
</div>
<div id="click-section" class="click-section">
<br>
<button type="button" class="action primary" id="click_section2">
<span data-bind="i18n: 'Click Me'"></span>
</button>
</div>
<script type="text/javascript">
require(
[
'jquery',
'Magento_Ui/js/modal/modal'
],
function($,modal) {
var options = {
type: 'popup',
responsive: true,
innerScroll: true,
title: 'Popup',
buttons: []
};
$( "#click_section2" ).click(function() {
modal(options, $('#popup_content'));
$('#popup_content').modal('openModal');
});
}
);
When I click submit button:
TypeError: Argument 1 passed to MagentoFrameworkMailAddressConverter::convertMany() must be of the type array, null given,