0

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

1

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

<form action="<?= $block->escapeUrl($block->getSaveFormAction()) ?>"
        id="newsletter-form"
        class="newsletter-form"
        method="post"
        data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>"
        data-mage-init='{"validation":{}}'>
    ...
    <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" />
    ...
</form>

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

Namespace/Module/Controller/Index/Save.php

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

You will get newsletter data in form of following variables:

Namespace/Module/view/frontend/email/newsletter.html

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.