Reputation: 9691
I'm having trouble investigating an 500 Internal Server Error I get when trying to do an AJAX request ( I'm doing a "PUT" / "GET" ) on my server.
Locally it runs without any issues and it responds, but after I uploaded the content on the server it doesn't, as if the file / folder wouldn't be there.
The host is running on Apache with PHP at least version 5.3.0 since I last checked. I get the error when trying to use the footer newsletter subscribe.
My index file which is requested when doing the AJAX looks like the following :
<?php
/*
* Import PHPMailer Class
*/
require("phpmailer/class.phpmailer.php");
/*
* Decode JSON Data
*/
$request = json_decode(file_get_contents("php://input"), true);
/*
* Check Valid Email Address
*/
if (!filter_var($request["Email"], FILTER_VALIDATE_EMAIL)) {
echo json_encode([
"error" => true,
"message"=> "You must enter a valid email address"
]);
return false;
};
/*
* Instantiate PHPMailer Class
*/
$mailer = new PHPMailer();
/*
* Set Reply Settings
*/
$reply_email = "[email protected]";
$reply_name = "Barber Shoppen";
/*
* Specific PHPMailer Settings
*/
$mailer->IsSMTP(); /* SMTP Usage */
$mailer->SMTPAuth = true; /* SMTP Authentication */
$mailer->SMTPSecure = "ssl"; /* Sets Servier Prefix */
$mailer->Host = "smtp.gmail.com"; /* SMTP Server */
$mailer->Port = 465; /* SMTP Port */
$mailer->Username = "[email protected]"; /* SMTP Account Username */
$mailer->Password = "333333333"; /* SMTP Account Password */
/*
* Email Settings
*/
$mailer->SetFrom($reply_email, $reply_name);
$mailer->AddReplyTo($reply_email, $reply_name);
$mailer->AddAddress($request["Email"]);
$mailer->Subject = "Barber Shoppen [ Confirmation Email ]";
$mailer->Body = "You have successfully subscribed to our newsletter";
$mailer->isHTML(true);
/*
* Send Email
*/
if($mailer->Send()) {
echo json_encode([
"error" => false,
"message"=> "You have successfully subscribed"
]);
} else {
echo json_encode([
"error" => true,
"message"=> $mailer->ErrorInfo
]);
};
?>
I would appreciate some help or some pointers in which directions I should head and fix this error.
Upvotes: 1
Views: 2506
Reputation: 23001
You want to pass in a PHP-style array instead of a javascript-style array into json_encode:
if (!filter_var($request["Email"], FILTER_VALIDATE_EMAIL)) {
echo json_encode(array(
"error" => true,
"message"=> "You must enter a valid email address"
));
return false;
};
And:
if($mailer->Send()) {
echo json_encode(array(
"error" => false,
"message"=> "You have successfully subscribed"
));
} else {
echo json_encode(array(
"error" => true,
"message"=> $mailer->ErrorInfo
));
};
Upvotes: 5