Question

Using Magento 2 Version 2.1.0 on WAMP Windows 10

I'm using https://github.com/magepal/magento2-gmailsmtpapp

In magento Admin, then goto Stores -> Configuration -> Advanced -> System -> Gmail/Google Apps SMTP Pro. I have configured things

  • Authentication method: LOGIN
  • SSL type: SSL (Gmail / Google Apps)
  • SMTP Host: smtp.gmail.com
  • Username: Gmail Account Username
  • Password: GmailAccount Password
  • Set Reply-to: Yes
  • Set Return-Path: Yes

I have below code in my Controller

 $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
        $transport = $this->_transportBuilder->setTemplateIdentifier(1)
                ->setTemplateOptions(['area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE, 'store' => 1])
                ->setTemplateVars(['data' => $postObject])
                ->setFrom('XXXX@gmail.com', $storeScope)
                ->addTo('XXXX@gmail.com', $storeScope)
                ->setReplyTo('XXXX@gmail.com')
                ->getTransport();
        echo $transport->sendMessage();

When i run this code, it gives below error

1 exception(s): Exception #0 (Magento\Framework\Exception\MailException): Invalid sender data

Now it's causing

Could Not Open Socket

Was it helpful?

Solution

You have to replace ->setFrom('XXXX@gmail.com', $storeScope) to ->setFrom('XXXX@gmail.com') in above code, Becasue setFrom method is not supported two arguments.

Check core function for this, inside file, magento2/vendor/magento/framework/Mail/Template/TransportBuilder.php

public function setFrom($from)
    {
        $result = $this->_senderResolver->resolve($from);
        $this->message->setFrom($result['email'], $result['name']);
        return $this;
    }

if you want to pass storescope in your method, please refer core contact module mail functionality from here, magento2/vendor/magento/module-contact/Controller/Index/Post.php

->setFrom($this->scopeConfig->getValue(self::XML_PATH_EMAIL_SENDER, $storeScope))
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top