Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift_TransportException Connection could not be established with host smtp.gmail.com

I can't figure for the life of me why exactly is it failing.. this is the exact error I am getting:

Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection  
could not be established with host smtp.gmail.com [Connection refused #111]' in 
/home/content/38/6896038/html/test/lib/classes/Swift/Transport/StreamBuffer.php:259 
Stack trace: #0 
/home/content/38/6896038/html/test/lib/classes/Swift/Transport/StreamBuffer.php(64): 
Swift_Transport_StreamBuffer->_establishSocketConnection() #1 /home/content/38/6896038/html/test/lib/classes/Swift/Transport/AbstractSmtpTransport.php(115): Swift_Transport_StreamBuffer->initialize(Array) #2 /home/content/38/6896038/html/test/lib/classes/Swift/Mailer.php(80): Swift_Transport_AbstractSmtpTransport->start() #3 /home/content/38/6896038/html/test/contact.php(55): Swift_Mailer->send(Object(Swift_Message)) #4 {main} thrown in /home/content/38/6896038/html/test/lib/classes/Swift/Transport/StreamBuffer.php on line 259

And the code Im using is what I've gotten from the tutorial and reading on here examples, here it is:

require_once 'lib/swift_required.php';

$transport = Swift_MailTransport::newInstance();

// Create the Transport the call setUsername() and setPassword()
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
  ->setUsername('[email protected]')
  ->setPassword('xxx')
  ;

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

$nombre = $_POST['name'];
$apellido = $_POST['apellido'];
$email = $_POST['email'];
$telefono = $_POST['telefono'];
$title = $_POST['jobtitle'];

// Create the message
$message = Swift_Message::newInstance()

  // Give the message a subject
  ->setSubject('Nuevo applicante para: ' . $title)

  // Set the From address with an associative array
  ->setFrom(array('[email protected]' => 'no-reply'))

  // Set the To addresses with an associative array
  ->setTo('[email protected]')

  // Give it a body
  ->setBody('<html>
                <head></head>
                <body>
                    <table>
                        <tr>
                            <td>Nombre:</td><td>' . $nombre . ' ' . $apellido .'</td>
                        </tr>
                        <tr>
                            <td>Email:</td><td>' . $email . '</td>
                        </tr>
                        <tr>
                            <td>Telefono:</td><td>'. $telefono .'</td>
                        </tr>
                    </table>
                </body>
            </html>', 'text/html')

  // Optionally add any attachments
  ->attach(Swift_Attachment::fromPath($_FILES["file"]["tmp_name"])->setFilename($_FILES['file']['name']));

// Send the message
$result = $mailer->send($message);

any help is greatly appreciated, I've done my reading and searching and cannot find a solution to this :(

EDIT: Apparently it's an issue with gmail, I changed to a different smtp and it worked.. :S

like image 321
Andres Avatar asked Jan 05 '13 22:01

Andres


People also ask

How do I fix Gmail SMTP not working?

Solution. Check SMTP settings, enable less secure apps, and unlock Captcha: Confirm the form's SMTP settings are correct. Enable access to Less secure apps.


6 Answers

Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp.gmail.com [Connection refused #111]

Connection refused is a very explicit and clear error message. It means that the socket connection could not be established because the remote end actively refused to connect.

It's very unlikely that Google is blocking the connection.

It's very likely that your web hosting provider has firewall settings that block outgoing connections on port 465, or that they are blocking SMTP to Gmail. 465 is the "wrong" port for secure SMTP, though it is often used, and Gmail does listen there. Try port 587 instead. If the connection is still refused, call your host and ask them what's up.

like image 135
Charles Avatar answered Oct 05 '22 14:10

Charles


remove from your config.yml spool: { type: memory }

Then you will be to add try catch like this

    try{
        $mailer = $this->getMailer();
        $response = $this->getMailer()->send($message);
    }catch(\Swift_TransportException $e){
        $response = $e->getMessage() ;
    }
like image 41
Ivan Proskuryakov Avatar answered Oct 05 '22 12:10

Ivan Proskuryakov


I had the same issue when I was using Godaddy web hosting and solved this by editing my .env file as,

MAIL_DRIVER=smtp
MAIL_HOST=XXXX.XXXX.in
MAIL_PORT=587
MAIL_USERNAME=dexxxxx
MAIL_PASSWORD=XXXXXXXX
MAIL_ENCRYPTION=tls

Where MAIL_HOST is your domain name from Godaddy, MAIL_USERNAME is your user name from Godaddy and MAIL_PASSWORD is your password from Godaddy.

I hope this may help you.

like image 21
AmarjaPatil4 Avatar answered Oct 05 '22 14:10

AmarjaPatil4


tcp:465 was blocked. Try to add a new firewall rules and add a rule port 465. or check 587 and change the encryption to tls.

like image 36
Kuya A Avatar answered Oct 05 '22 13:10

Kuya A


My solution was to change:

  'driver' => 'smtp',

to

 'driver' => 'mail',

In app/config/mail

Hope that helps

like image 24
Samir Daraf Avatar answered Oct 05 '22 13:10

Samir Daraf


In v 5.8.38, I set the env file as the following:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=difficultCombination
MAIL_ENCRYPTION=ssl
MAIL_FROM_NAME=myWebappName

After doing php artisan config:clear, it worked well on a shard server.

like image 39
grape100 Avatar answered Oct 05 '22 12:10

grape100