erick.chali erick.chali - 7 months ago 27
PHP Question

PHPMailer response

I'm using phpmailer to send email with the function:

function send_email($address, $message){
$mail = new PHPMailer;
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = "name@gmail.com";
$mail->Password = "password";
$mail->SMTPSecure = "tls";
$mail->Port = 587;

$mail->From = "name@gmail.com";
$mail->FromName = "Name";
$mail->addAddress($address, "Cust. name");

$mail->isHTML(true);

$mail->Subject = "Nueva clave";
$mail->Body = "<i>".$message."</i>";
$mail->AltBody = "This is yout password.";

if($mail->send())
{
return true;
}else{
return false;
}
}


in my php file I set the response header this way:

header('Content-Type: application/json');
after the process that needs to be made I handle email this way:

if(send_email('customer@gmail.com', $message))
{
array_push($response, 'email sent');
array_push($response, $new_password);
echo json_encode(array('errors'=>$errors, 'response' => $response));
}
else
{
array_push($errors, 'email not send');
array_push($errors, $new_password);
echo json_encode(array('errors'=>$errors, 'response' => $response));
}


normally I expected a response from the server like: Object[errors[],response[]] just as several times I have done this, however instead I'm receiving this as an answer JSON RESPONSE

why I'm getting this response, do I need any other configuration that was not set.
Thanks

Answer

You are including debug output in your response, which is not valid JSON. Just do this to turn it off:

$mail->SMTPDebug = 0;