0

i'm using Kiwicommerce enhanced SMTP extension. when order is placed it'll automaticalaly get redirected to cart page.

Fatal error: Uncaught Error: Call to undefined method Magento\Framework\Mail\Message\Interceptor::getRecipients() in /home/ksantosh/public_html/app/code/KiwiCommerce/EnhancedSMTP/Email/Log/SaveEmailLog.php:347 Stack trace: #0 /home/ksantosh/public_html/generated/code/KiwiCommerce/EnhancedSMTP/Email/Log/SaveEmailLog/Interceptor.php(128): KiwiCommerce\EnhancedSMTP\Email\Log\SaveEmailLog->prepareMessage() #1 /home/ksantosh/public_html/vendor/magento/framework/Mail/Template/TransportBuilder.php(247): KiwiCommerce\EnhancedSMTP\Email\Log\SaveEmailLog\Interceptor->prepareMessage() #2 /home/ksantosh/public_html/generated/code/KiwiCommerce/EnhancedSMTP/Email/Log/SaveEmailLog/Interceptor.php(258): Magento\Framework\Mail\Template\TransportBuilder->getTransport() #3 /home/ksantosh/public_html/vendor/magento/module-sales/Model/Order/Email/SenderBuilder.php(78): KiwiCommerce\EnhancedSMTP\Email\Log\SaveEmailLog\Interceptor->getTransport() #4 /home/ksantosh/public_html/vendor/magento/module-sales/Model/Order/Email/Sender.php(85): Magento in /home/ksantosh/public_html/app/code/KiwiCommerce/EnhancedSMTP/Email/Log/SaveEmailLog.php on line 347

Here is the emailLog.php

<?php
/**
 * KiwiCommerce
 *
 * Do not edit or add to this file if you wish to upgrade to newer versions in the future.
 * If you wish to customise this module for your needs.
 * Please contact us https://kiwicommerce.co.uk/contacts.
 *
 * @category   KiwiCommerce
 * @package    KiwiCommerce_EnhancedSMTP
 * @copyright  Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
 * @license    https://kiwicommerce.co.uk/magento2-extension-license/
 */
namespace KiwiCommerce\EnhancedSMTP\Email\Log;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\App\TemplateTypesInterface;
use Magento\Framework\Mail\MessageInterface;
use KiwiCommerce\EnhancedSMTP\Helper\Config;
use KiwiCommerce\EnhancedSMTP\Model\SendEmail;
/**
 * Class SaveEmailLog
 * @package KiwiCommerce\EnhancedSMTP\Email\Log
 */
class SaveEmailLog extends TransportBuilder
{
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    public $storeManager;
    /**
     * @var \KiwiCommerce\EnhancedSMTP\Model\LogsFactory
     */
    public $logsFactory;

    /**
     * @var \KiwiCommerce\EnhancedSMTP\Helper\Config
     */
    public $config;

    /**
     * @var \KiwiCommerce\EnhancedSMTP\Logger\Logger
     */
    public $logger;

    /**
     * To get request parameter
     * @var \Magento\Framework\App\Request\Http
     */
    public $requests;

    /**
     * @var \KiwiCommerce\EnhancedSMTP\Model\Logs\Status
     */
    public $status;

    /**
     * @var \KiwiCommerce\EnhancedSMTP\Helper\Benchmark
     */
    public $benchmark;

    /**
     * @var \Magento\Framework\Mail\MessageInterfaceFactory
     */
    protected $messageFactory;

    /**
     * @var $mailMessage
     */
    protected $mailMessage;

