Good day
i am not receiving emails when hitting submit; not sure if my code is correct.
jtconfirmation.co.za
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Names Confirmation RSVP</title>
<link rel="stylesheet" href="church.css">
</head>
<body>
<form action="/mail_form.php" method="POST" id="church-form">
<div class="top">
<nav>
<a href="index.html" class="mybutton">Home</a>
</nav>
</div>
<div class="form">
<div class="info">
<h1>RSVP</h1>
<h2>for the Confirmation Service of</h2>
<h1>Name</h1>
<p class="line">________________________________________</p>
<h2>The Details</h2>
<p>Sunday, 28 February 2021</p>
<p>10:00 AM</p>
<p>Please RSVP by 15 February 2021</p>
<h2>Confirmation Service</h2>
<p><a href="#" target="_blank">
Calvyn Protestant Church Diep River</a></p>
<p>10 Keswick St, Elfindale</p>
<p class="line">________________________________________</p>
<input type="text" value="name" placeholder="Name">
<input type="email" value="email" placeholder="Email">
</div>
<?php echo((!empty($errorMessage)) ? $errorMessage : '') ?>
<button type="submit" class="accept" value="Send">Accept</button>
<button type="submit" class="regret" value="Send">Regret</button>
</div>
</form>
<script src="//cdnjs.cloudflare.com/ajax/libs/validate.js/0.13.1/validate.min.js"></script>
<script>
const constraints = {
name: {
presence: {allowEmpty: false}
},
email: {
presence: {allowEmpty: false},
email: true
}
};
const form = document.getElementById('church-form');
form.addEventListener('submit', function (event) {
const formValues = {
name: form.elements.name.value,
email: form.elements.email.value
};
const errors = validate(formValues, constraints);
if (errors) {
event.preventDefault();
const errorMessage = Object
.values(errors)
.map(function (fieldValues) {
return fieldValues.join(', ')
})
.join("\n");
alert(errorMessage);
}
}, false);
</script>
</body>
</html>
s
<?php
use PHPMailer\PHPMailer\PHPMailer;
require __DIR__ . '/vendor/autoload.php';
$errors = [];
$errorMessage = '';
if (!empty($_POST)) {
$name = $_POST['name'];
$email = $_POST['email'];
if (empty($name)) {
$errors[] = 'Name is empty';
}
if (empty($email)) {
$errors[] = 'Email is empty';
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Email is invalid';
}
if (!empty($errors)) {
$allErrors = join('<br/>', $errors);
$errorMessage = "<p style='color: red;'>{$allErrors}</p>";
} else {
$mail = new PHPMailer();
// specify SMTP credentials
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '
[email protected]';
$mail->Password = 'P@ssword123';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom($email, 'Mailtrap Website');
$mail->addAddress('
[email protected] ', 'Me');
$mail->Subject = 'RSVP Church';
// Enable HTML if needed
$mail->isHTML(true);
$bodyParagraphs = ["Name: {$name}", "Email: {$email}", "Message:", nl2br($message)];
$body = join('<br />', $bodyParagraphs);
$mail->Body = $body;
echo $body;
if($mail->send()){
header('Location: thank-you.html'); // redirect to 'thank you' page
} else {
$errorMessage = 'Oops, something went wrong. Mailer Error: ' . $mail->ErrorInfo;
}
}
}
?>