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 Myvendor\Mymodule\Block;
use Magento\Framework\View\Element\Template;
class Popup extends \Magento\Framework\View\Element\Template
{
protected $_registry;
public function __construct(Template\Context $context, array $data = [], \Magento\Framework\Registry $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 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
) {
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 \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_section_sendmail_email_template') // this code we have mentioned in the email_templates.xml
->setTemplateOptions(
[
'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])
->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 = \Magento\Framework\App\ObjectManager::getInstance()->get('Myvendor\Mymodule\Block\Popup');
?>
<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 Magento\Framework\Mail\AddressConverter::convertMany() must be of the type array, null given,