ZF3 - Zend Mail Exception - Could not open socket

1.2k views Asked by At

I have the class below. It works well stand-alone (without zend skeleton) but when I try to put it in a Controller of the zend skeleton I get an error. In same server it works well without zend MVC, whence do not seem ssl or tsl configuration. Seems some in the Zend skeleton environment.

Sorry for bad English.

Thanks!

use Zend\Mail\Message;
use Zend\Mail\Transport\Smtp;
use Zend\Mail\Transport\SmtpOptions;

class SendMail
{
    public function send()
    {
        $message = new Message();
        $message->addTo('[email protected]');
        $message->addFrom('[email protected]');
        $message->setSubject('Greetings and Salutations!');
        $message->setBody("Sorry, I'm going to be late today!");

        $transport = new Smtp();
        $options = new SmtpOptions([
            'name' => 'smtp.gmail.com',
            'host' => 'smtp.gmail.com',
            'connection_class' => 'login',
            'port' => 587,
            'connection_config' => [
                'username' => '[email protected]',
                'password' => 'mypass',
                'ssl' => 'tls'
            ],
        ]);
        $transport->setOptions($options);

        var_dump($transport);
        $transport->send($message);
    }
}

The controller:

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class ContactFormController extends AbstractActionController
{
    protected $sendMail;
    public function __construct($sendMail)
    {
        $this->sendMail = $sendMail;
    }

    public function indexAction()
    {
        $this->sendMail->send();

        return new ViewModel();
    }
}

I get the error:

An error occurred
An error occurred during execution; please try again later.
Additional information:

Zend\Mail\Protocol\Exception\RuntimeException

File:

/var/www/ContactForm/vendor/zendframework/zend-mail/src/Protocol/AbstractProtocol.php:215

Message:

Could not open socket

Stack trace:

#0 /var/www/ContactForm/vendor/zendframework/zend-mail/src/Protocol/Smtp.php(149): Zend\Mail\Protocol\AbstractProtocol->_connect('tcp://smtp.gmai...')
#1 /var/www/ContactForm/vendor/zendframework/zend-mail/src/Transport/Smtp.php(376): Zend\Mail\Protocol\Smtp->connect()
#2 /var/www/ContactForm/vendor/zendframework/zend-mail/src/Transport/Smtp.php(362): Zend\Mail\Transport\Smtp->connect()
#3 /var/www/ContactForm/vendor/zendframework/zend-mail/src/Transport/Smtp.php(373): Zend\Mail\Transport\Smtp->lazyLoadConnection()
#4 /var/www/ContactForm/vendor/zendframework/zend-mail/src/Transport/Smtp.php(230): Zend\Mail\Transport\Smtp->connect()
#5 /var/www/ContactForm/module/ContactForm/src/Mail/SendMail.php(34): Zend\Mail\Transport\Smtp->send(Object(Zend\Mail\Message))
#6 /var/www/ContactForm/module/ContactForm/src/Controller/ContactFormController.php(32): ContactForm\Mail\SendMail->send()
#7 /var/www/ContactForm/vendor/zendframework/zend-mvc/src/Controller/AbstractActionController.php(78): ContactForm\Controller\ContactFormController->indexAction()
#8 /var/www/ContactForm/vendor/zendframework/zend-eventmanager/src/EventManager.php(322): Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#9 /var/www/ContactForm/vendor/zendframework/zend-eventmanager/src/EventManager.php(179): Zend\EventManager\EventManager->triggerListeners(Object(Zend\Mvc\MvcEvent), Object(Closure))
#10 /var/www/ContactForm/vendor/zendframework/zend-mvc/src/Controller/AbstractController.php(105): Zend\EventManager\EventManager->triggerEventUntil(Object(Closure), Object(Zend\Mvc\MvcEvent))
#11 /var/www/ContactForm/vendor/zendframework/zend-mvc/src/DispatchListener.php(119): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#12 /var/www/ContactForm/vendor/zendframework/zend-eventmanager/src/EventManager.php(322): Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#13 /var/www/ContactForm/vendor/zendframework/zend-eventmanager/src/EventManager.php(179): Zend\EventManager\EventManager->triggerListeners(Object(Zend\Mvc\MvcEvent), Object(Closure))
#14 /var/www/ContactForm/vendor/zendframework/zend-mvc/src/Application.php(332): Zend\EventManager\EventManager->triggerEventUntil(Object(Closure), Object(Zend\Mvc\MvcEvent))
#15 /var/www/ContactForm/public/index.php(48): Zend\Mvc\Application->run()
#16 {main}
0

There are 0 answers