0
<?php
/**
 *
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Eaglerocket\Customquote\Controller\Index;


use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\Translate\Inline\StateInterface;



class Index extends Action
{
    private $dataPersistor;
    /**
     * @return \Magento\Framework\Controller\Result\Redirect|\Magento\Framework\View\Result\Page
     */

    protected $context;
  //  private $fileUploaderFactory;
    private $fileSystem;


    /**
     * @var \Magento\Framework\Mail\Template\TransportBuilder
     */
    protected $_transportBuilder;

    /**
     * @var \Magento\Framework\Translate\Inline\StateInterface
     */
    protected $inlineTranslation;

    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    protected $scopeConfig;

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;


    /**
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
     * @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     */

     public function __construct(
        \Magento\Framework\App\Action\Context $context,
       // Filesystem $fileSystem,
        //\Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory,
        \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
        \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
      //  \Eaglerocket\Customquote\Helper\Data $helper,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Store\Model\StoreManagerInterface $storeManager

    ) {
        parent::__construct($context );
      //  $this->fileUploaderFactory = $fileUploaderFactory;
       // $this->fileSystem          = $fileSystem;,$transportBuilder,$inlineTranslation, $scopeConfig
                $this->_context = $context;

        $this->_transportBuilder = $transportBuilder;
        $this->inlineTranslation = $inlineTranslation;
    //    $this->helper = $helper;
        $this->scopeConfig = $scopeConfig;
        $this->_storeManager = $storeManager;

    }

    public function execute()
    {

        $post = $this->getRequest()->getPostValue();
       // $filesData = $this->getRequest()->getFiles('upload_document');

       

        $txt='<table>';

       
        if($post['comment1']){
            $txt.='<tr><td><strong>Comment</strong>:'.$post['comment1'].'</td></tr>';
        }
        if($post['comment2']){
            $txt.='<tr><td><strong>Comment</strong>:'.$post['comment2'].'</td></tr>';
        }if($post['comment3']){
            $txt.='<tr><td><strong>Comment</strong>:'.$post['comment3'].'</td></tr>';
        }
        $txt.='</table>';
      




$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 Auriga!!.'
                );
$from = array('email' => "[email protected]", 'name' => 'Name of Sender');
$this->inlineTranslation->suspend();
$to = array('[email protected]');

$storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
$templateId = $this->scopeConfig->getValue ( 'eaglerocket/customquote/email_template', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId );

$transport = $this->_transportBuilder->setTemplateIdentifier($templateId, $storeScope)->setTemplateOptions($templateOptions)
            ->setTemplateVars($templateVars)
            ->setFrom($from)
            ->addTo($to)
            ->getTransport();
$transport->sendMessage();
$this->messageManager->addSuccess(__('Mail Sent Successfully'));
$this->inlineTranslation->resume();




        $this->inlineTranslation->resume();

        $this->messageManager->addSuccess(__('Form successfully submitted'));

        $this->_redirect('post_requirements');
    }

}

1 Answer 1

0

You didn't inject storeManager in construct. Add like this in your construct :

protected $storeManager;

public function __construct(
    .....
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    .....
) {
    $this->storeManager = $storeManager;
}

Now remove generated and clean cache.

11
  • Please check the updated code, new error...............Error: Call to a member function getStore() on null in /var/www/demo/app/code/Eaglerocket/Customquote/Controller/Index/Index.php:143
    – kks
    Jun 28, 2021 at 5:35
  • use $this->storeManager->getStore()->getId() instead of $this->storeManager->getStore(); Jun 28, 2021 at 5:38
  • Error: Call to a member function getStore() on null
    – kks
    Jun 28, 2021 at 5:45
  • Not possible that way. Can you please update $this->_storeManager = $storeManager;? You need to use $this->storeManager = $storeManager; Jun 28, 2021 at 6:28
  • yes it worked, could you please tell me how to get the template id(as it is empty) cause new error is Exception #0 (UnexpectedValueException): Email template '' is not defined.
    – kks
    Jun 28, 2021 at 6:55

Your Answer

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

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