2

I am having some problems with the mail() function so when I try to send mail with PHPmailer, the below code which I copied from one tutorial is giving me error

<?php
include("PHPMailer-master/class.phpmailer.php");
include('PHPMailer-master/class.smtp.php');
$mail = new PHPMailer();
$mail->IsSMTP();  // telling the class to use SMTP
$mail->Host     = "smtp.gmail.com"; // SMTP server
$mail->Username   = "nati323@gmail.com";
$mail->Password   = "SOMEPASS";
$mail->SMTPAuth   = true;       
$mail->Port       = 587;
$mail->SMTPDebug  = 2;
$mail->From     = "from@example.com";
$mail->AddAddress("nati323@gmail.com");
$mail->Subject  = "First PHPMailer Message";
$mail->Body     = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>

and when i run it i get this error:

2015-05-18 13:46:19 SERVER -> CLIENT: 2015-05-18 13:46:19   SMTP NOTICE: EOF caught while checking if connected 2015-05-18 13:46:19 SMTP Error: Could not authenticate. 2015-05-18 13:46:19 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message was not sent.Mailer error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
Kaki Baleven
  • 355
  • 3
  • 7
  • 19
  • I'd bet your `from` can't be `from@example.com`. It likely has to be `nati323@gmail.com`. Gmail doesn't let you send from other addresses. – ceejayoz May 18 '15 at 13:53
  • maybe you right, but the problem seems to be at the SMTP connection. – Kaki Baleven May 18 '15 at 13:56
  • check your email account you get a email by Google to enable access by less secure app...enable that and then try again.. – Moid May 18 '15 at 14:04
  • Gmail requires tls connection, try adding that `$mail->SMTPSecure = 'tls';` and see if it helps. – Aziz Saleh May 18 '15 at 14:25
  • i dont get any mail like that.... , also i try to add tls and its gave me a php error: Warning: 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\site\diamonds\PHPMailer-master\class.smtp.php on line – Kaki Baleven May 18 '15 at 14:32
  • Why did you go to the effort of finding an obsolete tutorial instead of using [the up to date examples included with PHPMailer](https://github.com/PHPMailer/PHPMailer/tree/master/examples)? Did you follow [the link in the error message](https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting)? – Synchro May 18 '15 at 14:36
  • possible duplicate of [PHPMailer - SSL3\_GET\_SERVER\_CERTIFICATE:certificate verify failed](http://stackoverflow.com/questions/26827192/phpmailer-ssl3-get-server-certificatecertificate-verify-failed) –  May 18 '15 at 15:36

1 Answers1

2

NOTE: [EDIT]

You have to include the below line in your above code and check again

$mail->SMTPSecure = 'tls';

Always initialte PHPMailer by passing true parameter since it helps you to catch exceptions

   $mail = new PHPMailer(true);

Then in try block put your code of sending emails Then you can catch the exceptions like this

catch (phpmailerException $e) {
  echo $e->errorMessage(); //PHPMailer error messages
} catch (Exception $e) { 
  echo $e->getMessage(); //other error messages
}

Get the latest PHPMailer examples from here

Latest PHPMailer Examples

EDIT: change the file class.smtp.php probably in line around 238

public function connect($host, $port = null, $timeout = 30, $options = array()) {
       if (count($options) == 0) {
           $options['ssl'] = array('verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true);
       }
Abhinav
  • 8,028
  • 12
  • 48
  • 89
  • 1
    You're better off pointing at [the examples bundled with PHPMailer](https://github.com/PHPMailer/PHPMailer/tree/master/examples). They are very similar, but are kept up to date. – Synchro May 18 '15 at 14:35
  • hey, i did what you say, and like i say before when i try to set the SMTPsecure to tls, i get this php error:Warning: 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\site\diamonds\PHPMailer-master\class.smtp.php – Kaki Baleven May 18 '15 at 15:24
  • hey..I just checked it...it seems you must be using php version 5.6. php 5.6 allows ssl certificate verificatIon. check my edit – Abhinav May 18 '15 at 15:27
  • hey, thank you very much, its work good now, you dont know how much you help me, since this morning (before 8 hours) i have been tried to send email via php, now i can do it thanks to you – Kaki Baleven May 18 '15 at 16:07