Jump to content

Not receiving email after submitting form


warren_thieras

Recommended Posts

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;
        }
    }
}

?>

 

Link to comment
Share on other sites

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 by phppup
Typos
Link to comment
Share on other sites

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'.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.