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   = "[email protected]";
$mail->Password   = "SOMEPASS";
$mail->SMTPAuth   = true;       
$mail->Port       = 587;
$mail->SMTPDebug  = 2;
$mail->From     = "[email protected]";
$mail->AddAddress("[email protected]");
$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
7
  • I'd bet your from can't be [email protected]. It likely has to be [email protected]. Gmail doesn't let you send from other addresses.
    – ceejayoz
    Commented May 18, 2015 at 13:53
  • maybe you right, but the problem seems to be at the SMTP connection. Commented May 18, 2015 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
    Commented May 18, 2015 at 14:04
  • Gmail requires tls connection, try adding that $mail->SMTPSecure = 'tls'; and see if it helps.
    – Aziz Saleh
    Commented May 18, 2015 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 Commented May 18, 2015 at 14:32

1 Answer 1

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);
       }
4
  • 1
    You're better off pointing at the examples bundled with PHPMailer. They are very similar, but are kept up to date.
    – Synchro
    Commented May 18, 2015 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 Commented May 18, 2015 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
    Commented May 18, 2015 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 Commented May 18, 2015 at 16:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.