plugin for custom field for contact form magento2main.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2 Add new field to Magento_User admin formMagento offline custom Payment method with drop down listI have created one field using product form field for my price i want save my field value at product creation time from backend magento2Magento 2.3 Can't view module's front end page output?Magento 2 get custom attribute of a single product inside a pluginMagento 2 plugin change price of products that have a custom attribute withOverriding the contact form module gets the fatal error in Magento2.2.5?How to create custom form in Magento 2.2.3
Can "être sur" mean "to be about" ?
Does fossil fuels use since 1990 account for half of all the fossil fuels used in history?
Breadcrumb history decision
How can I decide if my homebrew item should require attunement?
A continuous water "planet" ring around a star
Bitcoin successfully deducted on sender wallet but did not reach receiver wallet
Why are Tucker and Malcolm not dead?
How can I categorize files in a directory based on their content?
Is there any way to stop a user from creating executables and running them?
How many people would you need to pull a whale over cobblestone streets?
Why does chown not work in RUN command in Docker?
Loading military units into ships optimally, using backtracking
What is an internal dimension/glue/muglue?
Can sampling rate be a floating point number?
Boss wants me to ignore a software license
PhD advisor lost funding, need advice
First amendment and employment: Can a police department terminate an officer for speech?
Why is there a large performance impact when looping over an array over 240 elements?
How does "Te vas a cansar" mean "You're going to get tired"?
Simplification of numbers
How can Radagast come across Gandalf and Thorin's company?
AsyncDictionary - Can you break thread safety?
Submitting a new paper just after another was accepted by the same journal
How do some PhD students get 10+ papers? Is that what I need for landing good faculty position?
plugin for custom field for contact form magento2
main.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2 Add new field to Magento_User admin formMagento offline custom Payment method with drop down listI have created one field using product form field for my price i want save my field value at product creation time from backend magento2Magento 2.3 Can't view module's front end page output?Magento 2 get custom attribute of a single product inside a pluginMagento 2 plugin change price of products that have a custom attribute withOverriding the contact form module gets the fatal error in Magento2.2.5?How to create custom form in Magento 2.2.3
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I want to create plugin for contact form with two custom fields first-name and last-name to send email.
anyone can help with this?
Thanks in advance.
in di.xml
<config xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoContactControllerIndexPost">
<plugin name="Convert_CustomContact_Plugin" type="ConvertCustomContactPluginPost" sortOrder="10" disabled="false" />
</type>
</config>
In plugin
<?php
namespace ConvertCustomContactPlugin;
use MagentoFrameworkAppRequestDataPersistorInterface ;
use MagentoFrameworkAppActionContext;
use MagentoContactModelMailInterface;
use MagentoFrameworkEscaper;
use MagentoFrameworkTranslateInlineStateInterface;
class Post
protected $inlineTranslation;
protected $_escaper;
public function __construct(
DataPersistorInterface $dataPersistor,
Context $context,
Escaper $escaper,
StateInterface $inlineTranslation
)
$this->dataPersistor = $dataPersistor;
$this->context = $context;
$this->_escaper = $escaper;
$this->inlineTranslation= $inlineTranslation;
public function afterExecute(MagentoContactControllerIndexPost $subject, $result)
// In the original method, dataPersistor is cleared if a message has been sent
// so we can use it as an indicator to whether a user can be redirected somewhere else
// or fall back to the result of the original method
$post = $subject->getRequest()->getPostValue();
if ($post)
$this->inlineTranslation->suspend();
// $this->inlineTranslation->suspend();
$postObject = new MagentoFrameworkDataObject();
$postObject->setData($post);
$error = false;
$sender = [
'firstname' => $this->_escaper->escapeHtml($post['firstname']),
'lastname' => $this->_escaper->escapeHtml($post['lastname']),
];
return $result;
magento2 plugin contact-form
add a comment |
I want to create plugin for contact form with two custom fields first-name and last-name to send email.
anyone can help with this?
Thanks in advance.
in di.xml
<config xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoContactControllerIndexPost">
<plugin name="Convert_CustomContact_Plugin" type="ConvertCustomContactPluginPost" sortOrder="10" disabled="false" />
</type>
</config>
In plugin
<?php
namespace ConvertCustomContactPlugin;
use MagentoFrameworkAppRequestDataPersistorInterface ;
use MagentoFrameworkAppActionContext;
use MagentoContactModelMailInterface;
use MagentoFrameworkEscaper;
use MagentoFrameworkTranslateInlineStateInterface;
class Post
protected $inlineTranslation;
protected $_escaper;
public function __construct(
DataPersistorInterface $dataPersistor,
Context $context,
Escaper $escaper,
StateInterface $inlineTranslation
)
$this->dataPersistor = $dataPersistor;
$this->context = $context;
$this->_escaper = $escaper;
$this->inlineTranslation= $inlineTranslation;
public function afterExecute(MagentoContactControllerIndexPost $subject, $result)
// In the original method, dataPersistor is cleared if a message has been sent
// so we can use it as an indicator to whether a user can be redirected somewhere else
// or fall back to the result of the original method
$post = $subject->getRequest()->getPostValue();
if ($post)
$this->inlineTranslation->suspend();
// $this->inlineTranslation->suspend();
$postObject = new MagentoFrameworkDataObject();
$postObject->setData($post);
$error = false;
$sender = [
'firstname' => $this->_escaper->escapeHtml($post['firstname']),
'lastname' => $this->_escaper->escapeHtml($post['lastname']),
];
return $result;
magento2 plugin contact-form
add a comment |
I want to create plugin for contact form with two custom fields first-name and last-name to send email.
anyone can help with this?
Thanks in advance.
in di.xml
<config xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoContactControllerIndexPost">
<plugin name="Convert_CustomContact_Plugin" type="ConvertCustomContactPluginPost" sortOrder="10" disabled="false" />
</type>
</config>
In plugin
<?php
namespace ConvertCustomContactPlugin;
use MagentoFrameworkAppRequestDataPersistorInterface ;
use MagentoFrameworkAppActionContext;
use MagentoContactModelMailInterface;
use MagentoFrameworkEscaper;
use MagentoFrameworkTranslateInlineStateInterface;
class Post
protected $inlineTranslation;
protected $_escaper;
public function __construct(
DataPersistorInterface $dataPersistor,
Context $context,
Escaper $escaper,
StateInterface $inlineTranslation
)
$this->dataPersistor = $dataPersistor;
$this->context = $context;
$this->_escaper = $escaper;
$this->inlineTranslation= $inlineTranslation;
public function afterExecute(MagentoContactControllerIndexPost $subject, $result)
// In the original method, dataPersistor is cleared if a message has been sent
// so we can use it as an indicator to whether a user can be redirected somewhere else
// or fall back to the result of the original method
$post = $subject->getRequest()->getPostValue();
if ($post)
$this->inlineTranslation->suspend();
// $this->inlineTranslation->suspend();
$postObject = new MagentoFrameworkDataObject();
$postObject->setData($post);
$error = false;
$sender = [
'firstname' => $this->_escaper->escapeHtml($post['firstname']),
'lastname' => $this->_escaper->escapeHtml($post['lastname']),
];
return $result;
magento2 plugin contact-form
I want to create plugin for contact form with two custom fields first-name and last-name to send email.
anyone can help with this?
Thanks in advance.
in di.xml
<config xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoContactControllerIndexPost">
<plugin name="Convert_CustomContact_Plugin" type="ConvertCustomContactPluginPost" sortOrder="10" disabled="false" />
</type>
</config>
In plugin
<?php
namespace ConvertCustomContactPlugin;
use MagentoFrameworkAppRequestDataPersistorInterface ;
use MagentoFrameworkAppActionContext;
use MagentoContactModelMailInterface;
use MagentoFrameworkEscaper;
use MagentoFrameworkTranslateInlineStateInterface;
class Post
protected $inlineTranslation;
protected $_escaper;
public function __construct(
DataPersistorInterface $dataPersistor,
Context $context,
Escaper $escaper,
StateInterface $inlineTranslation
)
$this->dataPersistor = $dataPersistor;
$this->context = $context;
$this->_escaper = $escaper;
$this->inlineTranslation= $inlineTranslation;
public function afterExecute(MagentoContactControllerIndexPost $subject, $result)
// In the original method, dataPersistor is cleared if a message has been sent
// so we can use it as an indicator to whether a user can be redirected somewhere else
// or fall back to the result of the original method
$post = $subject->getRequest()->getPostValue();
if ($post)
$this->inlineTranslation->suspend();
// $this->inlineTranslation->suspend();
$postObject = new MagentoFrameworkDataObject();
$postObject->setData($post);
$error = false;
$sender = [
'firstname' => $this->_escaper->escapeHtml($post['firstname']),
'lastname' => $this->_escaper->escapeHtml($post['lastname']),
];
return $result;
magento2 plugin contact-form
magento2 plugin contact-form
edited Aug 1 at 9:19
Jimit Bhavsar
5295 silver badges18 bronze badges
5295 silver badges18 bronze badges
asked Aug 1 at 8:16
trem jennytrem jenny
116 bronze badges
116 bronze badges
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Please try with below url : which have to create module for custom form.
https://magenticians.com/add-custom-field-in-magento-2-contact-page/
Please try with below code :
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoFrameworkMailTemplateTransportBuilder $transportBuilder,
MagentoFrameworkTranslateInlineStateInterface $inlineTranslation,
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoFrameworkEscaper $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()->getPostValue();
if (!$post)
$this->_redirect('*/*/');
return;
$this->inlineTranslation->suspend();
try
$postObject = new MagentoFrameworkDataObject();
$postObject->setData($post);
$error = false;
$sender = [
'firstname' => $this->_escaper->escapeHtml($post['firstname']),
'lastname' => $this->_escaper->escapeHtml($post['lastname']),'email' => $this->_escaper->escapeHtml($post['email']),
];
$storeScope = MagentoStoreModelScopeInterface::SCOPE_STORE;
$transport = $this->_transportBuilder
->setTemplateIdentifier('send_email_email_template') // this code we have mentioned in the email_templates.xml
->setTemplateOptions(
[
'area' => MagentoFrameworkAppArea::AREA_FRONTEND, // this is using frontend area to get the template file
'store' => MagentoStoreModelStore::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(
__('Mail send successfully')
);
$this->_redirect('*/*/');
return;
catch (Exception $e)
$this->inlineTranslation->resume();
$this->messageManager->addError(__('Something wrong.'.$e->getMessage())
);
$this->_redirect('*/*/');
return;
I hope its very helpful to you.
thanks for your help, but i have to use plugin for it. Can you suggest me with this?
– trem jenny
Aug 1 at 8:22
yes please update que with your code.
– Anas Mansuri
Aug 1 at 8:26
i have updated my code, thanks.
– trem jenny
Aug 1 at 8:37
where you are facing the issue ?
– Anas Mansuri
Aug 1 at 8:40
i want to create plugin for two fields last name and first name to send email in contact form , but the thing is that i dont know how to do ít, with the code above, i just get the value of the fields. I want to send email with these custom fields.
– trem jenny
Aug 1 at 8:46
|
show 1 more comment
Plugin is not the right route with this particular change. Reason being if you add a field to the contact form template the magento contact controller will automatically catch values for these new fields.
In my extension I add a file field to the contact us form
https://github.com/DominicWatts/ContactAttachment
xml override
https://github.com/DominicWatts/ContactAttachment/blob/master/view/frontend/layout/contact_index_index.xml
New field
https://github.com/DominicWatts/ContactAttachment/blob/master/view/frontend/templates/form.phtml#L45-L50
You are going to have to create a new email template with the new fields
https://github.com/DominicWatts/ContactAttachment/blob/master/view/frontend/email/submitted_form.html#L25-L28
Then within magento add the transactional email. Then under system configuration use the contacts section to configure the use of the new template.
You shouldn't need to touch the controller as shown in other answer as magento automatically picks up other submitted fields.
Although as I've typed all this up I've realised that I've more or less summaried what's covered in the tutorial https://magenticians.com/add-custom-field-in-magento-2-contact-page/
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f284056%2fplugin-for-custom-field-for-contact-form-magento2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Please try with below url : which have to create module for custom form.
https://magenticians.com/add-custom-field-in-magento-2-contact-page/
Please try with below code :
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoFrameworkMailTemplateTransportBuilder $transportBuilder,
MagentoFrameworkTranslateInlineStateInterface $inlineTranslation,
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoFrameworkEscaper $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()->getPostValue();
if (!$post)
$this->_redirect('*/*/');
return;
$this->inlineTranslation->suspend();
try
$postObject = new MagentoFrameworkDataObject();
$postObject->setData($post);
$error = false;
$sender = [
'firstname' => $this->_escaper->escapeHtml($post['firstname']),
'lastname' => $this->_escaper->escapeHtml($post['lastname']),'email' => $this->_escaper->escapeHtml($post['email']),
];
$storeScope = MagentoStoreModelScopeInterface::SCOPE_STORE;
$transport = $this->_transportBuilder
->setTemplateIdentifier('send_email_email_template') // this code we have mentioned in the email_templates.xml
->setTemplateOptions(
[
'area' => MagentoFrameworkAppArea::AREA_FRONTEND, // this is using frontend area to get the template file
'store' => MagentoStoreModelStore::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(
__('Mail send successfully')
);
$this->_redirect('*/*/');
return;
catch (Exception $e)
$this->inlineTranslation->resume();
$this->messageManager->addError(__('Something wrong.'.$e->getMessage())
);
$this->_redirect('*/*/');
return;
I hope its very helpful to you.
thanks for your help, but i have to use plugin for it. Can you suggest me with this?
– trem jenny
Aug 1 at 8:22
yes please update que with your code.
– Anas Mansuri
Aug 1 at 8:26
i have updated my code, thanks.
– trem jenny
Aug 1 at 8:37
where you are facing the issue ?
– Anas Mansuri
Aug 1 at 8:40
i want to create plugin for two fields last name and first name to send email in contact form , but the thing is that i dont know how to do ít, with the code above, i just get the value of the fields. I want to send email with these custom fields.
– trem jenny
Aug 1 at 8:46
|
show 1 more comment
Please try with below url : which have to create module for custom form.
https://magenticians.com/add-custom-field-in-magento-2-contact-page/
Please try with below code :
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoFrameworkMailTemplateTransportBuilder $transportBuilder,
MagentoFrameworkTranslateInlineStateInterface $inlineTranslation,
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoFrameworkEscaper $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()->getPostValue();
if (!$post)
$this->_redirect('*/*/');
return;
$this->inlineTranslation->suspend();
try
$postObject = new MagentoFrameworkDataObject();
$postObject->setData($post);
$error = false;
$sender = [
'firstname' => $this->_escaper->escapeHtml($post['firstname']),
'lastname' => $this->_escaper->escapeHtml($post['lastname']),'email' => $this->_escaper->escapeHtml($post['email']),
];
$storeScope = MagentoStoreModelScopeInterface::SCOPE_STORE;
$transport = $this->_transportBuilder
->setTemplateIdentifier('send_email_email_template') // this code we have mentioned in the email_templates.xml
->setTemplateOptions(
[
'area' => MagentoFrameworkAppArea::AREA_FRONTEND, // this is using frontend area to get the template file
'store' => MagentoStoreModelStore::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(
__('Mail send successfully')
);
$this->_redirect('*/*/');
return;
catch (Exception $e)
$this->inlineTranslation->resume();
$this->messageManager->addError(__('Something wrong.'.$e->getMessage())
);
$this->_redirect('*/*/');
return;
I hope its very helpful to you.
thanks for your help, but i have to use plugin for it. Can you suggest me with this?
– trem jenny
Aug 1 at 8:22
yes please update que with your code.
– Anas Mansuri
Aug 1 at 8:26
i have updated my code, thanks.
– trem jenny
Aug 1 at 8:37
where you are facing the issue ?
– Anas Mansuri
Aug 1 at 8:40
i want to create plugin for two fields last name and first name to send email in contact form , but the thing is that i dont know how to do ít, with the code above, i just get the value of the fields. I want to send email with these custom fields.
– trem jenny
Aug 1 at 8:46
|
show 1 more comment
Please try with below url : which have to create module for custom form.
https://magenticians.com/add-custom-field-in-magento-2-contact-page/
Please try with below code :
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoFrameworkMailTemplateTransportBuilder $transportBuilder,
MagentoFrameworkTranslateInlineStateInterface $inlineTranslation,
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoFrameworkEscaper $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()->getPostValue();
if (!$post)
$this->_redirect('*/*/');
return;
$this->inlineTranslation->suspend();
try
$postObject = new MagentoFrameworkDataObject();
$postObject->setData($post);
$error = false;
$sender = [
'firstname' => $this->_escaper->escapeHtml($post['firstname']),
'lastname' => $this->_escaper->escapeHtml($post['lastname']),'email' => $this->_escaper->escapeHtml($post['email']),
];
$storeScope = MagentoStoreModelScopeInterface::SCOPE_STORE;
$transport = $this->_transportBuilder
->setTemplateIdentifier('send_email_email_template') // this code we have mentioned in the email_templates.xml
->setTemplateOptions(
[
'area' => MagentoFrameworkAppArea::AREA_FRONTEND, // this is using frontend area to get the template file
'store' => MagentoStoreModelStore::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(
__('Mail send successfully')
);
$this->_redirect('*/*/');
return;
catch (Exception $e)
$this->inlineTranslation->resume();
$this->messageManager->addError(__('Something wrong.'.$e->getMessage())
);
$this->_redirect('*/*/');
return;
I hope its very helpful to you.
Please try with below url : which have to create module for custom form.
https://magenticians.com/add-custom-field-in-magento-2-contact-page/
Please try with below code :
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoFrameworkMailTemplateTransportBuilder $transportBuilder,
MagentoFrameworkTranslateInlineStateInterface $inlineTranslation,
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoFrameworkEscaper $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()->getPostValue();
if (!$post)
$this->_redirect('*/*/');
return;
$this->inlineTranslation->suspend();
try
$postObject = new MagentoFrameworkDataObject();
$postObject->setData($post);
$error = false;
$sender = [
'firstname' => $this->_escaper->escapeHtml($post['firstname']),
'lastname' => $this->_escaper->escapeHtml($post['lastname']),'email' => $this->_escaper->escapeHtml($post['email']),
];
$storeScope = MagentoStoreModelScopeInterface::SCOPE_STORE;
$transport = $this->_transportBuilder
->setTemplateIdentifier('send_email_email_template') // this code we have mentioned in the email_templates.xml
->setTemplateOptions(
[
'area' => MagentoFrameworkAppArea::AREA_FRONTEND, // this is using frontend area to get the template file
'store' => MagentoStoreModelStore::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(
__('Mail send successfully')
);
$this->_redirect('*/*/');
return;
catch (Exception $e)
$this->inlineTranslation->resume();
$this->messageManager->addError(__('Something wrong.'.$e->getMessage())
);
$this->_redirect('*/*/');
return;
I hope its very helpful to you.
edited Aug 1 at 8:50
answered Aug 1 at 8:20
Anas MansuriAnas Mansuri
1,3642 silver badges16 bronze badges
1,3642 silver badges16 bronze badges
thanks for your help, but i have to use plugin for it. Can you suggest me with this?
– trem jenny
Aug 1 at 8:22
yes please update que with your code.
– Anas Mansuri
Aug 1 at 8:26
i have updated my code, thanks.
– trem jenny
Aug 1 at 8:37
where you are facing the issue ?
– Anas Mansuri
Aug 1 at 8:40
i want to create plugin for two fields last name and first name to send email in contact form , but the thing is that i dont know how to do ít, with the code above, i just get the value of the fields. I want to send email with these custom fields.
– trem jenny
Aug 1 at 8:46
|
show 1 more comment
thanks for your help, but i have to use plugin for it. Can you suggest me with this?
– trem jenny
Aug 1 at 8:22
yes please update que with your code.
– Anas Mansuri
Aug 1 at 8:26
i have updated my code, thanks.
– trem jenny
Aug 1 at 8:37
where you are facing the issue ?
– Anas Mansuri
Aug 1 at 8:40
i want to create plugin for two fields last name and first name to send email in contact form , but the thing is that i dont know how to do ít, with the code above, i just get the value of the fields. I want to send email with these custom fields.
– trem jenny
Aug 1 at 8:46
thanks for your help, but i have to use plugin for it. Can you suggest me with this?
– trem jenny
Aug 1 at 8:22
thanks for your help, but i have to use plugin for it. Can you suggest me with this?
– trem jenny
Aug 1 at 8:22
yes please update que with your code.
– Anas Mansuri
Aug 1 at 8:26
yes please update que with your code.
– Anas Mansuri
Aug 1 at 8:26
i have updated my code, thanks.
– trem jenny
Aug 1 at 8:37
i have updated my code, thanks.
– trem jenny
Aug 1 at 8:37
where you are facing the issue ?
– Anas Mansuri
Aug 1 at 8:40
where you are facing the issue ?
– Anas Mansuri
Aug 1 at 8:40
i want to create plugin for two fields last name and first name to send email in contact form , but the thing is that i dont know how to do ít, with the code above, i just get the value of the fields. I want to send email with these custom fields.
– trem jenny
Aug 1 at 8:46
i want to create plugin for two fields last name and first name to send email in contact form , but the thing is that i dont know how to do ít, with the code above, i just get the value of the fields. I want to send email with these custom fields.
– trem jenny
Aug 1 at 8:46
|
show 1 more comment
Plugin is not the right route with this particular change. Reason being if you add a field to the contact form template the magento contact controller will automatically catch values for these new fields.
In my extension I add a file field to the contact us form
https://github.com/DominicWatts/ContactAttachment
xml override
https://github.com/DominicWatts/ContactAttachment/blob/master/view/frontend/layout/contact_index_index.xml
New field
https://github.com/DominicWatts/ContactAttachment/blob/master/view/frontend/templates/form.phtml#L45-L50
You are going to have to create a new email template with the new fields
https://github.com/DominicWatts/ContactAttachment/blob/master/view/frontend/email/submitted_form.html#L25-L28
Then within magento add the transactional email. Then under system configuration use the contacts section to configure the use of the new template.
You shouldn't need to touch the controller as shown in other answer as magento automatically picks up other submitted fields.
Although as I've typed all this up I've realised that I've more or less summaried what's covered in the tutorial https://magenticians.com/add-custom-field-in-magento-2-contact-page/
add a comment |
Plugin is not the right route with this particular change. Reason being if you add a field to the contact form template the magento contact controller will automatically catch values for these new fields.
In my extension I add a file field to the contact us form
https://github.com/DominicWatts/ContactAttachment
xml override
https://github.com/DominicWatts/ContactAttachment/blob/master/view/frontend/layout/contact_index_index.xml
New field
https://github.com/DominicWatts/ContactAttachment/blob/master/view/frontend/templates/form.phtml#L45-L50
You are going to have to create a new email template with the new fields
https://github.com/DominicWatts/ContactAttachment/blob/master/view/frontend/email/submitted_form.html#L25-L28
Then within magento add the transactional email. Then under system configuration use the contacts section to configure the use of the new template.
You shouldn't need to touch the controller as shown in other answer as magento automatically picks up other submitted fields.
Although as I've typed all this up I've realised that I've more or less summaried what's covered in the tutorial https://magenticians.com/add-custom-field-in-magento-2-contact-page/
add a comment |
Plugin is not the right route with this particular change. Reason being if you add a field to the contact form template the magento contact controller will automatically catch values for these new fields.
In my extension I add a file field to the contact us form
https://github.com/DominicWatts/ContactAttachment
xml override
https://github.com/DominicWatts/ContactAttachment/blob/master/view/frontend/layout/contact_index_index.xml
New field
https://github.com/DominicWatts/ContactAttachment/blob/master/view/frontend/templates/form.phtml#L45-L50
You are going to have to create a new email template with the new fields
https://github.com/DominicWatts/ContactAttachment/blob/master/view/frontend/email/submitted_form.html#L25-L28
Then within magento add the transactional email. Then under system configuration use the contacts section to configure the use of the new template.
You shouldn't need to touch the controller as shown in other answer as magento automatically picks up other submitted fields.
Although as I've typed all this up I've realised that I've more or less summaried what's covered in the tutorial https://magenticians.com/add-custom-field-in-magento-2-contact-page/
Plugin is not the right route with this particular change. Reason being if you add a field to the contact form template the magento contact controller will automatically catch values for these new fields.
In my extension I add a file field to the contact us form
https://github.com/DominicWatts/ContactAttachment
xml override
https://github.com/DominicWatts/ContactAttachment/blob/master/view/frontend/layout/contact_index_index.xml
New field
https://github.com/DominicWatts/ContactAttachment/blob/master/view/frontend/templates/form.phtml#L45-L50
You are going to have to create a new email template with the new fields
https://github.com/DominicWatts/ContactAttachment/blob/master/view/frontend/email/submitted_form.html#L25-L28
Then within magento add the transactional email. Then under system configuration use the contacts section to configure the use of the new template.
You shouldn't need to touch the controller as shown in other answer as magento automatically picks up other submitted fields.
Although as I've typed all this up I've realised that I've more or less summaried what's covered in the tutorial https://magenticians.com/add-custom-field-in-magento-2-contact-page/
answered Aug 1 at 11:55
Dominic XigenDominic Xigen
3,2281 gold badge5 silver badges18 bronze badges
3,2281 gold badge5 silver badges18 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f284056%2fplugin-for-custom-field-for-contact-form-magento2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown