0

I have create a custom module which send mail. The mail functionality works perfectly but success message not show up.

mail code is :

<?php
namespace Codextblog\Customemail\Controller\Index;

use Zend\Log\Filter\Timestamp;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;

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;
protected $request;
protected $scopeConfig;
protected $messageManager;
protected $_customerSession;
private $logger;
 
public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\App\Request\Http $request,
    \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
    \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    \Magento\Customer\Model\SessionFactory $customerSession,
    \Psr\Log\LoggerInterface $loggerInterface,
    \Psr\Log\LoggerInterface $logger,
    \Magento\Framework\Message\ManagerInterface $messageManager,
    array $data = []
     
    )
{
    $this->_inlineTranslation = $inlineTranslation;
    $this->logger = $logger;
    $this->_transportBuilder = $transportBuilder;
    $this->_customerSession = $customerSession->create();
    $this->scopeConfig = $scopeConfig;
    $this->_logLoggerInterface = $loggerInterface;
    $this->messageManager = $messageManager;  
    parent::__construct($context);
     
     
}

public function getCustomerData() {
    if ($this->_customerSession->isLoggedIn()) {
        return $this->_customerSession->getCustomerData()->getEmail();
    }
    return false;
}
 
public function execute()
{
    $useremail = $this->getCustomerData();
    $toemail = $this->scopeConfig->getValue('trans_email/ident_support/email',ScopeInterface::SCOPE_STORE);
    $toname  = $this->scopeConfig->getValue('trans_email/ident_support/name',ScopeInterface::SCOPE_STORE);
    $post = $this->getRequest()->getPost();
    $template = 'customemail_email_template';
    $recipient_address = $toemail;
    $recipient_name = $toname;
    $from_address = [
        'name' => $useremail,
        'email' => $useremail
    ];
    //an array with variables, format is key = variable name, value = variable value
    $vars = [
        'name' => $post['name'],
        'email' => $post['email'],
        'ownnumber' => $post['ownnumber'],
        'heldcertificate' => $post['heldcertificate'],
        'subjectedtovat' => $post['subjectedtovat']
    ];
    //several variables in email template, i.e. storeName are generated based on store Id
    $storeId = \Magento\Store\Model\Store::DEFAULT_STORE_ID;

    $this->_inlineTranslation->suspend();

        $this->_transportBuilder->setTemplateIdentifier(
            $template
        )->setTemplateOptions(
            [
                'area'  => \Magento\Framework\App\Area::AREA_FRONTEND,
                'store' => $storeId,
            ]
        )->setTemplateVars(
            $vars
        )->setFrom(
            $from_address
        )->addTo(
            $recipient_address,
            $recipient_name
        );


    //create the transport
    if (!isset($transport)) {
        $transport = $this->_transportBuilder->getTransport();
    }

    //send the email
    try {
        $transport->sendMessage();
        $this->_inlineTranslation->resume();
        $this->messageManager->addSuccess('Email sent successfully');
        $this->_redirect('platevaluations/index/index');
    } catch (\Exception $exception) {
    //log failed email send operation
        $this->logger->critical($exception->getMessage());
    }        
}
}
  • Any error into email send? put entire code into the try.....catch block and then check. – Dhiren Vasoya Sep 18 at 14:17
  • you have checked logs during received email – Rakesh Donga Sep 19 at 7:55
  • dont'n know why but now my mail is also not sending gave an error on system.log [2020-09-19 08:09:12] main.CRITICAL: Unable to send mail: Unknown error [] [] @RakeshDonga Have you idea about issue ? – Prits Sep 19 at 8:10

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.