warren_thieras Posted January 28, 2021 Share Posted January 28, 2021 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 = 'name@example.com'; $mail->Password = 'P@ssword123'; $mail->SMTPSecure = 'tls'; $mail->Port = 587; $mail->setFrom($email, 'Mailtrap Website'); $mail->addAddress('piotr@mailtrap.io ', '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; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/312063-not-receiving-email-after-submitting-form/ Share on other sites More sharing options...
gw1500se Posted January 28, 2021 Share Posted January 28, 2021 Did you check your mail server logs and/or httpd logs? Quote Link to comment https://forums.phpfreaks.com/topic/312063-not-receiving-email-after-submitting-form/#findComment-1584081 Share on other sites More sharing options...
phppup Posted January 28, 2021 Share Posted January 28, 2021 (edited) I'm no expert, but in an effort to assist, I would think some simple troubleshooting is in order. For starters, your code says: if($mail->send()){ header('Location: thank-you.html'); // redirect to 'thank you' page } else { $errorMessage = 'Oops, something went wrong. Mailer Error: ' . $mail->ErrorInfo; } So, my first question is: After you send a test email do you EITHER get redirected to the Thank You page OR see the ERROR? The answer to this question should at least get you started in a direction with analysis. PS: Experience has taught me to check inbox and SPAM folders. Also, there is sometimes a delay on receiving emails from strange addresses depending on your service provider. Edited January 28, 2021 by phppup Typos Quote Link to comment https://forums.phpfreaks.com/topic/312063-not-receiving-email-after-submitting-form/#findComment-1584086 Share on other sites More sharing options...
maxxd Posted January 29, 2021 Share Posted January 29, 2021 Also, some mail services will mark a message as spam or not deliver it if the 'from' address doesn't match the actual domain from which the email is sent. You set from to the email of the user filling out the form - probably not going to match your domain. If your mail actually isn't going anywhere or is ending up in the spam folder, try changing the 'from' header to 'contact@jtconfirmation.co.za'. Quote Link to comment https://forums.phpfreaks.com/topic/312063-not-receiving-email-after-submitting-form/#findComment-1584101 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.