0

I have a custom form, when the customer submit the form i am sending mail to the customer. When i try to add specific mail id like addTo('mani@gmail.com') function i am receiving mail to mentioned email id.

But when i take user mentioned mail id like addTo($post['emailaddress']) i am not able to receive mail.

this is my code for mail. I am not sure why its not working.Please help me to find what is wrong in my code.

Thanks.

<?php
namespace Mani\Popup\Controller\Action;
 
use Zend\Log\Filter\Timestamp;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\Controller\ResultFactory; 
 
class Luma extends \Magento\Framework\App\Action\Action
{
        /**
        * Recipient email config path
        */
        /**
        * @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;
        /**
        * @var \Magento\Framework\Escaper
        */
        protected $_escaper;

        /**
        * @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
        * @param \Magento\Store\Model\StoreManagerInterface $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\Store\Model\StoreManagerInterface $storeManager,
            \Magento\Framework\Escaper $escaper
        ) {
            parent::__construct($context);
            $this->_transportBuilder = $transportBuilder;
            $this->inlineTranslation = $inlineTranslation;
            $this->scopeConfig = $scopeConfig;
            $this->storeManager = $storeManager;
            $this->_escaper = $escaper;
        }

        /**
         * Post user question
         *
         * @return void
         * @throws \Exception
         */
        public function execute()
        {
            $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
            $post = $this->getRequest()->getPostValue();
            if (!$post) {
                $this->_redirect('/');
                return;
            }

            $this->inlineTranslation->suspend();

            try {
                $postObject = new \Magento\Framework\DataObject();
                $postObject->setData($post);
                $error = false;

                $sender = [
                    'name' => $this->_escaper->escapeHtml('luma store'),
                    'email' => $this->_escaper->escapeHtml('noreplyluma@gmail.com'),
                ];
                $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE; 
                $transport = $this->_transportBuilder
                    ->setTemplateIdentifier('carehomecatering_email') // this code we have mentioned in the email_templates.xml
                    ->setTemplateOptions(
                        [
                            'area' => 'frontend',                    
                            'store' => $this->storeManager->getStore()->getId() 
                        ]
                    )
                    ->setTemplateVars([
                        'firstname' => $post['firstname'],
                        'lastname' => $post['lastname'],
                        'carehome' => $post['carehome'],
                        'email' => $post['emailaddress']
                        
                    ])
                    ->setFrom($sender)
                    ->addTo($post['emailaddress'])
                    ->getTransport();

                    $transport->sendMessage();
                    $this->inlineTranslation->resume();
                    $this->messageManager->addSuccess(
                        __('Thanks for contacting us with your comments and questions. We\'ll respond to you very soon.')
                   );
                    
                    return;
            } catch (\Exception $e) {
                $this->inlineTranslation->resume();
                $this->messageManager->addError(__('We can\'t process your request right now. Sorry, that\'s all we know.'.$e->getMessage())
               );
                return;
            }
        }

}        
 
2
  • Does it work if instead of hardcoding 'mani@gmail.com' you enter the same email in the form email address field? Jun 2 at 18:06
  • No, and what i observed is if i use outlook mail id in the form i am able to receive the mail without any hardcoding. But when i use gmail id in the form i not receiving the mail.
    – Mani
    Jun 3 at 3:52

0

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.