Facts
I am using PEAR Mail, I want to use gmail SMTP to send a mail. I have Apache/2.4.27 (Win64) PHP/7.2.0beta3, PEAR 1.10.15, Mail 1.4.1, Net_SMTP 1.8.0, Net_Socket 1.2.2.
I went to php.ini
and added extension = php_openssl.dll
. The error.log
gives no ssl-related errors.
Here is the code
require_once "Mail.php"; $from = '<slevin@gmail.com>'; $to = '<slevinkelevra@gmal.com>'; $subject = 'Hi!'; $body = "Hi,\n\nHow are you?"; $headers = array( 'From' => $from, 'To' => $to, 'Subject' => $subject ); $smtp = Mail::factory('smtp', array( 'host' => 'ssl://smtp.gmail.com', 'port' => '465', 'auth' => true, 'username' => 'slevinmail@gmail.com', 'password' => 'mypassword' )); $mail = $smtp->send($to, $headers, $body); if (PEAR::isError($mail)) { echo('<p>' . $mail->getMessage() . '</p>'); } else { echo('<p>Message successfully sent!</p>'); }
The problem
I get this error
Failed to connect to ssl://smtp.gmail.com:465 [SMTP: Failed to connect socket: fsockopen(): unable to connect to ssl://smtp.gmail.com:465 (Unknown error) (code: -1, response: )]
and I have no clue what to do, I Googled but I got more confused.
Please advice on how to fix this. Thank you
Update
Following symcbean's instructions I got the following results :
bool(true) array(5) { [0]=> string(31) "alt3.gmail-smtp-in.l.google.com" [1]=> string(26) "gmail-smtp-in.l.google.com" [2]=> string(31) "alt4.gmail-smtp-in.l.google.com" [3]=> string(31) "alt1.gmail-smtp-in.l.google.com" [4]=> string(31) "alt2.gmail-smtp-in.l.google.com" } IPV4 address = 64.233.188.27 If you've got this far without errors then problem is with your SSL config Check you've got your cacerts deployed in one of the following locations default_cert_file = C:\Program Files\Common Files\SSL/cert.pem default_cert_file_env = SSL_CERT_FILE default_cert_dir = C:\Program Files\Common Files\SSL/certs default_cert_dir_env = SSL_CERT_DIR default_private_dir = C:\Program Files\Common Files\SSL/private default_default_cert_area = C:\Program Files\Common Files\SSL ini_cafile = ini_capath = If all good so far, then this bit should work.... fsockopen Warning: fsockopen(): SSL operation failed with code 1. OpenSSL Error messages: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed in C:\Apache24\htdocs\phptest2.php on line 28 Warning: fsockopen(): Failed to enable crypto in C:\Apache24\htdocs\phptest2.php on line 28 Warning: fsockopen(): unable to connect to ssl://smtp.gmail.com:465 (Unknown error) in C:\Apache24\htdocs\phptest2.php on line 28 bool(false) int(0) string(0) ""
Line 28 is this line var_dump(fsockopen("ssl://smtp.gmail.com", 465, $errno, $errstr, 3.0));
Thanks again
Update #2
I googled just "fsockopen(): SSL operation failed with code 1." of the first warning.
End up here . I changed the mail port of the AVG, like the answer. symcbean's code run with no errors, but my code replied with mail error : authentication failure [SMTP: Invalid response code received from server (code: 534, response: 5.7.14 Please log in via your web browser and 5.7.14 then try again. 5.7.14 Learn more at 5.7.14 https://support.google.com/mail/answer/78754 c1sm1243434wre.84 - gsmtp)]
So I googled code: 534, response: 5.7.14
and end-up here, followed the instructions of the first answer by emgh3i, enabled less secured connections and allowed access to my google account
And its working perfectly now.
7 Answers
Answers 1
Few debugging steps :
1. check phpinfo
I recommend checking phpinfo()
to check whether all modules are enabled. Check for mail, fsocketopen.
2. Enable debug flag
Enable debug
flag to check exactly what's the problem. Like below.
$smtp = Mail::factory('smtp', array( 'host' => 'ssl://smtp.gmail.com', 'port' => '465', 'auth' => true, 'debug' => true, 'pipelining' => true, 'username' => 'xxx@gmail.com', 'password' => 'xxx' ));
After running above code on my machine I got follow response. Issue can be different from yours. But debug helped me. As I have 2FA enabled, it gave me error. And I got a mail also, that my login has been blocked.
DEBUG: Recv: 220 smtp.gmail.com ESMTP s65sm4891344pfi.36 - gsmtp DEBUG: Send: EHLO localhost DEBUG: Recv: 250-smtp.gmail.com at your service, [110.227.210.84] DEBUG: Recv: 250-SIZE 35882577 DEBUG: Recv: 250-8BITMIME DEBUG: Recv: 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH DEBUG: Recv: 250-ENHANCEDSTATUSCODES DEBUG: Recv: 250-PIPELINING DEBUG: Recv: 250-CHUNKING DEBUG: Recv: 250 SMTPUTF8 DEBUG: Send: AUTH LOGIN DEBUG: Recv: 334 VsadfSFcm5hbWU6 DEBUG: Send: cGF0ZWwuZ29wYhkafdaASFnbWFpbC5jb20= DEBUG: Recv: 334 UGFzc3dvcmQ6 DEBUG: Send: OWwzMy5zaHlAbTE4 DEBUG: Recv: 534-5.7.14 Please log in via your web browser and DEBUG: Recv: 534-5.7.14 then try again. DEBUG: Recv: 534-5.7.14 Learn more at DEBUG: Recv: 534 5.7.14 https://support.google.com/mail/answer/78754 s65sm4891344pfi.36 - gsmtp DEBUG: Send: RSET DEBUG: Send: QUIT DEBUG: Recv: 250 2.1.5 Flushed s65sm4891344pfi.36 - gsmtp DEBUG: Recv: 221 2.0.0 closing connection s65sm4891344pfi.36 - gsmtp authentication failure [SMTP: Invalid response code received from server (code: 534, response: 5.7.14 Please log in via your web browser and 5.7.14 then try again. 5.7.14 Learn more at 5.7.14 https://support.google.com/mail/answer/78754 s65sm4891344pfi.36 - gsmtp)]
Update:
Your issue looks like PHP is not even able to connect to gmail server.
Answers 2
Your host
configuration shouldn't contain the protocol. The reason it's failing is because it's probably trying to perform a DNS Lookup on ssl://smtp.gmail.com
and failing.
Change
'host' => 'ssl://smtp.gmail.com',
to
'host' => 'smtp.gmail.com',
Answers 3
Your code is correct
I tried to test my gmail account. Mail sending was successful.
Check your socket connection
<?php error_reporting(E_ALL); var_dump(fsockopen("ssl://smtp.gmail.com", 465, $errno, $errstr)); var_dump($errno); var_dump($errstr);
resource(4) of type (stream)
int(0)
string(0) ""
Answers 4
Been Kyung-yoong is the only person to have made a meaningful contribution to solving the problem so far (+1 Been!). I can confirm his result. And I would recommend you try the same. You are currently trying to debug a rather complex stack of components:
- Error is being thrown by PEAR smtp here
- Which is failing when it calls Net_SMTP::connect()
- which is a wrapper around Net_Socket::connect()
- which is rather elaborate wrapper around fsockopen()
Been is doing your job for you - as the person posting the question - should be creating a Minimal, Complete, and Verifiable example
This will hopefully also provide more meaningful diagnostic information.
The most likely reasons for this to be failing are:
- the host you are running this on cannot route outgoing connections to the internet (but since you seem to be using a desktop PC, I would think you might have noticed this by now)
- the code is running within a security sandbox (but MSWindows doesn't really have such things)
- the host is unable to resolve the hostname (see first point about routing)
- the host is able to connect but unable to verify the certificate
Hence you might consider this more elaborate implementation of a test script:
<?php error_reporting(E_ALL); print "DNS\n"; var_dump(getmxrr('gmail.com',$result)); var_dump($result); $use_ip=gethostbyname($result[0]); print "IPV4 address = $use_ip\n"; print "\nIf you've got this far without errors then problem is with your SSL config\n"; $calocns=openssl_get_cert_locations(); if (count($calocns)) { print "Check you've got your cacerts deployed in one of the following locations\n"; foreach ($calocns as $k=>$v) print "$k = $v\n"; } else { print "You've not configured your openssl installation on this host\n"; } print "\nIf all good so far, then this bit should work....\n"; print "fsockopen\n"; var_dump(fsockopen("ssl://smtp.gmail.com", 465, $errno, $errstr, 3.0)); var_dump($errno); var_dump($errstr);
Which should give you a response like this:
DNS bool(true) array(5) { [0]=> string(31) "alt1.gmail-smtp-in.l.google.com" [1]=> string(31) "alt2.gmail-smtp-in.l.google.com" [2]=> string(31) "alt4.gmail-smtp-in.l.google.com" [3]=> string(26) "gmail-smtp-in.l.google.com" [4]=> string(31) "alt3.gmail-smtp-in.l.google.com" } IPV4 address = 74.125.131.26 If you've got this far without errors then problem is with your SSL config Check you've got your cacerts deployed in one of the following locations default_cert_file = /usr/lib/ssl/cert.pem default_cert_file_env = SSL_CERT_FILE default_cert_dir = /usr/lib/ssl/certs default_cert_dir_env = SSL_CERT_DIR default_private_dir = /usr/lib/ssl/private default_default_cert_area = /usr/lib/ssl ini_cafile = ini_capath = If all good so far, then this bit should work.... fsockopen resource(4) of type (stream) int(0) string(0) ""
Given that we can't replicate your error we can't give a definitive answer what the problem is - but my guess would be that you haven't configure openSSL.
Answers 5
Before I begin, let me preface this that there are many possibly solutions and outcomes between your server and the google server, so these may or may not work for different people.
1) SMTP is not very secure, so Google may be rejecting your request. I had this problem 6 months ago and the solution was enabling insecure apps under 'myaccount.google.com'
2) If that doesn't work for you then you may consider switching protocols.
from
'host' => 'ssl://smtp.gmail.com',
to
'host' => 'tls://smtp.gmail.com:587';
Answers 6
The "Use the Gmail SMTP Server" section of this guide says you need to enable "Less secure apps".
I notice that the
from
address you are using for the message is not the same as the Gmail account you are using to send. I would guess that is a problem. Try using$from = '<slevinmail@gmail.com>';
, so that address is the same one you are using in the'username' => 'slevinmail@gmail.com',
field.
Answers 7
When something fail and we don't know the cause we have to do debugging. So here instead of putting an answer I am requesting you to execute some tests
confirm system connectivity with internet: Open cmd terminal and type
ping smtp.gmail.com
confirm firewall: Enter following in cmd terminal
telnet smtp.gmail.com 465
confirm php setup: enter
php -a
at cmd terminal and on php prompt execute (copy / paste and then press enter) following code.$result = fsockopen('ssl://smtp.gmail.com', 465, $error_no, $error_message, 5); if ($result === false) { echo "error no: $error_no error message: $error_message"; echo print_r($result, true); } else { echo 'success'; }
confirm Pear Mail library and Gmail SMTP access: again on cmd and php prompt
php -a
execute your own code (as you posted in this thread)
And lets know where it breaks, and what is the error. Only after that we can help
0 comments:
Post a Comment