Swift_TransportExceptionConnection could not be established with host smtp.gmail.com

断了今生、忘了曾经 提交于 2020-01-16 14:48:03

问题


Trying to send email in php.

transport: smtp
host: smtp.gmail.com
username: example@example.com
password: password
port: 587
encryption: ssl

Options already tried:

  1. Tried all combinations of port (22, 465, 587) and encryption (No encryption, SSL, TLS)
  2. Allow access to less secure apps is on and two ways authentication is off.
  3. checked host details:

    and tried with above host name and ips.

  4. Tested mailtrap.io and its working fine! (receiving email in mailtrap inbox, so no issue in code)

  5. I'm using same gmail account in another .net application and its working fine over there.

What could be the option left to try?


回答1:


I use this and work

    'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'viewPath' => '@common/mail',
        'useFileTransport' => false,//set this property to false to send mails to real email addresses
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'smtp.gmail.com',
            'username' => 'your_name@gmail.com', // a valid gmail account
            'password' => 'your_password', // the related  passwordd
            'port' => '587',
            'encryption' => 'tls',
        ],



回答2:


Added this line in streamBuffer.php and it worked!

$options = array_merge($options, array('ssl' => array('allow_self_signed' => true, 'verify_peer' => false)));



回答3:


Added this line in streamBuffer.php and it worked!

$options = array_merge($options, array('ssl' => array('allow_self_signed' => true, 'verify_peer' => false)));

Don't disable ssl certificate verification - this will open you to MITM attacks.

I'm pretty sure that Gmail has correct certificate, so you should try to fix this at your end.

  1. Make sure that you have proper Certificate authority bundle at your server.
  2. If you're behind some trusted proxy which works like MITM, you should add proxy's certificate to your trusted certificates.

You may want to look at How to update cURL CA bundle on RedHat?, How do you add a certificate authority (CA) to Ubuntu? or similar instructions related to your system.

Also, take a look at similar issue at Composer bug tracker.



来源:https://stackoverflow.com/questions/50028966/swift-transportexceptionconnection-could-not-be-established-with-host-smtp-gmail

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!