How to Send Mail using Custom Form

 How to Send Mail using Custom Form


How to Send Mail using Custom Form 

Step 1) - Create a email_templates.xml file in - app\code\EnquiryForm\Enquiry\etc\email_templates.xml


<?xml version="1.0" encoding="UTF-8"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd">

    <template id="email_section_sendmail_email_template" label="Email Form" file="email_file.html" type="html" module="EnquiryForm_Enquiry" area="frontend"/>

</config>


Step 2) Create a file in email_file.html- app\code\EnquiryForm\Enquiry\View\frontend\email\email_file.html


{{template config_path="design/email/header_template"}}

<table>

    <tr class="email-intro">

        <td>

            Email: {{var email}}

        </td>

        <td>

            Name: {{var name}}

        </td>

    </tr>

</table>

{{template config_path="design/email/footer_template"}}


Step 3) Create a file in Save.php- app\code\EnquiryForm\Enquiry\Controller\Form\Save.php


<?php

namespace Wo\EyeTest\Controller\Form;

use Zend\Log\Filter\Timestamp;

use Magento\Store\Model\StoreManagerInterface;


class Postmail extends \Magento\Framework\App\Action\Action

{

    const XML_PATH_EMAIL_RECIPIENT_NAME = 'trans_email/ident_support/name';

    const XML_PATH_EMAIL_RECIPIENT_EMAIL = 'trans_email/ident_support/email';


    protected $_inlineTranslation;

    protected $_transportBuilder;

    protected $_scopeConfig;

    protected $_logLoggerInterface;

    protected $storeManager;


    public function __construct(

        \Magento\Framework\App\Action\Context $context,

        \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,

        \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,

        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,

        \Psr\Log\LoggerInterface $loggerInterface,

        StoreManagerInterface $storeManager,

        array $data = []

    )

    {

        $this->_inlineTranslation = $inlineTranslation;

        $this->_transportBuilder = $transportBuilder;

        $this->_scopeConfig = $scopeConfig;

        $this->_logLoggerInterface = $loggerInterface;

        $this->messageManager = $context->getMessageManager();

        $this->storeManager = $storeManager;

        parent::__construct($context);

    }

    public function execute()

    {

        $post = $this->getRequest()->getPost();

        try

        {

            // Send Mail

            $this->_inlineTranslation->suspend();


            $sender = [

                'name' => $post['name'],

                'email' => $post['email']

            ];

            $sentToEmail = $this->_scopeConfig ->getValue('trans_email/ident_general/email',\Magento\Store\Model\ScopeInterface::SCOPE_STORE);

            $sentToName = $this->_scopeConfig ->getValue('trans_email/ident_general/name',\Magento\Store\Model\ScopeInterface::SCOPE_STORE);


            $transport = $this->_transportBuilder

                ->setTemplateIdentifier('eyetest_email_template')

                ->setTemplateOptions(

                    [

                        'area' => 'frontend',

                        'store' => $this->storeManager->getStore()->getId()

                    ]

                )

                ->setTemplateVars([

                    'name'  => $post['name'],

                    'email'  => $post['email']

                ])

                ->setFrom($sender)

                ->addTo($sentToEmail,$sentToName)

                ->addTo('chapaie@hotmail.com','owner')

                ->getTransport();

            $transport->sendMessage();

            $this->_inlineTranslation->resume();

            $this->messageManager->addSuccess('Email sent successfully');

            $this->_redirect('eyetest/index/index');


        } catch(\Exception $e){

            $this->messageManager->addError($e->getMessage());

            $this->_logLoggerInterface->debug($e->getMessage());

            exit;

        }

    }

}


And save Data in Database and send mail - 

Create a Save.php file in - app\code\EnquiryForm\Enquiry\Controller\Form\Save.php


<?php

namespace EnquiryForm\Enquiry\Controller\Form;

use Zend\Log\Filter\Timestamp;

use Magento\Store\Model\StoreManagerInterface;

use Magento\Framework\App\Filesystem\DirectoryList;

use Magento\MediaStorage\Model\File\UploaderFactory;

use Magento\Framework\Image\AdapterFactory;

use Magento\Framework\Filesystem;

class Save extends \Magento\Framework\App\Action\Action {

    const XML_PATH_EMAIL_RECIPIENT_NAME = 'trans_email/ident_support/name';

    const XML_PATH_EMAIL_RECIPIENT_EMAIL = 'trans_email/ident_support/email';

    protected $_inlineTranslation;

