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

12,494

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.

Share:
12,494
Patrick_Finucane
Author by

Patrick_Finucane

Never stop learning.

Updated on June 14, 2022

Comments

  • Patrick_Finucane
    Patrick_Finucane almost 2 years

    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.