1

I would like to send a mail after a user fill out the form.

The form is OK but now I want to use values in $post to send a mail to a mail address (the mail address of the company)

I'm beginner and I found a lot of tutorials but it's not what I search.

Can someone help me to start ?

Thanks.

<?php

namespace Kg\KgContent\Controller\Index;

use Magento\Framework\Controller\ResultFactory;

class Catalog extends \Magento\Framework\App\Action\Action
{
    /**
     * Catalog action
     *
     * @return void
     */
    public function execute()
    {
        // 1. POST request : Get catalog data
        $post = (array) $this->getRequest()->getPost();

        if (!empty($post)) {
            // Retrieve your form data
            $firstname   = $post['firstname'];
            $lastname    = $post['lastname'];
            $phone       = $post['phone'];

            // Doing-something with...


            // Display the success form validation message
            $this->messageManager->addSuccessMessage('Votre demande de catalogue a bien été prise en compte');

            // Redirect to your form page (or anywhere you want...)
            $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
            $resultRedirect->setUrl('/kgkgcontent/index/catalog');

            return $resultRedirect;
        }
        // 2. GET request : Render the catalog page
        $this->_view->loadLayout();
        $this->_view->renderLayout();
    }
}
0

Your controller file

    <?php
    namespace  Kg\KgContent\Controller\Index;

    use Magento\Framework\Controller\ResultFactory;

    class Catalog extends \Magento\Framework\App\Action\Action
    {
     protected $storeManager;
     protected $_transportBuilder;
     protected $inlineTranslation;
     protected $_pageFactory;

     public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
        \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
        \Magento\Framework\View\Result\PageFactory $pageFactory)
     {
        $this->storeManager = $storeManager;
        $this->_transportBuilder = $transportBuilder;
        $this->inlineTranslation = $inlineTranslation;
        $this->_pageFactory = $pageFactory;
        parent::__construct($context); 
     }

     public function execute()
     {      
        $post = (array) $this->getRequest()->getPost();

        if (!empty($post)) {
            // Retrieve your form data

                $firstname   = $post['firstname'];
                $lastname    = $post['lastname'];
                $phone       = $post['phone'];

            // Display the succes form validation message
            $this->messageManager->addSuccessMessage('Sucessfully you had done your returns order !');

            $templateOptions = array('area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $this->storeManager->getStore()->getId());
            $templateVars = array(
                'store' => $this->storeManager->getStore(),
                'firstname' =>$firstname,
                'lastname'    => $lastname,
                'phone'    => $phone
                );
            $from = array('email' => "email@gmail.com", 'name' => 'Name of Sender');
            $this->inlineTranslation->suspend();
            $to = array('email@gmail.com');
            $transport = $this->_transportBuilder->setTemplateIdentifier('email_template')
                ->setTemplateOptions($templateOptions)
                ->setTemplateVars($templateVars)
                ->setFrom($from)
                ->addTo($to)
                ->getTransport();
            $transport->sendMessage();
            $this->inlineTranslation->resume();

            // Redirect to your form page (or anywhere you want...)
            $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
            $resultRedirect->setUrl('/kgkgcontent/index/catalog');

            return $resultRedirect;
         }


        return $this->_pageFactory->create();
       }
     }

You have to create email_templates.xml

Kg/KgContent\etc

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd">
    <template id="email_template" label="Kg KgContent" file="custom.html" type="html" module="Kg_KgContent" area="frontend"/>
</config>

Now create email template file custom.html in folder

Kg/KgContent/view/frontend/email

 <!--@subject Email Subject @-->
    <!--@vars
    {"store url=\"\"":"Store Url",
    "skin url=\"images/logo_email.gif\" _area='frontend'":"Email Logo Image"}
    @-->
    <!--@styles
    body,td { color:#2f2f2f; font:11px/1.35em Verdana, Arial, Helvetica, sans-serif; }
    @-->
    {{template config_path="design/email/header_template"}}
        <table cellspacing="0" cellpadding="0" border="0" width="100%">
            <tr>
                <td align="center" valign="top" style="padding:20px 0 20px 0">
                    <table bgcolor="#FFFFFF" cellspacing="0" cellpadding="10" border="0" width="650" style="border:1px solid #E0E0E0;">
                        <tr>
                            <td valign="top">
                                <h1 style="font-size:22px;font-weight:normal;line-height:22px;margin:0 0 11px 0;">{{trans "Hello"}},</h1>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <table cellspacing="0" cellpadding="0" border="0" width="650">
                                    <tbody>
                                        <tr>
                                            <td colspan="2" valign="top" style="font-size:12px;padding:7px 9px 9px 9px;border:1px solid #EAEAEA;">
                                                {{var firstname}}
                                            </td>
<td colspan="2" valign="top" style="font-size:12px;padding:7px 9px 9px 9px;border:1px solid #EAEAEA;">
                                            {{var lastname}}
                                        </td>
                                        <td colspan="2" valign="top" style="font-size:12px;padding:7px 9px 9px 9px;border:1px solid #EAEAEA;">
                                            {{var phone}}
                                        </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </td>
                        </tr>
                        <tr>
                            <td bgcolor="#EAEAEA" align="center" style="background:#EAEAEA;text-align:center;">
                                <center>
                                    <p style="font-size:12px;margin:0;">
                                        <strong>{{trans "Thank you"}}</strong>
                                    </p>
                                </center>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    {{template config_path="design/email/footer_template"}}
| improve this answer | |
  • Did you try this? – Arunprabakaran M Jul 3 '19 at 14:45
  • Kindly response. If you get an answer, mark answer is correct and upvote – Arunprabakaran M Jul 3 '19 at 14:57
  • Yes, I try but I have this error 1 exception(s): Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object: Kg\KgContent\Controller\Index\Catalog\Interceptor – Robert Jul 3 '19 at 15:10
  • can you post your code? – Arunprabakaran M Jul 3 '19 at 15:13
  • I copy paste the 3 files you get. I can post but it's the same you gave me ;) I set the code on the post – Robert Jul 3 '19 at 15:16

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.