Cesar
Cesar

Reputation: 534

Avoid phpMailer to send an email if it is not server validated

I have the following code to send an email with phpMailer. What I don't understand is why the email is sent and everything is OK even if I use a wrong password. In that case, the headers of the email give this answer (adapted from real case): X-PHP-Originating-Script: 532:class.phpmailer.php

How can I force the login error to appear and avoid the email being sent anyway? I guess that this has something to do with the class using other methods after trying to connect to the SMTP server. I don't want the email to be sent in ALL cases, if the password has changed or service not available, I want to know it and the script to stop and throw error. I use the latest available version of the class.

require 'phpmailer/class.phpmailer.php';
$to = "[email protected]";
$subject = "Hello World";
$body = "HELLO WORLD";
$mail = new PHPMailer;
$mail->Host       = "mail.example.com";                 
$mail->Port       = 25;                             
$mail->SMTPAuth   = true;   
$mail->Username   = "[email protected]";
$mail->Password   = "**********"; 
$mail->From = "[email protected]";
$mail->FromName = "TEST";
$mail->AddReplyTo($to);             
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->MsgHTML($body);
if(!$mail->Send())
{
   echo "KO " . $mail->ErrorInfo;
   return false;
}
else
{
  echo "OK";
  return true;
}

Upvotes: 0

Views: 69

Answers (1)

Álvaro González
Álvaro González

Reputation: 146450

You aren't using SMTP so I guess it's defaulting to mail(), which doesn't accept authentication.

To enable SMTP:

$mail->IsSMTP();

Upvotes: 2

Related Questions