Swift_TransportException Connection could not be e

2019-01-11 10:17发布

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

11条回答
不美不萌又怎样
2楼-- · 2019-01-11 10:45

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!

查看更多
走好不送
3楼-- · 2019-01-11 10:46

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.

查看更多
别忘想泡老子
4楼-- · 2019-01-11 10:47

My solution was to change:

  'driver' => 'smtp',

to

 'driver' => 'mail',

In app/config/mail

Hope that helps

查看更多
Luminary・发光体
5楼-- · 2019-01-11 10:51

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.

查看更多
该账号已被封号
6楼-- · 2019-01-11 10:55

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.

查看更多
登录 后发表回答