lI created a custom form and sending mail once submit the form.Now i need to add upload file button and send all form details through mail. i added input file tag in form but i have no idea how to configure that in controller file and email html file
Any help can be appreciated.. Thanks
my phtml file
<div id="popup-modal">
<div class="contact-form-popup" style="display:none;">
<form class="form contact"
action=""
id="contact-form"
method="post"
data-hasrequired="<?= $block->escapeHtmlAttr(__('* Required Fields')) ?>"
data-mage-init='{"validation":{}}'
autocomplete="off">
<fieldset class="fieldset">
<legend class="legend"><span><?= $block->escapeHtml(__('Get in touch')) ?></span></legend>
<div class="field note"><?= $block->escapeHtml(__('Simply fill in your details and enquiry below and one of our team will be in touch as soon as possible.')) ?></div>
<div class="field firstname">
<label class="label" for="name"><span><?= $block->escapeHtml(__('Your Name:')) ?></span></label>
<div class="required-field">Required</div>
<div class="control">
<input name="name" id="name" title="<?= $block->escapeHtmlAttr(__('Name')) ?>"
class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="field emailaddress">
<label class="label" for="emailaddress"><span><?= $block->escapeHtml(__('Customer email address:')) ?></span></label>
<div class="required-field">Required</div>
<div class="control">
<input name="emailaddress" id="emailaddress" title="<?= $block->escapeHtmlAttr(__('Email')) ?>"
class="input-text" type="email" data-validate="{required:true, 'validate-email':true}"/>
</div>
</div>
<div class="field mobile">
<label class="label" for="mobile"><span><?= $block->escapeHtml(__('Phone number:')) ?></span></label>
<div class="required-field">Required</div>
<div class="control">
<input name="mobile" id="mobile" title="<?= $block->escapeHtmlAttr(__('Phone number')) ?>"
class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="field postcode">
<label class="label" for="postcode"><span><?= $block->escapeHtml(__('Customer postcode:')) ?></span></label>
<div class="required-field">Required</div>
<div class="control">
<input name="postcode" id="postcode" title="<?= $block->escapeHtmlAttr(__('Customer postcode')) ?>"
class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="field enquiry">
<label class="label" for="enquiry"><span><?= $block->escapeHtml(__('Enquiry:')) ?></span></label>
<div class="control">
<textarea name="enquiry" id="enquiry" title="<?= $block->escapeHtmlAttr(__('Enquiry')) ?>"
class="input-text" type="text" ></textarea>
</div>
</div>
</fieldset>
<div class="actions-toolbar">
<div class="primary">
<button type="submit" id='customer-contact' title="<?= $block->escapeHtmlAttr(__('Submit')) ?>"
class="action submit primary">
<span><?= $block->escapeHtml(__('Submit')) ?></span>
</button>
</div>
</div>
</form>
</div>
<div style="display:none" class="static-block-message">
<legend class="legend"><span><?= $block->escapeHtml(__('Get in touch')) ?></span></legend>
<div class="field note">
<?= $block->escapeHtml(__('Simply fill in your details and enquiry below and one of our team will be in touch as soon as possible.')) ?>
<strong><?= $block->escapeHtml(__('Thank you. A member of our team will be in touch shortly.'))?></strong>
</div>
</div>
</div>
<script>
require(
[
'jquery',
'Magento_Ui/js/modal/modal'
],
function($, modal) {
jQuery(document).ready(function(){
var options = {
type: 'popup',
responsive: true,
innerScroll: true,
modalClass: 'get-in-touch',
buttons: []
};
var openModal = modal(options, $('#popup-modal'));
var issubmit = '<?= $_SESSION['issubmitted'];?>';
jQuery('body').on('click', '.demo-banner .pagebuilder-button-primary', function(){
jQuery('.contact-form-popup').show();
jQuery('.static-block-message').hide();
jQuery('#popup-modal').modal('openModal');
});
jQuery('body').on('click', '#contact-form .action.submit', function(e){
e.preventDefault();
//e.stopImmediatePropagation();
if(!(jQuery('#contact-form').valid())){
return false;
}
else if((jQuery('#contact-form').valid())){
jQuery.ajax({
type: 'post',
url: '<?php echo $block->getUrl("popup/action/index") ?>',
data: jQuery('#contact-form').serialize(),
cache: false,
showLoader: 'true',
success: function(response) {
//alert('success');
jQuery('.contact-form-popup').hide();
jQuery('.static-block-message').show();
}
});
}
return false;
});
});
}
);
</script>
my controller
<?php
namespace Apetito\Popup\Controller\Action;
use Zend\Log\Filter\Timestamp;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\Controller\ResultFactory;
class Index extends \Magento\Framework\App\Action\Action
{
/**
* Recipient email config path
*/
const XML_PATH_EMAIL_RECIPIENT = 'contact/email/recipient_email';
/**
* @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()
{
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$post = $this->getRequest()->getPostValue();
//print_r($post);
//die("hello");
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['emailaddress']),
];
$storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
$transport = $this->_transportBuilder
->setTemplateIdentifier('popup_email') // this code we have mentioned in the email_templates.xml
->setTemplateOptions(
[
'area' => 'frontend',
'store' => $this->storeManager->getStore()->getId()
]
)
->setTemplateVars([
'name' => $post['name'],
'email' => $post['emailaddress'],
'mobile' => $post['mobile'],
'postcode' => $post['postcode'],
'enquiry' => $post['enquiry'],
])
->setFrom($sender)
->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope))
->addTo('[email protected]')
->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.')
//);
//$resultRedirect->setUrl($this->_redirect->getRefererUrl());
//$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())
//);
//$resultRedirect->setUrl($this->_redirect->getRefererUrl());
//$this->_redirect('/');
return;
}
}
}
template file
<!--@subject customer Enquiry @-->
{{template config_path="design/email/header_template"}}
<table>
<tr class="email-intro">
<tr>
Customer Name : {{var name}}
</tr>
<tr>
Customer Email Address : {{var email}}
</tr>
<tr>
Phone Number : {{var mobile}}
</tr>
<tr>
Customer Postcode : {{var postcode}}
</tr>
<tr>
Customer Enquiry : {{var enquiry}}
</tr>
</tr>
</table>
{{template config_path="design/email/footer_template"}}