I want to get values from this form fields and put into email template. What I was suppose to do? For every field I need to create custom variable, and bind it on field? Documentation is not too informative, as I think. Demo pop-up


You can create a <form> and assign input field name as follows:

<form action="<?= $block->escapeUrl($block->getSaveFormAction()) ?>"
        data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>"
    <input type="text" name="newsletter[firstname]" placeholder="First Name" />
    <input type="text" name="newsletter[lastname]" placeholder="Last Name" />
    <input type="text" name="newsletter[email]" placeholder="Email Address" />

So you can get form values in your controller as follows:


public function execute() {
    $post = (array) $this->getRequest()->getPost();
    // Get newsletter data
    $newsletterData = $post['newsletter'];
    $transport = $this->transportBuilder
                    'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                    'store' => $storeId()
            ->setTemplateVars($newsletterData) // set newsletter data in email template
            ->setFrom(['email' => $fromEmail, 'name' => $fromName])
            ->setReplyTo($fromEmail, $fromName)

You will get newsletter data in form of following variables:


First Name: {{var firstname}}
Last Name: {{var lastname}}
Email Address: {{var email}}

I hope this may helpful to you!!

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.