I created a custom form and even extended the controller of Magento Contact Us and modified it. Now my problem is that whenever, I fill out the form that I created, It will use the email template of Magento Contact Us to send notification. Sorry I'm just new to this. I hope you can help me.
And also I have created a custom email template for my form under Marketing>Email Templates, I want this email template to be used in my custom form.
Attached is the current screenshot of my Index.php Controller.
Here is the code of Index.php Controller
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Company\ModuleName\Controller;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\App\RequestInterface;
use Magento\Store\Model\ScopeInterface;
/**
* Contact index controller
*/
abstract class Index extends \Magento\Framework\App\Action\Action
{
/**
* Recipient email config path
*/
const XML_PATH_EMAIL_RECIPIENT = 'contact/email/recipient_email';
/**
* Sender email config path
*/
const XML_PATH_EMAIL_SENDER = 'contact/email/sender_email_identity';
/**
* Email template config path
*/
const XML_PATH_EMAIL_TEMPLATE = 'contact/email/email_template';
/**
* Enabled config path
*/
const XML_PATH_ENABLED = 'contact/contact/enabled';
/**
* @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;
/**
* @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
) {
parent::__construct($context);
$this->_transportBuilder = $transportBuilder;
$this->inlineTranslation = $inlineTranslation;
$this->scopeConfig = $scopeConfig;
$this->storeManager = $storeManager;
}
/**
* Dispatch request
*
* @param RequestInterface $request
* @return \Magento\Framework\App\ResponseInterface
* @throws \Magento\Framework\Exception\NotFoundException
*/
public function dispatch(RequestInterface $request)
{
if (!$this->scopeConfig->isSetFlag(self::XML_PATH_ENABLED, ScopeInterface::SCOPE_STORE)) {
throw new NotFoundException(__('Page not found.'));
}
return parent::dispatch($request);
}
}
And Here is the Controller
<?php
/**
*
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Company\ModuleName\Controller\Index;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;
class Cs extends \Company\ModuleName\Controller\Index
{
/**
* @var DataPersistorInterface
*/
private $dataPersistor;
/**
* Post user question
*
* @return void
* @throws \Exception
*/
public function execute()
{
$post = $this->getRequest()->getPostValue();
if (!$post) {
$this->_redirect('*/*/');
return;
}
$this->inlineTranslation->suspend();
try {
$postObject = new \Magento\Framework\DataObject();
$postObject->setData($post);
$error = false;
if (!\Zend_Validate::is(trim($post['name']), 'NotEmpty')) {
$error = true;
}
if (!\Zend_Validate::is(trim($post['address']), 'NotEmpty')) {
$error = true;
}
if (!\Zend_Validate::is(trim($post['mix-name']), 'NotEmpty')) {
$error = true;
}
if (!\Zend_Validate::is(trim($post['company']), 'NotEmpty')) {
$error = true;
}
if (!\Zend_Validate::is(trim($post['city']), 'NotEmpty')) {
$error = true;
}
if (!\Zend_Validate::is(trim($post['address']), 'NotEmpty')) {
$error = true;
}
if (!\Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
$error = true;
}
if (!\Zend_Validate::is(trim($post['telephone']), 'NotEmpty')) {
$error = true;
}
if (!\Zend_Validate::is(trim($post['zip']), 'NotEmpty')) {
$error = true;
}
if (!\Zend_Validate::is(trim($post['fax']), 'NotEmpty')) {
$error = true;
}
if (!\Zend_Validate::is(trim($post['concentration']), 'NotEmpty')) {
$error = true;
}
if (!\Zend_Validate::is(trim($post['instructions']), 'NotEmpty')) {
$error = true;
}
// if (\Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
// $error = true;
// }
if ($error) {
throw new \Exception();
}
$storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
$transport = $this->_transportBuilder
->setTemplateIdentifier($this->scopeConfig->getValue(self::XML_PATH_EMAIL_TEMPLATE, $storeScope))
->setTemplateOptions(
[
'area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE,
'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
]
)
->setTemplateVars(['customer' => $postObject])
->setFrom($this->scopeConfig->getValue(self::XML_PATH_EMAIL_SENDER, $storeScope))
->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope))
->setReplyTo($post['email'])
->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.')
);
$this->getDataPersistor()->clear('contact_us');
$this->_redirect('thank-you-contact-us');
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())
);
$this->getDataPersistor()->set('analytical-instrument-supplies-standards-custom-standards.html', $post);
$this->_redirect('analytical-instrument-supplies-standards-custom-standards.html');
return;
}
}
/**
* Get Data Persistor
*
* @return DataPersistorInterface
*/
private function getDataPersistor()
{
if ($this->dataPersistor === null) {
$this->dataPersistor = ObjectManager::getInstance()
->get(DataPersistorInterface::class);
}
return $this->dataPersistor;
}
}
Best Answer
app/code/Company/ModuleName/etc/adminhtml/system.xml
app/code/Company/ModuleName/etc/email_templates.xml
app/code/Company/ModuleName/view/frontend/email/customform.html
Change,