3

Below is code that i am using send mail , and getting 'Invalid sender data'

<?php
    namespace Mycompany\Orderupdate\Helper;
    use Exception;
    use Psr\Log\LoggerInterface;
    use Magento\Framework\Mail\Template\TransportBuilder;
    use Magento\Framework\Translate\Inline\StateInterface;
    use Magento\Framework\App\Area;
    use Magento\Framework\App\Config\ScopeConfigInterface;
    use Magento\Store\Model\ScopeInterface;


    /**
     * Class Sendmail
     * @package Mycompany\Orderupdate\Model
     */
    class Sendmail
    {
        /**
         * Sender email
         */
        const SENDER_EMAIL='trans_email/ident_general/email';
        /**
         * Email Template
         */
        const EMAIL_TEMPLATE='orderupdate/email/email_template';
        /**
         * @var \Magento\Framework\Mail\Template\TransportBuilder
         */
        protected $transportBuilder;

        /**
         * @var \Magento\Framework\Translate\Inline\StateInterface
         */
        protected $inlineTranslation;

        /**
         * @var LoggerInterface
         */
        protected $logger;

        /**
         * @var \Magento\Store\Model\StoreManagerInterface
         */
        protected $_storeManager;
        protected $request;
        protected $scopeConfig;
        /**
         * ChangeRequestEmailSender constructor
         *
         * @param TransportBuilder $transportBuilder
         * @param StateInterface   $inlineTranslation
         * @param \Magento\Store\Model\StoreManagerInterface $storeManager
         */
        public function __construct(
            TransportBuilder $transportBuilder,
            StateInterface $inlineTranslation,
            LoggerInterface $logger,
            ScopeConfigInterface $scopeConfig,
            \Magento\Store\Model\StoreManagerInterface $storeManager
        )
        {
            $this->transportBuilder = $transportBuilder;
            $this->inlineTranslation = $inlineTranslation;
            $this->logger = $logger;
            $this->_storeManager = $storeManager;
            $this->scopeConfig = $scopeConfig;
        }

        public function sendEmailOnDelivery($order, $customerName, $customerEmail)
        {
            $this->inlineTranslation->suspend();
            $orderId=$order->getId();
            $adminAmail = $this->senderEmail();
            $emailtemplate = $this->emailTemplate();
            try {
                $templateVars = [
                    'name' => $customerName,
                    'orderId' => $orderId
                ];
                $transport = $this->transportBuilder
                    ->setTemplateIdentifier($emailtemplate)
                    ->setTemplateOptions
                    (
                        [
                            'area' => Area::AREA_ADMINHTML,
                            'store' => $this->_storeManager->getStore()->getId()
                        ]
                    )
                    ->setTemplateVars($templateVars)
                    ->setFrom($adminAmail)
                    ->addTo($customerEmail,$customerName)
                    ->getTransport();
                $transport->sendMessage();
                $this->inlineTranslation->resume();
            } catch (Exception $e) {
                print_r($e->getMessage());
                exit;
                $this->inlineTranslation->resume();
            }
        }

        public function senderEmail()
        {
            $storeScope = ScopeInterface::SCOPE_STORE;
            return $this->scopeConfig->getValue(
                self::SENDER_EMAIL,
                $storeScope
            );
        }
     public function emailTemplate()
        {
            $storeScope = ScopeInterface::SCOPE_STORE;
            return $this->scopeConfig->getValue(
                self::EMAIL_TEMPLATE,
                $storeScope
            );
        }

    }

Tried to debug where is exception from vendor\magento\module-email\Model\Template\SenderResolver.php

 public function resolve($sender, $scopeId = null)
    {
        $result = [];

        if (!is_array($sender)) {
            $result['name'] = $this->_scopeConfig->getValue(
                'trans_email/ident_' . $sender . '/name',
                \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
                $scopeId
            );
            $result['email'] = $this->_scopeConfig->getValue(
                'trans_email/ident_' . $sender . '/email',
                \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
                $scopeId
            );
        } else {
            $result = $sender;
        }

        if (!isset($result['name']) || !isset($result['email'])) {
            throw new \Magento\Framework\Exception\MailException(__('Invalid sender data'));
        }

        return $result;
    }

I am getting a blank array in $sender , but I'm passing value in $adminAmail.

2 Answers 2

10

Try with below code,

public function senderEmail($type = null, $storeId = null)
    {
        $sender ['email'] = $this->scopeConfig->getValue(
                                self::SENDER_EMAIL,
                                ScopeInterface::SCOPE_STORE,
                                $storeId
                            );
        $sender['name'] = 'admin';

        return $sender;
    }
6

The problem comes from your setFrom method.

I suggest you try to replace:

->setFrom($adminAmail)

With:

->setFrom([
    'email' => $this->scopeConfig->getValue(self::EMAIL_TEMPLATE, ScopeInterface::SCOPE_STORE),
    'name' => $this->scopeConfig->getValue('trans_email/ident_general/email', ScopeInterface::SCOPE_STORE)
]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.