0

Here I am using default contact us email template from admin panel magento 2.

app/code/Vendor/Module/Controller/Index/Contact.php

<?php

    namespace Vendor\Module\Controller\Index;


    use Magento\Framework\App\Action\Action;
    use Magento\Framework\Controller\ResultFactory; 
    use Magento\Framework\App\Request\DataPersistorInterface;



    class Contact extends Action
    {
            private $dataPersistor;

        protected $_transportBuilder;
        protected $inlineTranslation;
        protected $scopeConfig;
        protected $_messageManager;
        protected $_storeManager;


        public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
        \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Framework\Message\ManagerInterface $messageManager,
                \Magento\Store\Model\StoreManagerInterface $storeManager,        
                        array $data = []
       ) {
            parent::__construct($context,$transportBuilder,$inlineTranslation, $scopeConfig,$data,$storeManager);
            $this->_transportBuilder = $transportBuilder;
            $this->inlineTranslation = $inlineTranslation;
            $this->scopeConfig = $scopeConfig;
            $this->_messageManager = $messageManager;
            $this->_storeManager = $storeManager;   
        }

        public function execute()
        {
            $post = $this->getRequest()->getPostValue();
            $fromEmail= 'shop@xyz.com';
            $fromName = 'Email Testing';

            $templateVars = [
            'name' => $post['firstname'],
            'email' => $post['email'],
            'telephone' => $post['phone'],
            'message'   => $post['message']
            ];
            $from = ['email' => $fromEmail, 'name' => $fromName];
            $this->inlineTranslation->suspend();


            $to = 'dyane@gmail.com';     

            $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;

            $templateOptions = [
            'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
            'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID
            ];

            $transport = $this->_transportBuilder->setTemplateIdentifier(6, $storeScope)
            ->setTemplateOptions($templateOptions)
            ->setTemplateVars($templateVars)
            ->setFrom($from)
            ->addTo($to)            
            ->getTransport();
            $transport->sendMessage();
            $this->inlineTranslation->resume();

            $this->messageManager->addSuccess(__('You sent the contact details successfully.'));
            $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
            $resultRedirect->setUrl($this->_redirect->getRefererUrl());
            return $resultRedirect;

        }
    }       

Below is Error

This page isn’t working
site is currently unable to handle this request.
HTTP ERROR 500
  • can you check your error logs file in 'var/log/' folder. ? – Shashank Kumrawat Oct 9 '18 at 6:49
  • no error related it in system.log file – Hitesh Balpande Oct 9 '18 at 6:50
  • Do you have the mail server installed on your server? – Sourabh Kumar Sharma Oct 9 '18 at 6:51
  • 1
    check your apache/nginx log /var/log/apache/error.log – Murtuza Zabuawala Oct 9 '18 at 6:58
  • sorry but there is no such apache inside log directory – Hitesh Balpande Oct 9 '18 at 6:59

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.