问题
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:
- Tried all combinations of port (22, 465, 587) and encryption (No encryption, SSL, TLS)
- Allow access to less secure apps is on and two ways authentication is off.
checked host details:
and tried with above host name and ips.
Tested mailtrap.io and its working fine! (receiving email in mailtrap inbox, so no issue in code)
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.
- Make sure that you have proper Certificate authority bundle at your server.
- 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