Reputation: 375
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
Upvotes: 2
Views: 1952
Reputation: 8168
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
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);
}
Upvotes: 2