0

I am writing a new module (quote request, when user go to the product details page, they can request a quote for the product) I created a form to display in the product details page. When user submit the form, the data should store in the custom table and send email to one of the store admin.

I am stuck at mail sending. After saving the data a mail should go to the store admin and it’s not working, Can you please help. I am using controller to send the mail.

Below is my controller file:

namespace Vendor\QuoteRequest\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Action\Context;
use George\QuoteRequest\Model\QuoteRequestFactory;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Framework\Escaper;


class Index extends Action
{
    /**
     * Recipient email config path
     */
    const XML_PATH_EMAIL_ADMIN_QUOTEREQUEST_EMAIL =   'quoterequest/general/admin_email';

    protected $_modelQuoteRequestFactory;
    /**
     * @var \Magento\Framework\Mail\Template\TransportBuilder\
     */
    protected $_transportBuilder;
    /**
     * @var \Magento\Framework\Translate\Inline\StateInterface
     */
    protected $inlineTranslation;

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

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

    protected $_logger;

    public function __construct(
        \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Framework\Escaper $escaper,
        \Psr\Log\LoggerInterface $logger,
        Context $context,
        QuoteRequestFactory $modelQuoteRequestFactory
    )
    {
        parent::__construct($context);
        $this->_modelQuoteRequestFactory    =   $modelQuoteRequestFactory;
        $this->_transportBuilder    =   $transportBuilder;
        $this->_storeManager    =   $storeManager;
        $this->scopeConfig  =   $scopeConfig;
        $this->inlineTranslation = $inlineTranslation;
        $this->_escaper = $escaper;
    }

    public function execute()
    {
        $post = $this->getRequest()->getPostValue();
        if (!empty($post)) {

            $data = $this->_modelQuoteRequestFactory->create();
            $data->setData($post);
            $data->save();
        }
        $this->inlineTranslation->suspend();
        try {
                $postObject = new \Magento\Framework\DataObject();
                $postObject->setData($post);

                $error = false;

                $sender = [
                    $quoterequester_email    =   $this->getRequest()->getPost('email'),
                    $quoterequester_name     =   $this->getRequest()->getPost('firstaname').''.$this->getRequest()->getPost('lastname'),
                ];
                $storeScope =   \Magento\Store\Model\ScopeInterface::SCOPE_STORE;

                $transport  =   $this->_transportBuilder->setTemplateIdentifier('contact_form')->setTemplateOptions(
                    [
                    'area'=> \Magento\Framework\App\Area::AREA_FRONTEND,
                    'store'=> \Magento\Store\Model\Store::DEFAULT_STORE_ID,]
                )
                    ->setTemplateVars(['data'=> $postObject])
                    ->setFrom($sender)
                    ->addTo($this->scopeConfig->getValue('quoterequest/general/admin_email', $storeScope))
                    ->getTransport();

                $transport->sendMessage();
                $this->inlineTranslation->resume();
                $this->messageManager->addSuccessMessage('Quote Request Send. We\'ll respond to you very soon.');
                $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
                $resultRedirect->setUrl($this->_redirect->getRefererUrl());
                return $resultRedirect;

    }
        catch (\Exception $e) {
            $this->inlineTranslation->resume();
            $this->messageManager->addErrorMessage('We can\'t process your request right now. Sorry, that\'s all we know.'.$e->getMessage());
            $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
            $resultRedirect->setUrl($this->_redirect->getRefererUrl());
            return $resultRedirect;

    }
        $this->_view->loadLayout();
        $this->_view->renderLayout();
    }

}
  • "it’s not working" more information please. error, stacktrace, ... – Philipp Sander Feb 8 at 12:38
  • do all other emails work? – Philipp Sander Feb 8 at 12:38
  • @PhilippSander, Yes all other email works – GeorgeM Feb 8 at 13:43
  • error, stacktrace, ... ??? – Philipp Sander Feb 8 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.