27

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@googleappsdomain.com')
  ->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('no-reply@topytop.com.pe' => 'no-reply'))

  // Set the To addresses with an associative array
  ->setTo('email@thisemail.com')

  // 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

Andres
  • 2,013
  • 6
  • 42
  • 67
  • It looks like everything (code-wise) is set up correctly, have you tried logging into Google Apps via the login info you're using here? If you just created the account, gmail requires you to agree to some terms before the account can be use. Until you hit "agree" on that page, access to SMTP will not be available. – SamT Jan 05 '13 at 22:46
  • yea I have that email currently working. – Andres Jan 05 '13 at 22:47
  • Does your server allows SSL connection through the 465 port ? I know some shared hosting that disable this. If that's the case, usually you just have to contact the support, and they will open the port for you. – Julien Jan 05 '13 at 23:52
  • The code does not have any errors, the more possible is which your hosting service provider to deactivated all the capabilities of SMTP in your account, contact your hosting provider to find the solution. – Steven Cruz Mar 10 '17 at 20:54

14 Answers14

20

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.

Charles
  • 50,943
  • 13
  • 104
  • 142
  • @Charles, Seems you can help me. Look at this : https://stackoverflow.com/questions/48654849/how-can-i-solve-connection-could-not-be-established-with-host-smtp-gmail-com – moses toh Feb 13 '18 at 03:48
7

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() ;
    }
Ivan Proskuryakov
  • 1,625
  • 2
  • 23
  • 32
  • Seems you can help me. Look at this : https://stackoverflow.com/questions/48654849/how-can-i-solve-connection-could-not-be-established-with-host-smtp-gmail-com – moses toh Feb 13 '18 at 03:48
6

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.

AmarjaPatil4
  • 1,640
  • 3
  • 28
  • 41
  • Where can I find the .env file? I am also using godaddy! Thanks! – Gacci Aug 02 '16 at 17:02
  • It wont be visible on cpanel by default. you can use ftp connection to see this file. it will be on the root of your project – Maha Dev Mar 15 '17 at 05:29
4

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.

Kuya A
  • 351
  • 2
  • 18
4

My solution was to change:

  'driver' => 'smtp',

to

 'driver' => 'mail',

In app/config/mail

Hope that helps

Samir Daraf
  • 139
  • 9
  • 2
    by using the mail driver, mails will be sent using the smtp settings as set in php.ini. This is not a solution for above problem – Arnout May 10 '18 at 17:05
1

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

MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
MAIL_USERNAME=mygmail@gmail.com
MAIL_PASSWORD=difficultCombination
MAIL_ENCRYPTION=ssl
MAIL_FROM_NAME=myWebappName

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

Tariqul Islam
  • 347
  • 4
  • 18
grape100
  • 341
  • 4
  • 11
0

I had the same problem for a while, replacing: smtp.gmail.com with 173.194.65.108 actually worked for me!

Mohammad Anini
  • 5,073
  • 4
  • 35
  • 46
0

In my case, I was using Laravel 5 and I had forgotten to change the mail globals in the .env file that is located in your directory root folder (these variables override your mail configuration)

   MAIL_DRIVER=smtp
   MAIL_HOST=smtp.gmail.com
   MAIL_PORT=465
   MAIL_USERNAME=yourmail@gmail.com
   MAIL_PASSWORD=yourpassword

by default, the mailhost is:

   MAIL_HOST=mailtraper.io

I was getting the same error but that worked for me.

Jesus Rodriguez
  • 2,571
  • 2
  • 22
  • 38
0

I had the same issue and i manage to fix it by disabling my AVAST Anti Virus. As well as my AVAST extension in the browser. Antivirus sometimes blocks your application. :) I hope it helps to anyone.

Dominic
  • 29
  • 2
0

firstly,check for gmail SMTP server . you should have to allow access for less secured apps without allowing swift mailer is not possible.for this login in your email account first then go into privacy settings and then click on sign and security then click on apps without access and then make on to less secure apps option then try mailer again, it will work then.

0

In my case, I had trouble with GoDaddy and SSL encryption.

Setting the encryption to Null and Port to 80 (Or any supportive port) did the job.

0

If you have hosted your website already it advisable to use the email or create an offical mail to send the email Let say your url is www.example.com. Then go to the cpanel create an email which will be send@example.com. Then request for the setting of the email. Change the email address to send@example.com. Then change your smtp which will be mail.example.com. The ssl will remain 465

0

Disable "SMTP Restrictions" from WHM. it can help you

Sajibe Kanti
  • 181
  • 1
  • 9
-2

I just had this issue and solved it by editing mail.php under config folder. Usually when using shared hosting, they hosting company allows you to create an email such as

XXXX@yourdomainname.com

. So go to email settings under you shared hosting cpanel and add new email. Take that email and password and set it in the

mail.php.

It should work!

spacemonkey
  • 2,530
  • 6
  • 23
  • 27
  • 1
    If the OP is using gmail's SMTP servers, how in the world is a shared hosting company's configuration relevant? Not to mention that an email that does not exist is unlikely to produce a "connection refused" error. – kstev Apr 06 '14 at 16:33