Sending Form Data as Email in Magento
In Magento, sending form data to an email address is a common requirement for contact forms or any other interactive page on your site. The built-in Contact Us module provides a good starting point.
Using the Built-in Contact Form
-
Enable and configure the Contact Us module:
- In the Magento admin panel, navigate to Stores > Configuration > General
- Expand the Contacts section.
- Set “Enabled” to Yes.
- Enter your email address under “Contact Email”.
- Click Save Config.
-
Add the Contact Us block: You can add this block in any CMS page or static block.
-
Open a CMS page or create a new one from Content > Pages.
-
Click on the source code editor to switch to HTML mode.
-
Add the following code:
{{block type="core/contact_form" template="contact/form.phtml"}}
-
-
Test the form: Access your Magento site and navigate to the page with the contact block.
- Fill out the form fields and submit it.
- You should receive an email at the specified address containing the submitted data.
Sending Form Data from a Static Block
If you want to send form data from a static block (for example, if your form is in a sidebar or header), you’ll need to create your own controller action. Here’s how:
-
Create a new module: In app/code, create a new folder for your custom module.
- Create the following files: registration.php, config.xml, etc/modules/Your_Module.xml.
-
Create a new controller:
-
Under Your_Module, create the Controllers directory and inside that, ContactController.php
-
Add the following code:
namespace Your_Module\Controller\Contact; use Magento\Framework\App\Action\HttpPostActionInterface; use Magento\Framework\Controller\ResultFactory; class Index implements HttpPostActionInterface { protected $inlineTranslation; protected $transportBuilder; public function __construct( \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation, \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder ) { $this->inlineTranslation = $inlineTranslation; $this->transportBuilder = $transportBuilder; } public function execute() { $this->inlineTranslation->suspend(); // Get form data from request (you can use $_POST or a form model) $data = [ 'name' => 'John Doe', 'email' => 'john.doe@example.com', 'message' => 'Hello, this is a test email.', ]; $templateVars = ['data' => $data]; $fromEmail = 'admin@mystore.com'; $toEmail = 'admin@mystore.com'; // Build and send the email $transport = $this->transportBuilder ->setTemplateIdentifier('your_template_id') ->setTemplateOptions([ 'area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID, ]) ->setTemplateVars($templateVars) ->setFrom($fromEmail) ->addTo($toEmail) ->getTransport(); try { $transport->sendMessage(); } catch (\Exception $e) { // Log or handle exception } $this->inlineTranslation->resume(); return $resultFactory->create(ResultFactory::TYPE_REDIRECT)->setUrl('/'); } }
-
-
Create an email template: Navigate to Stores > Configuration > Sales > Sales Emails.
- Click on Add New Template under General Settings
- Set the following:
- Name: Your Custom Form Email
- Template: your_template_id (use a unique ID)
- Subject: Contact Form Submission
-
Add the form to a static block: In your static block, create an HTML form that posts data to
https://your-magento-site.com/your-module/contact/index. The action URL is based on the router configuration. -
Test the custom form: Access your Magento site, navigate to the page with the static block containing the form.
- Fill out the form fields and submit it.
- You should receive an email at the specified address (admin@mystore.com in this example) containing the submitted data.
Conclusion
Sending form data as emails is a common requirement for Magento sites. The built-in Contact Us module provides a straightforward solution, but if you need more customization or integration with other parts of your site, creating a custom controller and email template is the way to go.
Remember that proper error handling and security measures are essential when dealing with form submissions and sensitive data. Always validate user input on both client and server sides.
For more advanced form handling in Magento, consider exploring third-party extensions or custom development solutions for complex needs.
Happy coding!