0

I was sending custom emails in magento version 2.3.2 successfully with below code:

$storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
        $stroreid = $this->_storeManager->getStore()->getId();
            $this->inlineTranslation->suspend();
            $error = false;
        $to = array($email);
            $transport = $this->_transportBuilder
                ->setTemplateIdentifier('template_name_here')
                ->setTemplateOptions(
                    [
                        'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                        'store' => $stroreid,
                    ]
                )
                ->setTemplateVars(['data' => $variable])
                ->setFrom($this->scopeConfig->getValue(self::XML_PATH_EMAIL_SENDER, $storeScope))
                ->addTo($to)
                ->getTransport();
            $transport->sendMessage();
            $this->inlineTranslation->resume();

But after upgrade to version 2.3.4, when this sendEmail() function is executed I am getting error:

"message": "Missing required argument $to of Magento\Framework\Mail\EmailMessage.",

By comparing vendor folders of 2.3.2 and 2.3.4 I noticed

 Magento\Framework\Mail\EmailMessage

above class is new in 2.3.4 and has the following checks:

$mimeMessage = new ZendMimeMessage();
        $mimeMessage->setParts($body->getParts());
        $this->zendMessage->setBody($mimeMessage);
        if ($subject) {
            $this->zendMessage->setSubject($subject);
        }
        if ($sender) {
            $this->zendMessage->setSender($sender->getEmail(), $sender->getName());
        }
        if (count($to) < 1) {
            throw new InvalidArgumentException('Email message must have at list one addressee');
        }
        if ($to) {
            $this->zendMessage->setTo($this->convertAddressArrayToAddressList($to));
        }
        if ($replyTo) {
            $this->zendMessage->setReplyTo($this->convertAddressArrayToAddressList($replyTo));
        }
        if ($from) {
            $this->zendMessage->setFrom($this->convertAddressArrayToAddressList($from));
        }
        if ($cc) {
            $this->zendMessage->setCc($this->convertAddressArrayToAddressList($cc));
        }
        if ($bcc) {
            $this->zendMessage->setBcc($this->convertAddressArrayToAddressList($bcc));
        }
        $this->mimeMessageFactory = $mimeMessageFactory;
        $this->addressFactory = $addressFactory;

I am trying to solve the issue by successfully sending email in 2.3.4. Can somebody check it also!

1

Try this code it's working for me in Magento 2.3.4

$email = $this->scopeConfig->getValue(self::XML_PATH_EMAIL_SENDER, $storeScope);
$subject = 'ABC';
$from = array('email' => $email, 'name' => $subject);
$emailTemplateVariables = array();
$emailTemplateVariables['data'] = $variable;
$storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
$templateOptions = array('area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $this->_storeManager->getStore()->getId());
$transport = $this->_transportBuilder->setTemplateIdentifier('template_name_here')
    ->setTemplateOptions($templateOptions)
    ->setTemplateVars($emailTemplateVariables)
    ->setFrom($from)
    ->addTo('aaa@bbb.com')
    ->getTransport();

try {
    $transport->sendMessage();
} catch (\Exception $e) {
    echo $e;
}
| improve this answer | |
0

Error was, I was passing $to value in array as:

$to = array($email);

Which was working in version 2.3.2. Now I changed it to:

$to = $email;

It solved the error in 2.3.4. I hope it helps others.

| improve this answer | |

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.