3

I created custom form module, if customer submit the form i need to get all values by email.

My module files are

Customform\ReturnRequest\Block\Index.php

<?php

namespace Customform\ReturnRequest\Block;

class Index extends \Magento\Framework\View\Element\Template
{

public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    array $data = []
)
{
    parent::__construct($context, $data);
   }

  public function getFormAction() 
 { 
  return '/returnrequest/index/index';   
 }
}

Customform\ReturnRequest\Controller\Index\Index.php

<?php

namespace Customform\ReturnRequest\Controller\Index;

use Magento\Framework\Controller\ResultFactory;

class Index extends \Magento\Framework\App\Action\Action
{
 protected $_pageFactory;

 public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\View\Result\PageFactory $pageFactory)
 {
    $this->_pageFactory = $pageFactory;
    return parent::__construct($context);
 }

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

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

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

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

        return $resultRedirect;
     }


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

Customform\ReturnRequest\etc\frontend\routes.xml

 <?xml version="1.0" ?>
 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
   <router id="standard">
    <route frontName="returnrequest" id="returnrequest">
        <module name="Customform_ReturnRequest"/>
    </route>
  </router>
 </config>

Customform\ReturnRequest\view\frontend\layout\returnrequest_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
 <referenceContainer name="content">
    <block class="Customform\ReturnRequest\Block\Index" name="helloworld_index_index" template="Customform_ReturnRequest::index.phtml" />
 </referenceContainer>
</page>

Customform\ReturnRequest\view\frontend\templates\index.phtml

   <form id="contact-form" class="form-horizontal" method="post" enctype="multipart/form-data" action="<?php echo $block->getFormAction()?>">

      <input name="name" id="name" class="input-text" type="text" />
      <input name="email" id="email" class="input-text" type="email" />          

      <button type="submit" id="add" title="" class="action submit primary">
            <span><?= $block->escapeHtml(__('Request Return')) ?></span>
        </button>

    </form>

My form displaying good. i need to set only email function. Please let me know . Thanks

1 Answer 1

6

You have to create email_templates.xml

Customform\ReturnRequest\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="hello_template" label="Hello World" file="hello.html" type="html" module="Webkul_Hello" area="frontend"/>
</config>

Now create email template file hello.html in folder

Customform/ReturnRequest/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 message}}
                                        </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"}}

Now Update your controller with my code:

Customform\ReturnRequest\Controller\Index\Index.php

    <?php

namespace Customform\ReturnRequest\Controller\Index;

use Magento\Framework\Controller\ResultFactory;

class Index 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;
    return parent::__construct($context);
 }

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

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

        // 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(),
            'customer_name' => 'John Doe',
            'message'    => 'Hello World!!.'
            );
        $from = array('email' => "[email protected]", 'name' => 'Name of Sender');
        $this->inlineTranslation->suspend();
        $to = array('[email protected]');
        $transport = $this->_transportBuilder->setTemplateIdentifier('hello_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('/returnrequest/index/index');

        return $resultRedirect;
     }


    return $this->_pageFactory->create();
   }
 }
  • php bin/magento setup:upgrade
  • php bin/magento cache:clean
11
  • hello file is an html or else phtml?
    – Priya
    Jun 26, 2019 at 12:58
  • 1
    It is HTML it contains your email template content
    – Ravi Soni
    Jun 26, 2019 at 13:01
  • Thank you, let me try
    – Priya
    Jun 26, 2019 at 13:04
  • I didn't get email
    – Priya
    Jun 26, 2019 at 13:22
  • 1
    Yeah i had Done
    – Priya
    Jun 27, 2019 at 4:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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