27

我一生都无法弄清楚为什么它会失败..这是我得到的确切错误:

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

我使用的代码是我从教程中得到的,并阅读了这里的例子,这里是:

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);

非常感谢任何帮助,我已经完成了阅读和搜索,但找不到解决方案:(

编辑:显然这是 gmail 的问题,我更改为不同的 smtp 并且它有效.. :S

4

13 回答 13

20

致命错误:未捕获的异常“Swift_TransportException”与消息“无法与主机 smtp.gmail.com 建立连接 [连接被拒绝 #111]

拒绝连接是一个非常明确和明确的错误消息。表示由于对端主动拒绝连接,无法建立socket连接。

谷歌阻止连接的可能性很小。

很有可能您的网络托管服务提供商的防火墙设置阻止了端口 465 上的传出连接,或者他们阻止了 SMTP 到 Gmail。465 是安全 SMTP 的“错误”端口,尽管它经常被使用,而且 Gmail 确实在那里侦听。请尝试使用端口 587。如果连接仍然被拒绝,请致电您的主机并询问他们发生了什么事。

于 2013-01-06T03:48:43.947 回答
7

从您的 config.yml 中删除 spool: { type: memory }

然后你将像这样添加try catch

    try{
        $mailer = $this->getMailer();
        $response = $this->getMailer()->send($message);
    }catch(\Swift_TransportException $e){
        $response = $e->getMessage() ;
    }
于 2014-02-20T20:17:07.650 回答
6

我在使用 Godaddy 网络托管时遇到了同样的问题,并通过编辑我的.env文件解决了这个问题,

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

MAIL_HOST 是 Godaddy 的域名,MAIL_USERNAME 是 Godaddy 的用户名,MAIL_PASSWORD 是 Godaddy 的密码。

我希望这可以帮助你。

于 2016-01-04T07:11:14.787 回答
4

tcp:465 被阻止。尝试添加新的防火墙规则并添加规则端口 465。或检查 587 并将加密更改为 tls。

于 2015-12-11T09:50:23.683 回答
3

我的解决方案是改变:

  'driver' => 'smtp',

 'driver' => 'mail',

在应用程序/配置/邮件中

希望有帮助

于 2018-01-26T18:32:43.503 回答
1

我遇到了同样的问题,我设法通过禁用我的 AVAST Anti Virus 来解决它。以及我在浏览器中的 AVAST 扩展。防病毒有时会阻止您的应用程序。:) 我希望它对任何人都有帮助。

于 2017-10-25T11:21:22.680 回答
0

我有一段时间遇到同样的问题,替换:smtp.gmail.com实际上173.194.65.108对我有用!

于 2015-01-18T20:22:31.707 回答
0

就我而言,我使用的是 Laravel 5,但我忘记更改位于目录根文件夹中的 .env 文件中的邮件全局变量(这些变量会覆盖您的邮件配置)

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

默认情况下,邮件主机是:

   MAIL_HOST=mailtraper.io

我遇到了同样的错误,但这对我有用。

于 2015-04-26T23:49:36.157 回答
0

如果您已经托管了您的网站,建议您使用电子邮件或创建官方邮件来发送电子邮件假设您的网址是 www.example.com。然后转到 cpanel 创建一个电子邮件,该电子邮件将是 send@example.com。然后请求设置电子邮件。将电子邮件地址更改为 send@example.com。然后更改您的 smtp,即 mail.example.com。ssl 将保持 465

于 2020-03-04T13:30:45.260 回答
0

在 v 5.8.38 中,我将 env 文件设置如下:

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

完成后php artisan config:clear,它在分片服务器上运行良好。

于 2020-07-26T12:17:02.807 回答
0

首先,检查 gmail SMTP 服务器。您应该必须允许访问安全性较低的应用程序而不允许使用 swift mailer。为此,请先在您的电子邮件帐户中登录,然后进入隐私设置,然后单击签名和安全性,然后单击无法访问的应用程序,然后再进行较少安全应用程序选项,然后再次尝试邮件程序,它将工作。

于 2018-04-14T16:02:21.167 回答
0

就我而言,我在使用 GoDaddy 和 SSL 加密时遇到了麻烦。

将加密设置为 Null 并将端口设置为 80(或任何支持的端口)就可以了。

于 2018-11-02T16:24:30.440 回答
-2

我刚刚遇到了这个问题,并通过编辑 config 文件夹下的 mail.php 解决了这个问题。通常在使用共享主机时,他们的托管公司允许您创建电子邮件,例如

XXXX@yourdomainname.com

. 因此,请转到共享主机 cpanel 下的电子邮件设置并添加新电子邮件。获取该电子邮件和密码并将其设置在

邮件.php。

它应该工作!

于 2014-01-10T05:42:08.367 回答