'PHPmailer: Failed to load resource: the server responded with a status of 500 (Internal Server Error)
I am trying to email a form data with phpmailer. I am stuck with it. for some reason I am getting Failed to load resource: the server responded with a status of 500 (Internal Server Error).
I have 2 pages. the index.php and sendit.php. the index.php has a bootstrap modal box (the form data to be send to the user email). the user fill in the form, click on save. the data will save into the database, and call the sendMail function to perform the sending operation. The sendMail function make a ajax call to the sendit.php and send the data
When I hard code the data in the data array in the sendit.php, it will work. when I change it to $_POST['name'], don't work.
Need HELP
Thanks in advance. the code:
this is the index.php snippet code
$('#btnSave').on('click', function() {
// some code
// the ajax call
$.ajax({
type: "POST",
url: 'addEvent.php',
data: thedata,
success: function(d) {
if (d == 'sucess') {
// data to send to client email
data = {
"name": $('#inpName').val(),
}
sendMail(data); // call the function sendMail.
resetForm(); // reset the form
} else if (d === 'false') {
//show some message
},
error: function(error) {
alert(error);
}
});
}
// the sendMail function
function sendMail(theData)
{
$.ajax({
type: "POST",
url: "sendit.php",
data: theData
})
}
Here is the snippet code for the sendit.php
<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require '../sendmail/mailer/autoload.php';
//Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.gmail.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = '[email protected]'; //SMTP username
$mail->Password = 'thepassword'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 587; //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom('client email adres', 'client name');
$data=[
lastName=>$_POST('name'),
];
// the message body
$body='
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Afspraak kaart</title>
</head>
<body>
<div class="afspraak-container">
<h2 class="afspraal-head">
Afspraak kaart
</h2>
<hr>
<div class="afspraak-body">
<p>Mr/Mevr '.$data[lastName].'</p>
<p>Bedankt voor uw afspraak bij het Kadaster en Openbare Registers.
</p>
<p>
Uw afspraak gegevens:
</p>
<div class="afspraak-card">
<div class="afspraak-card-head">
24 mei 2021,10:20 AM
</div>
<ul>
<li>Afspraak volgnr: 101</li>
<li>Naam: Joel Goncalves de Freitas</li>
<li>Product: Meetbrief</li>
</ul>
</div>
</div>
<footer>
</footer>
</div>
</body>
</html>';
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'test';
$mail->Body = $body;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
Solution 1:[1]
A 500 Error means that you have a server error on that file.
The PHP code is formally correct - at least if you're running a recent enough PHP - but on second thought, this is wrong:
$_POST('name')
The code will try to call the $_POST function, and crash. It should be $_POST['name']
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |