Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift_TransportException·Connection could not be established with host smtp.sendgrid.net [Connection timed out #110]

I am using Laravel 4.2, which uses the swiftmailer lib for email. I am trying to use the Sendgrid SMTP service but I am getting an timeout error on my staging server.

Everything works fine in my local development.

mail.php

return array(
    'driver' => 'smtp',
    'host' => 'smtp.sendgrid.net',
    'port' => 587,
    'encryption' => 'tls',
    'username' => 'username'
    'password' => ...

I have switching to port 465 and 'ssl', works on the local but no luck on the server. OpenSSL is enabled on the server.

What is interesting is Mandrill smtp (this uses a different laravel driver) and the local server smtp work fine on the server.

The ports are open. I can telnet into sendgrid from the server. I also switched out the host name for the IPv4 just to checks because of all of the trouble people have with using smtp.gmail.com IPv4 vs IPv6. Didn't make a difference.

The specific error location is this:

    $this->_stream = @stream_socket_client($host.':'.$this->_params['port'], $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $streamContext);
    if (false === $this->_stream) {
        throw new Swift_TransportException(
            'Connection could not be established with host '.$this->_params['host'].
            ' ['.$errstr.' #'.$errno.']');

So my question is what should I check to figure out why swiftmailer and the stream_socket_client php function is working locally but not on the server. Locally I am on a mac, the server is running centOS.

like image 357
Patrick_Finucane Avatar asked Dec 16 '15 15:12

Patrick_Finucane


Video Answer


1 Answers

Looks like the application was being run by a user without access to port 587. So if you run into this problem make double sure you are testing the port with the user that is running your application.

like image 117
Patrick_Finucane Avatar answered Nov 03 '22 02:11

Patrick_Finucane