You need to create module like below:
1. Create module directories Magemonkey/Customform in app/code.
2. Create registration.php file in app/code/Magemonkey/Customform/ and put below code.
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Magemonkey_Customform', __DIR__ ); |
3. Create module.xml file in app/code/Magemonkey/Customform/etc/ and put below code.
1 2 3 4 |
<pre class="lang:default decode:false"><?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> <module name="Magemonkey_Customform" setup_version="1.0.0"></module> </config></pre> |
4. Create routes.xml in app/code/Magemonkey/Customform/etc/frontend/ and put below code.
1 2 3 4 5 6 7 |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd"> <router id="standard"> <route id="customform" frontName="customform"> <module name="Magemonkey_Customform" /> </route> </router> </config> |
5. Create email_templates.xml in app/code/Magemonkey/Customform/etc/ and put below code.
1 2 3 4 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd"> <template id="customform" label="custom form" file="customform.html" type="html" module="Magemonkey_Customform" area="frontend"/> </config> |
6. Create Index.php file under app/code/Magemonkey/Customform/Controller/ and put below code.
1 2 3 4 5 6 7 8 9 10 11 |
<?php namespace Magemonkey\Customform\Controller\Index; class Index extends \Magento\Framework\App\Action\Action { public function execute() { $this->_view->loadLayout(); $this->_view->getLayout()->initMessages(); $this->_view->renderLayout(); } } |
7. Create customform.xml layout file in app/code/Magemonkey/Customform/view/frontend/layout/ and put below code.
1 2 3 4 5 6 7 |
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd"> <body> <referenceContainer name="content"> <block class="Magemonkey\Customform\Block\Index\Index" name="customform" template="Magemonkey_Customform::customform.phtml"/> </referenceContainer> </body> </page> |
8. Create template file customform.phtml in app/code/Magemonkey/Customform/view/templates/ and put below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<form action="<?php echo $block->getBaseUrl().'customform/index/post/';?>" name="customform" method="post" id="customform" data-hasrequired="<?php echo __('* Required Fields') ?>" data-mage-init='{"validation":{}}'> <fieldset class="fieldset"> <div class="form-group general-form"> <div class="field-list"> <div class="field name required"> <div class="control"> <input name="name" id="name" placeholder="<?php echo __('Full Name*') ?>" title="<?php echo __('Name') ?>" class="input-text" type="text" data-validate="{required:true}"/> </div> </div> <div class="field company required"> <div class="control"> <input name="company" id="company" placeholder="<?php echo __('Company Name*') ?>" title="<?php echo __('Company Name*') ?>" class="input-text" type="text" data-validate="{required:true}"/> </div> </div> <div class="field email required"> <div class="control"> <input name="email" id="email" placeholder="<?php echo __('Email Address*') ?>" title="<?php echo __('Email Address*') ?>" class="input-text" type="email" data-validate="{required:true, 'validate-email':true}"/> </div> </div> </div> </div> </fieldset> <div class="actions-toolbar"> <div class="primary primary-group"> <input type="hidden" name="hideit" id="hideit" value=""> <button type="submit" title="<?php echo __('Submit Entry') ?>" class="action submit primary"> <span><?php echo __('Submit Entry') ?></span> </button> </div> </div> </form> |
9. Create customform.html under app/code/Magemonkey/Customform/view/frontend/email/ and put below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<!--@subject Customform Notification @--> <!--@vars {"store url=\"\"":"Store Url", "skin url=\"images/logo_email.gif\" _area='frontend'":"Email Logo Image"} @--> <!--@styles body,td { color:#2f2f2f; font:11px/1.35em Verdana, Arial, Helvetica, sans-serif; } @--> {{template config_path="design/email/header_template"}} <table> <tr class="email-intro"> <th>Full Name</th><td>{{var name}}</td> </tr> <tr class="email-intro"> <th>Company Name</th><td>{{var company}}</td> </tr> <tr class="email-intro"> <th>Email Address</th><td>{{var email}}</td> </tr> </table> {{template config_path="design/email/footer_template"}} |
10. Now create file for submitting data to post action so Create Post.php under app/code/Magemonkey/Customform/Controller/post/ and put below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<?php namespace Magemonkey\Customform\Controller\Index; use Magento\Framework\Controller\ResultFactory; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; use Magento\Framework\App\Request\DataPersistorInterface; use Magento\Framework\View\Element\Messages; class Post extends Action { protected $_inlineTranslation; protected $_transportBuilder; protected $_scopeConfig; protected $_logLoggerInterface; public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation, \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Psr\Log\LoggerInterface $loggerInterface, array $data = [] ) { $this->_inlineTranslation = $inlineTranslation; $this->_transportBuilder = $transportBuilder; $this->_scopeConfig = $scopeConfig; $this->_logLoggerInterface = $loggerInterface; $this->messageManager = $context->getMessageManager(); parent::__construct($context); } public function execute() { $post = $this->getRequest()->getPost(); try { // Send Mail $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE; $sentToEmail = $this->_scopeConfig ->getValue('trans_email/ident_general/email',\Magento\Store\Model\ScopeInterface::SCOPE_STORE); $sentToName = $this->_scopeConfig ->getValue('trans_email/ident_general/name',\Magento\Store\Model\ScopeInterface::SCOPE_STORE); $fromEmail = $post['email']; $fromName = $post['name']; $templateOptions = array('area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID); $templateVars = array( 'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID, 'name' => $post['name'], 'company' => $post['company'], 'position' => $post['position'], 'telephone' => $post['telephone'], 'email' => $post['email'], 'type_of_business' => $post['type_of_business'], 'access_industry' => $post['access_industry'], 'series' => $post['series'], 'agree_administration' => $post['agree_administration'], 'agree_communications' => $post['agree_communications'] ); $from = array('email' => $fromEmail, 'name' => $fromName); $this->_inlineTranslation->suspend(); $to = $sentToEmail; /* Set admin email here */ $transport = $this->_transportBuilder->setTemplateIdentifier('Competitionform') ->setTemplateOptions($templateOptions) ->setTemplateVars($templateVars) ->setFrom($from) ->addTo($to) ->getTransport(); $transport->sendMessage(); $this->_inlineTranslation->resume(); $this->messageManager->addSuccess('Email has been sent successfully'); $this->_redirect('customform'); } catch(\Exception $e){ $this->messageManager->addError($e->getMessage()); $this->_logLoggerInterface->debug($e->getMessage()); exit; } } } |
[crayon-63d970903089b424839940/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.