Swift_TransportException无法使用host smtp.gmail.com建立连接

And*_*res 26 php swiftmailer email-attachments

我无法想象我的生活为什么它失败了......这是我得到的确切错误:

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
Run Code Online (Sandbox Code Playgroud)

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

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);
Run Code Online (Sandbox Code Playgroud)

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

编辑:显然这是一个gmail的问题,我改为不同的smtp,它工作..:S

Cha*_*les 18

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

拒绝连接是一个非常明确和明确的错误消息.这意味着无法建立套接字连接,因为远程端主动拒绝连接.

Google阻止连接的可能性很小.

您的网络托管服务提供商很可能具有阻止端口465上的传出连接的防火墙设置,或者它们阻止SMTP到Gmail.465是安全SMTP的"错误"端口,虽然经常使用,但Gmail确实在那里收听.请尝试使用端口587.如果仍然拒绝连接,请致电您的主机并询问他们的情况.


iva*_*kov 6

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

然后你将添加像这样的try catch

    try{
        $mailer = $this->getMailer();
        $response = $this->getMailer()->send($message);
    }catch(\Swift_TransportException $e){
        $response = $e->getMessage() ;
    }
Run Code Online (Sandbox Code Playgroud)


Ama*_*rja 5

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

MAIL_DRIVER=smtp
MAIL_HOST=XXXX.XXXX.in
MAIL_PORT=587
MAIL_USERNAME=dexxxxx
MAIL_PASSWORD=XXXXXXXX
MAIL_ENCRYPTION=tls
Run Code Online (Sandbox Code Playgroud)

其中 MAIL_HOST 是您在 Godaddy 的域名,MAIL_USERNAME 是您在 Godaddy 的用户名,MAIL_PASSWORD 是您在 Godaddy 的密码。

我希望这可以帮助你。