0

I created a custom module to send emails and on it was working good. Today I moved the website on another domain and I noticed that the module sometimes works and sometimes it does not.

I would say that 90% does not work. After a setup:upgrade or deploy it works. The weird thing is that it is ALWAYS showing the success message, but most of the time the message is not sent. I really don't know how to figure out where is the problem. It is driving me crazy!

Do you guys have any suggestions? Just one thing, the website now is https (it was HTTP before). Is there something I need to change (of course I updated the base URL - secure and unsecured)?

Here is the code:

controller/index/post.php

<?php
namespace company\module\Controller\Index;

use Zend\Log\Filter\Timestamp;

class Post 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;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Psr\Log\LoggerInterface $loggerInterface,
        \company\module\Helper\Data $helperData, // helper
        array $data = []

        )
    {
        $this->_inlineTranslation = $inlineTranslation;
        $this->messageManager = $messageManager;
        $this->_transportBuilder = $transportBuilder;
        $this->_scopeConfig = $scopeConfig;
        $this->_logLoggerInterface = $loggerInterface;
        $this->helperData = $helperData;


        parent::__construct($context);


    }

    public function execute()
    {
        $post = $this->getRequest()->getPost();
        $emailFromAdmin = $this->helperData->getGeneralConfig('email-C');
        try
        {
            // Send Mail
            $this->_inlineTranslation->suspend();
            $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;

            $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('customemail_email_template')
            ->setTemplateOptions(
                [
                    'area' => 'frontend',
                    'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                ]
                )
                ->setTemplateVars([
                    'name'  => $post['name'],
                    'email'  => $post['email'],
                    'telephone' => $post['telephone'],
                    'comment' => $post['comment']
                ])
                ->setFrom($sender)
                //->addTo($sentToEmail,$sentToName)
                ->addTo($emailFromAdmin,'owner')
                ->getTransport();

                $transport->sendMessage();

                $this->_inlineTranslation->resume();
                $this->messageManager->addSuccessMessage(
                    __('Thanks for contacting us with your comments and questions. We\'ll respond to you very soon.')
                );
                $this->_redirect('charlotte'); //NEED TO CHANGE WITH THE SAME URL OF THE CONTACT WHERE IT'S PLACED

        } catch(\Exception $e){
            $this->messageManager->addErrorMessage(
                __('An error occurred while processing your form. Please try again later.')
            );
            $this->_logLoggerInterface->debug($e->getMessage());
            exit;
        }



    }
}

Please, please, help me!

  • Please also add your code in question. – Sweety Masmiya Nov 20 '19 at 3:56
  • I added the code, but I just realized that it is working now. I didn't change absolutely NOTHING in the code and now it works. The only thing that I changed is the computer! On the computer at my house it didn't work. At the office, it is working like a charm! How can it be possible? – Gerardo Siano Nov 20 '19 at 11:39

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.