    protected $_transportBuilder;

    protected $_scopeConfig;

    protected $_logLoggerInterface;

    protected $storeManager;

    protected $modelFactory;

    protected $uploaderFactory;

    protected $_mediaDirectory;

    protected $adapterFactory;

    protected $filesystem;

    public function __construct(\Magento\Framework\App\Action\Context $context, \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation, \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Psr\Log\LoggerInterface $loggerInterface, StoreManagerInterface $storeManager, \EnquiryForm\Enquiry\Model\DocumentFactory $modelFactory, UploaderFactory $uploaderFactory, AdapterFactory $adapterFactory, Filesystem $filesystem, array $data = []) {

        $this->_inlineTranslation = $inlineTranslation;

        $this->_transportBuilder = $transportBuilder;

        $this->_scopeConfig = $scopeConfig;

        $this->_logLoggerInterface = $loggerInterface;

        $this->messageManager = $context->getMessageManager();

        $this->modelFactory = $modelFactory;

        $this->uploaderFactory = $uploaderFactory;

        $this->adapterFactory = $adapterFactory;

        $this->filesystem = $filesystem;

        $this->storeManager = $storeManager;

        $this->_mediaDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);

        parent::__construct($context);

    }

    public function execute() {

        $post = $this->getRequest()->getPost();

        try {

            // Send Mail

            $this->_inlineTranslation->suspend();

            $sender = ['name' => $post['name'], 'email' => $post['email']];

            $sentToEmail = $this->_scopeConfig->getValue('trans_email/ident_general/email', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

            $sentToName = $this->_scopeConfig->getValue('trans_email/ident_general/name', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

            $transport = $this->_transportBuilder->setTemplateIdentifier('email_section_sendmail_email_template')->setTemplateOptions(['area' => 'frontend', 'store' => $this->storeManager->getStore()->getId() ])->setTemplateVars(['name' => $post['name'], 'email' => $post['email']])->setFrom($sender)->addTo($sentToEmail, $sentToName)->addTo('jagservetechnology@gmail.com', 'owner')->addBcc([$post['email']])->getTransport();

            $transport->sendMessage();

            $this->_inlineTranslation->resume();

            $data = $this->getRequest()->getParams();

            $Cmessagedata = $this->modelFactory->create();

            if ($data) {

                $files = $this->getRequest()->getFiles();

                if (isset($files['upload_file']) && !empty($files['upload_file']["name"])) {

                    try {

                        $files = $this->getRequest()->getFiles('file');

                        $target = $this->_mediaDirectory->getAbsolutePath('EnquiryForm_enquiry/');

                        //attachment is the input file name posted from your form

                        $uploader = $this->uploaderFactory->create(['fileId' => 'upload_file']);

                        $_fileType = $uploader->getFileExtension();

                        $uniqid = uniqid();

                        $newFileName = $uniqid . '.' . $_fileType;

                        $uploader->setAllowedExtensions(['jpg', 'jpeg', 'png']);

                        $uploader->setAllowRenameFiles(true);

                        $result = $uploader->save($target, $newFileName);

                        //Set file path with name for save into database

                        $post_data = $this->getRequest()->getPostValue();

                        $post_data['upload_file'] = $newFileName;

                        $Cmessagedata->setData($post_data);

                        $Cmessagedata->save();

                        $this->messageManager->addSuccess(__("Save Data Successfully"));

                        $this->resultPageFactory->create();

                        return $resultRedirect->setPath('enquiry/form/index');

                    }

                    catch(\Exception $e) {

                        $this->messageManager->addErrorMessage($e->getMessage());

                        return $resultRedirect->setPath('enquiry/form/index');

                    }

                } else {

                    $post_data = $this->getRequest()->getPostValue();

                    $Cmessagedata->setData($post_data);

                    $Cmessagedata->save();

                    $this->messageManager->addSuccess(__("Save Data Successfully"));

                    $this->resultPageFactory->create();

                    return $resultRedirect->setPath('enquiry/form/index');

                }

            }

        }

        catch(\Exception $e) {

            $this->messageManager->addError($e->getMessage());

            $this->_logLoggerInterface->debug($e->getMessage());

            $this->_redirect('enquiry/form/index');

        }

    }

}



 

Deepak Kumar Bind

Success is peace of mind, which is a direct result of self-satisfaction in knowing you made the effort to become the best of which you are capable.

Post a Comment

If you liked this post please do not forget to leave a comment. Thanks

Previous Post Next Post