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.
<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'Magemonkey_Customform',
__DIR__
);
3. Create module.xml file in app/code/Magemonkey/Customform/etc/ and put below code.
<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.
<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.
<?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.
<?php
namespace MagemonkeyCustomformControllerIndex;
class Index extends MagentoFrameworkAppActionAction
{
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.
<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="MagemonkeyCustomformBlockIndexIndex" 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.
<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.
<!--@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.
<?php namespace MagemonkeyCustomformControllerIndex; use MagentoFrameworkControllerResultFactory; use MagentoFrameworkAppActionAction; use MagentoFrameworkAppActionContext; use MagentoFrameworkAppRequestDataPersistorInterface; use MagentoFrameworkViewElementMessages; class Post extends Action { protected $_inlineTranslation; protected $_transportBuilder; protected $_scopeConfig; protected $_logLoggerInterface; public function __construct( MagentoFrameworkAppActionContext $context, MagentoFrameworkTranslateInlineStateInterface $inlineTranslation, MagentoFrameworkMailTemplateTransportBuilder $transportBuilder, MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig, PsrLogLoggerInterface $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 = MagentoStoreModelScopeInterface::SCOPE_STORE;
$sentToEmail = $this->_scopeConfig ->getValue('trans_email/ident_general/email',MagentoStoreModelScopeInterface::SCOPE_STORE);
$sentToName = $this->_scopeConfig ->getValue('trans_email/ident_general/name',MagentoStoreModelScopeInterface::SCOPE_STORE);
$fromEmail = $post['email'];
$fromName = $post['name'];
$templateOptions = array('area' => MagentoFrameworkAppArea::AREA_FRONTEND, 'store' => MagentoStoreModelStore::DEFAULT_STORE_ID);
$templateVars = array(
'store' => MagentoStoreModelStore::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;
}
}
}