    /**
     * SaveEmailLog constructor.
     * @param \Magento\Framework\Mail\Template\FactoryInterface $templateFactory
     * @param MessageInterface $message
     * @param \Magento\Framework\Mail\Template\SenderResolverInterface $senderResolver
     * @param \Magento\Framework\ObjectManagerInterface $objectManager
     * @param \Magento\Framework\Mail\TransportInterfaceFactory $mailTransportFactory
     * @param \Magento\Framework\Mail\MessageInterfaceFactory|null $messageFactory
     * @param \KiwiCommerce\EnhancedSMTP\Model\LogsFactory $logsFactory
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Framework\App\Request\Http $request
     * @param Config $config
     * @param \KiwiCommerce\EnhancedSMTP\Model\Logs\Status $status
     * @param \KiwiCommerce\EnhancedSMTP\Logger\Logger $logger
     * @param \KiwiCommerce\EnhancedSMTP\Helper\Benchmark $benchmark
     */
    public function __construct(
        \Magento\Framework\Mail\Template\FactoryInterface $templateFactory,
        \Magento\Framework\Mail\MessageInterface $message,
        \Magento\Framework\Mail\Template\SenderResolverInterface $senderResolver,
        \Magento\Framework\ObjectManagerInterface $objectManager,
        \Magento\Framework\Mail\TransportInterfaceFactory $mailTransportFactory,
        \Magento\Framework\Mail\MessageInterfaceFactory $messageFactory = null,
        \KiwiCommerce\EnhancedSMTP\Model\LogsFactory $logsFactory,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\App\Request\Http $request,
        \KiwiCommerce\EnhancedSMTP\Helper\Config $config,
        \KiwiCommerce\EnhancedSMTP\Model\Logs\Status $status,
        \KiwiCommerce\EnhancedSMTP\Logger\Logger $logger,
        \KiwiCommerce\EnhancedSMTP\Helper\Benchmark $benchmark
    ) {

        $this->logsFactory = $logsFactory;
        $this->storeManager = $storeManager;
        $this->config = $config;
        $this->requests = $request;
        $this->status = $status;
        $this->logger = $logger;
        $this->benchmark = $benchmark;

        if (!$config->versionCompare('2.3.0')) {
            parent::__construct($templateFactory, $message, $senderResolver, $objectManager, $mailTransportFactory);
        } else {
            parent::__construct($templateFactory, $message, $senderResolver, $objectManager, $mailTransportFactory, $messageFactory);
        }
    }

    /**
     * Trim name
     *
     * @param $name
     * @return string
     */
    public function filterName($name)
    {
        return trim(strstr(
            $name,
            "<",
            true
        ));
    }

    /**
     * Get store identifier
     *
     * @return  int
     */
    public function getStoreId()
    {
        return $this->storeManager->getStore()->getId();
    }

    /**
     * Create Email Log object and set basic values.
     *
     * @return mixed
     */
    public function initEmailLog()
    {
        $module = $this->requests->getModuleName();
        $emailLog = $this->logsFactory->create();

        if ($this->config->versionCompare('2.3.0')) {

            $from = $this->mailMessage->getFrom();

            $senderEmail = '';
            $senderName = '';
            if (count($from)) {
                $from->rewind();
                $senderEmail = $from->current()->getEmail();
                $senderName = $from->current()->getName();
            }

            //Set sender details
            $emailLog->setSenderEmail($senderEmail);
            $emailLog->setSenderName($senderName);
            $emailLog->setEmailTemplate($this->mailMessage->getBodyText());

        } else {
            $header = $this->message->getHeaders();
            //Set sender details
            $emailLog->setSenderEmail($this->message->getFrom());

            $emailLog->setSenderName(
                $this->filterName(
                    isset($header['From'])?current($header['From']):''
                )
            );
            $emailLog->setEmailTemplate($this->message->getBody()->getRawContent());
        }

        //Set template detail
        $emailLog->setTemplateId($this->templateIdentifier);
        $emailLog->setModuleName($module);
        $emailLog->setEmailSubject($this->message->getSubject());
        $emailLog->setStoreId($this->getStoreId());
        $emailLog->setStatus(\KiwiCommerce\EnhancedSMTP\Model\Logs\Status::STATUS_PENDING);

        return $emailLog;
    }

    /**
     * Set recipient details
     *
     * @param $key
     * @return string
     */
    public function getRecipientName($key)
    {
        $header = $this->message->getHeaders();
        return $this->filterName(
            isset($header['To'][$key])?$header['To'][$key]:''
        );
    }

    /**
     * Save email log data
     *
     * @param $emailLog
     * @param $recipient
     * @param $name
     * @return int
     */
    public function sendLog($emailLog, $recipient, $name)
    {
        $emailLog->setRecipientName($name);
        $emailLog->setRecipientEmail($recipient);
        $emailLog->save();

        //Get Log Id After Email Log Save
        $logId = $emailLog->getId();

        //Unset Email Log Data
        $emailLog->unsetData();

        return $logId;
    }

