HI guys, I’m having some problems understanding exactly how php mailer works.
Following my previous thread, I now have a php file which deals only with sending the email when a form is submitted (through ajax). Now, what I’d like is to have an error displayed if the form, for whatever reason other than incorrect input, isn’t sent to the email address specified, but currently I don’t get anything. OK let’s first look at the script:
formhandler.php
[php] <?php
require ‘PHPMailer-master/PHPMailerAutoload.php’;
//header(‘Content-Type: application/json’);
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$firstName = filter_input(INPUT_POST, 'firstName', FILTER_SANITIZE_STRING);
$lastName = filter_input(INPUT_POST, 'lastName', FILTER_SANITIZE_STRING);
$emailAddress = filter_input(INPUT_POST, 'emailAddress', FILTER_SANITIZE_EMAIL);
$message = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_STRING);
$website = filter_input(INPUT_POST, 'website', FILTER_SANITIZE_URL);
$mail = new PHPMailer(true);
$mail->Timeout = 30; // set the timeout (seconds)
//$mail->isSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.live.com"; // sets hotmail as the SMTP server
$mail->Port = 587; // set the SMTP port for the hotmail server
$mail->Username = "[email protected]"; // hotmail username
//$mail->Username = "hotmail"; // hotmail username
$mail->Password = "**********"; // hotmail password
//$mail->setFrom($emailAddress, $firstName . ' ' . $lastName);
$mail->setFrom("[email protected]", "No reply");
$mail->addAddress('[email protected]', 'Giasone Attinio');
$mail->Subject = 'New request';
//$mail->Body = $message . "\r\n\r\nWebsite: " . $website;
$mail->Body = "First Name: " .$firstName . "\r\n\r\nLast Name: " . $lastName . "\r\n\r\nEmail address: " .$emailAddress . "\r\n\r\nMessage: " . $message . "\r\n\r\nWebsite: " . $website;
if(!$mail->send()){
echo "Mailer Error: " . $mail->ErrorInfo;
http_response_code(400);
}
else{
echo "success";
}
}
?>[/php]
And this is the Ajax form submission bit:
form_en.js
function sendForm($currentForm){
var $form = $currentForm;
var formAction = $form.attr('action');
var hasFormData = Boolean(window.FormData !== undefined);
var formData = hasFormData ? new FormData(document.forms[0]) : $form.serialize();
$.ajax({
url: formAction,
data: formData,
contentType: hasFormData ? false : 'application/x-www-form-urlencoded; charset=UTF-8',
processData: hasFormData ? false : true,
type: "POST",
success: function(data, textStatus, jqXHR){
//hideForm($currentForm,data);
$(".form-error").remove();//remove error first thing
clearFields();//clear fields
addSuccessMsg();//add a success message
},
error: function(jqXHR, textStatus, errorThrown){
//hideForm($currentForm,textStatus);
}
});
}
With this code the email gets submitted OK. To get the submission to fail, I remove the comment from this line in the php file:
[php]$mail->isSMTP();[/php]
If I add this the form doesn’t submit and I get a 500 error in the console: now, how do I get this error to appear on the page?