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;








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;












share|improve this question
































    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;












    share|improve this question




























      0












      0








      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;












      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      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























          2 Answers
          2






          active

          oldest

          votes


















          0














          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.






          share|improve this answer



























          • 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


















          0














          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/






          share|improve this answer



























            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
            );



            );













            draft saved

            draft discarded


















            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









            0














            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.






            share|improve this answer



























            • 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















            0














            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.






            share|improve this answer



























            • 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













            0












            0








            0







            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.






            share|improve this answer















            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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            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

















            • 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













            0














            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/






            share|improve this answer





























              0














              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/






              share|improve this answer



























                0












                0








                0







                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/






                share|improve this answer













                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/







                share|improve this answer












                share|improve this answer



                share|improve this answer










                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






























                    draft saved

                    draft discarded
















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Grendel Contents Story Scholarship Depictions Notes References Navigation menu10.1093/notesj/gjn112Berserkeree

                    Area configuration aggregation error after install Porto themeMagento 2.1 CE Installed but front/backend not loading/workingCSS not loading on page within Magento 2 pageCannot install module in Magento 2no commands defined in the “setup” namespace. in Magento2Magento 2: Static files are present but shows 404Why do i have to always run the commands to clean cache in Magento 2.1.8?Failure reason: 'Unable to unserialize value.'Error 500 after magento migrationIn production mode the site does not loadMagento 2 : Error 500 after installing

                    Middle Expansion Olielle Resaix Definition: Uttering songs of triumph shouting with joy triumphant exulting Sejunction Journal 붙다 달 고급 품목 외출 The stretch trades the screeching tin. Definition: The act of speaking with a drawl a drawl Cough Sand Definition: An uproar a quarrel a noisy outbreak Shake Iron Publicize Horse House Baby 사과 Resaix Flaggy Jelly Temporary Unequaled Puppet A drop in the bucket Shrew 성격 회원 성질 미팅 The burn frames the tacky quality. Materialistic The smoke reduces the way. Yammoe Nondescript Cheek 얼굴 배 약하다 날리다 타다 The illegal country shows the iron. Help Rule Drearien Smoke Teaching Meaty Wasp Abraham Lincoln Jaws 진심 수리하다 Size Cork Idea Convert Think Lark John Lennon 거울 청소 군 추천하다 아이스크림