0

This my code,

require_once("PHPMailer/class.PHPMailer.php");
$mail = new PHPMailer();
/* receiver details and message - start */
$toid = "example@gmail.com";
$toname = "Example";

$msg = "First name : ".$_POST["fname"] . "<br>";
$msg .= "Last name : ".$_POST["lname"]. "<br>";;
$msg .= "Email : ".$_POST["email"]. "<br>";;
$msg .= "Mobile : ".$_POST["mobile"]. "<br>";;
$msg .= "Message : ".$_POST["msg"];
$subject = "sample subject";
/* receiver details and message - end */

$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1;  // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true;  // authentication enabled
$mail->SMTPKeepAlive = true;  

/* mail smtp configuration - start */
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587; 
$mail->Username = 'test@gmail.com';  
$mail->Password = 'xxxxxx';
/* mail smtp configuration - end */

$mail->From = "test@gmail.com";
$mail->FromName = "from name";


$mail->AddAddress($toid, $toname);

$mail->Subject = $subject;
$mail->Body    = $msg;
$mail->IsHTML(true); 
if($mail->Send()){
echo "success";
}else{
echo "Failure";
}

When run the above , it throws

stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in C:\xampp\htdocs\mail\PHPMailer\class.smtp.php on line 200

mega6382
  • 9,211
  • 17
  • 48
  • 69
Renu
  • 109
  • 1
  • 1
  • 10
  • https://stackoverflow.com/questions/30371910/phpmailer-generates-php-warning-stream-socket-enable-crypto-peer-certificate – pavel Dec 07 '17 at 12:55
  • I'm not PHP guy but in C# I had similar problem when trying to send email by gmail. Solved after setting SSL=true in my request. – derloopkat Dec 07 '17 at 12:58
  • There have been a flurry of issues like this in the last week - I suspect gmail has changed a root certificate that's missing from many PHP installations, so try updating your CA certificates as the troubleshooting guide and PHP docs say. It's not this problem, but you're using a very old version of PHPMailer, so get the latest. – Synchro Dec 07 '17 at 13:11
  • i checked with latest also throws same error @Synchro – Renu Dec 07 '17 at 13:12
  • That's why I said "It's not this problem". Fix your certs, do what the guide says. – Synchro Dec 07 '17 at 13:13

2 Answers2

0

It's got nothing to do with your code - your OpenSSL CA cert store does not contain the CA cert and/or your system is running in an environment which masquerades the gmail service and/or your clock is very wrong and/or you are using a very old version of openSSL. You've told us nothing about the platform this sits on so we can't advise how to fix this.

BTW you'll also need to enable "insecure" connectivity at the google side - I don't think PHPmailer supports oauth over SMTP.

symcbean
  • 47,736
  • 6
  • 59
  • 94
  • 1
    PHPMailer has supported SMTP XOAUTH2 for a few years. See [this example](https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail_xoauth.phps), but that doesn't have anything to do with validating Gmail's certificate, which happens long before any authentication. – Synchro Dec 07 '17 at 13:03
  • I aready enabled the "insecure" connectivity at the google side... bt didn't wrk yet. – Renu Dec 07 '17 at 13:04
  • @Renu: your client has to connect before it can authenticate. – symcbean Dec 07 '17 at 13:08
  • @symcbean : I didn't get it..? – Renu Dec 07 '17 at 13:10
-1

For GMail, change your setup to:

$mail->SMTPSecure = 'ssl';
$mail->Port = 465; 
Emerson
  • 61
  • 7
  • It throws same error, SMTP -> ERROR: Failed to connect to server: (0) SMTP Error: Could not connect to SMTP host. Failure – Renu Dec 07 '17 at 13:14
  • 1
    Nope. Also, don't post unexplained code as an answer, say *why* you think it fixes the problem, though in this case it doesn't. – Synchro Dec 07 '17 at 13:14