enter image description hereim trying to make plugin for contact form to send email with the fields:

  • first name

  • last name

  • phone
  • comment
  • email

last name email phone comment

Here my code


namespace Convert\CustomContact\Plugin;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Escaper ;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\Message\ManagerInterface ;

class Post
    const XML_PATH_EMAIL_RECIPIENT = 'contact/email/recipient_email';
    protected $_escaper;
    protected $_contactConfig;
    protected  $_transportBuilder;
    protected $inlineTranslation;
    protected $scopeConfig;
    protected $storeManager;
    protected $messageManager;

    // It's just a regular constructor with classes we want to use in our code
    public function __construct(
            DataPersistorInterface $dataPersistor,
            Context $context,
            Escaper $escaper,
            TransportBuilder $transportBuilder,
            StateInterface $inlineTranslation,
            ScopeConfigInterface $scopeConfig,
            StoreManagerInterface $storeManager,
            ManagerInterface $messageManager
    ) {
        $this->dataPersistor = $dataPersistor;
        $this->context = $context;
        $this->_escaper = $escaper;
        $this->scopeConfig = $scopeConfig;
        $this->storeManager = $storeManager;
        $this->inlineTranslation = $inlineTranslation;
        $this->_transportBuilder =$transportBuilder;
        $this->messageManager = $messageManager;

    public function afterExecute(\Magento\Contact\Controller\Index\Post $subject, $result)

        if ($this->dataPersistor->get('contact_us')) {
            $post = $subject->getRequest()->getPostValue();


            $postObject = new \Magento\Framework\DataObject();

            $error = false;

            $sender = [
            'firstname' => $this->_escaper->escapeHtml($post['firstname']),
            'lastname' => $this->_escaper->escapeHtml($post['lastname'])           

            $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
            $transport = $this->_transportBuilder
            'area' => \Magento\Framework\App\Area::AREA_FRONTEND, 
            'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
            ->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope))
            __('Thanks for contacting us with your comments and questions. We\'ll respond to you very soon.')

             } catch (\Exception $e) {
            __('We can\'t process your request right now. Sorry, that\'s all we know.'.$e->getMessage())

           // return $this->context->getResultRedirectFactory()->create()->setPath('/');
        return $result;


The problem is that when i clicked on submit button of contact page, it didn't get the value of the first name and last name. Instead that, it still took the value of name field and print the alert name is missing. Anyone can help me with this? Thanks.

  • By default, there is no First Name or Last Name in contact form, there is only Name, which can be accessed by executing $subject->getRequest()->getParam('name'). Are you using custom contact form? – Rendy Eko Prastiyo Aug 5 at 9:59
  • yes, i'm using custom form and what i want is to get the value of two custom fields first name and last name – trem jenny Aug 5 at 10:01
  • Could you provide the data that are submitted by the custom form when you submit it? The easiest way if you use Chrome is by pressing F12 -> click on Network tab -> check the Preserve log -> fill in and submit the form -> click on the row that is capturing the form submission -> click on Headers tab. The submitted data are captured in Request Headers accordion. – Rendy Eko Prastiyo Aug 5 at 10:09
  • you mean form data ? – trem jenny Aug 5 at 10:19
  • firstname: Nguyễn lastname: thủy email: thuthuy210.tt@gmail.com telephone: comment: ewgftwg hideit: is that right? – trem jenny Aug 5 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.