Magento – Magento 2: SMTP Email gives error “Could not open socket”

emailmagento2PHPsslzend-framework

Using Magento 2 Version 2.1.0 on WAMP Windows 10

As continue with this question Magento 2: SMTP Email gives error "Invalid sender data"

Now this is my code

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

Now it's giving below error

2 exception(s): Exception #0
(Magento\Framework\Exception\MailException): Could not open socket
Exception #1 (Zend_Mail_Protocol_Exception): Could not open socket

I have already followed https://stackoverflow.com/questions/28433816/how-to-fix-could-not-open-socket-in-zend-mail-zend-framework-2

But still same issue. OpenSSL is enabled on WAMP. When i'm trying to uncomment

#LoadModule ssl_module modules/mod_ssl.so

in httpd.conf & trying to restart WAMP Server. It pauses on Yellow not become Green.

Best Answer

SELINUX BLOCKING If you are getting error message like SMTP -> ERROR: Failed to connect to server: Permission denied (13), Permission denied, and your are hosting on RedHat / Fedora / Centos this is more than likely cause by SELinux preventing PHP or the web server from sending email. . Using the getsebool command we can check if the httpd daemon is allowed to make a connection over the network and send an email:

getsebool httpd_can_sendmail
getsebool httpd_can_network_connect

This command will return a boolean on or off. If it’s off, we can turn it on:

sudo setsebool -P httpd_can_sendmail 1
sudo setsebool -P httpd_can_network_connect 1

Thanks to MagePal here is their full documentation