    /**
     * Check if module is allowed for email log
     * @return bool
     */
    public function isAllowed()
    {
        $allow = true;
        $module = $this->requests->getModuleName();
        switch ($module) {
            case 'customer':
                $customer = $this->config->getConfig(Config::ALLOWED_CUSTOMER_MODULE);
                $allow = ($customer == 0)?false:$allow;
                break;
            case 'newsletter':
                $newsletter = $this->config->getConfig(Config::ALLOWED_NEWSLETTER_MODULE);
                $allow = ($newsletter == 0)?false:$allow;
                break;
            case 'contact':
                $contact = $this->config->getConfig(Config::ALLOWED_CONTACTUS_MODULE);
                $allow = ($contact == 0)?false:$allow;
                break;
            case 'sales':
                $order = $this->config->getConfig(Config::ALLOWED_ORDER_MODULE);
                $allow = ($order == 0)?false:$allow;
                break;
        }

        return $allow;
    }

    /**
     *  Get recipients
     *
     * @return array
     */
    public function getRecipients()
    {
        $recipients = [];

        foreach ($this->mailMessage->getTo() as $address) {
            $recipients[] = $address->getEmail();
        }

        foreach ($this->mailMessage->getCc() as $address) {
            $recipients[] = $address->getEmail();
        }
        foreach ($this->mailMessage->getBcc() as $address) {
            $recipients[] = $address->getEmail();
        }

        $recipients = array_unique($recipients);
        return $recipients;
    }

    /**
     *  Get recipients name by email
     *
     * @param $email
     * @return string
     */
    public function getRecipientsNameByEmail($email)
    {
        $recipients = [];

        foreach ($this->mailMessage->getTo() as $address) {
            $recipients[$address->getEmail()] = $address->getName();
        }

        foreach ($this->mailMessage->getCc() as $address) {
            $recipients[$address->getEmail()] = $address->getName();
        }
        foreach ($this->mailMessage->getBcc() as $address) {
            $recipients[$address->getEmail()] = $address->getName();
        }

        $recipientName = '';

        if (!empty($recipients[$email])) {
            $recipientName = $recipients[$email];
        }

        return $recipientName;
    }

    /**
     * Log emails
     *
     * @return $this|bool
     */
    public function prepareMessage()
    {
        $this->benchmark->start(__METHOD__);

        if (!$this->config->isEnable()
            || $this->templateIdentifier == SendEmail::TEST_EMAIL_TEMPLATE
            || !$this->config->getConfig(Config::ENHANCED_SMTP_LOG_EMAIL)
        ) {
            return parent::prepareMessage();
        }

        try {

            if (!$this->isAllowed()) {
                return parent::prepareMessage();
            }

            parent::prepareMessage();

            if ($this->config->versionCompare('2.3.0')) {
                $this->mailMessage = \Zend\Mail\Message::fromString($this->message->getRawMessage());

                $recipients = $this->getRecipients();

            } else {
                $recipients = $this->message->getRecipients();
            }

            $logIdArray = [];

            foreach ($recipients as $key => $recipient) {
                $emailLog = $this->initEmailLog();

                if ($this->config->versionCompare('2.3.0')) {
                    $name = $this->getRecipientsNameByEmail($recipient);
                } else {
                    $name = $this->getRecipientName($key);
                }

                $logId = $this->sendLog($emailLog, $recipient, $name);
                $logIdArray[] = $logId;
            }

            //Set array of log id in registry
            $this->status->setLogId($logIdArray);
        } catch (\Exception $e) {
            $this->logger->critical($e->getMessage());
        }

        $this->benchmark->end(__METHOD__);

        return $this;
    }
}
  • here is the line in 347 : $recipients = $this->message->getRecipients(); – Hemant May 20 at 9:43
  • Can you update your question with the code of SaveEmailLog.php – Prachi Saxena May 20 at 9:50
  • i've updated,, plz check – Hemant May 20 at 9:53
  • Can you try replacing $this->message->getRecipients() to $this->getRecipients() on line 347 and check if you still getting the error. There does not seems to be any method like getRecipients in message.php The if statement does not seems to do something valid – Prachi Saxena May 20 at 10:30
  • now i'm getting error in line 272 : /home/user/public_html/app/code/KiwiCommerce/EnhancedSMTP/Email/Log/SaveEmailLog.php on line 272 – Hemant May 20 at 10:48

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.