PHP Build a Basic PHP Website (2018) Adding a Basic Form Validating Form Data

Parvez Noor
Parvez Noor
Pro Student 15,917 Points

After reading Github, watching videos, and scanning my code meticulously, my PHPMailer still isn't working - help please

Hi guys,

I'm making a simple website. It doesn't have too much functionality. The header and footer are included using php.

There is only one page - the index page.

On the index page, I have a simple contact form at the bottom. I've tried to connect this to my gmail using SMTP, after following Alena's building a basic PHP website course.

Everything seems to be working, except, it's not! I can still send the form without entering anything in the fields. Also, the page doesn't redirect to index.php?status=thanks. When I refresh the page, it still has problems with double form submission, and, worst of all, I'm still not getting any emails!

This is the website:

I'm assuming the whole if($_SERVER["REQUEST_METHOD"] == "POST") conditional is not even working, Can anyone help me figure out why?

Here is my code (I've removed most irrelevant html code to just show the form and the phpmailer code):

        use PHPMailer\PHPMailer\PHPMailer;
        use PHPMailer\PHPMailer\Exception;
        require 'vendor/phpmailer/src/PHPMailer.php';
        require 'vendor/phpmailer/src/Exception.php';
        require 'vendor/phpmailer/src/SMTP.php';

        if($_SERVER["REQUEST_METHOD"] == "POST") {
            $name = trim(filter_input(INPUT_POST,"name",FILTER_SANITIZE_STRING));
            $email = trim(filter_input(INPUT_POST,"email",FILTER_SANITIZE_EMAIL));
            $subject = trim(filter_input(INPUT_POST,"subject",FILTER_SANITIZE_STRING));
            $message = trim(filter_input(INPUT_POST,"message",FILTER_SANITIZE_SPECIAL_CHARS)); 

            if ($name == "" || $email == "" || $message == "") {
                $error_message = "Please fill in the required fields: Name, Email, Message";
                if (!isset($error_message) && $_POST["address"] != "") {
                $error_message =  "Bad form input";
            if (!isset($error_message) && !PHPMailer::validateAddress($email)) {
                $error_message =  "Invalid Email Address";
            if(!isset($error_message)) {
                $email_body = "";
                $email_body .= "Contact by " . $name . " about " . $subject . "\n";
                $email_body .= "Message: " . $message . "\n";
                $email_body .= "Reply to " . $name . ": " . $email;

                $mail = new PHPMailer;
                try {
                    $mail->SMTPDebug = 2;
                    $mail->Host = '';
                    $mail->Port = 587;
                    $mail->SMTPSecure = 'tls';
                    $mail->SMTPAuth = true;
                    $mail->Username = "email";
                    $mail->Password = "password";
                    $mail->setFrom('email', $name);
                    $mail->addReplyTo($email, $name);
                    $mail->addAddress('email', 'Parv Noor');
                    $mail->Subject = $subject . " from " . $name;
                    $mail->Body = $email_body;
                        ($mail->send()) {
                } catch (Exception $e) { 
                    $error_message = "Mailer Error: " . $mail->ErrorInfo;

          <section class="contact-us">
                <h1 class="get-in-touch">Get In Touch</h1>
                <?php if(isset($_GET["status"]) && $_GET["status"] == "thanks") {
                    echo  "<p> Thanks for the email! I&rsquo;ll check out your suggestion shortly!</p>"; 
                  } else { ?>
                <form class="contact-us-form" method="post" action="index.php">
                    <input class="name" type="text" id="name" name="name" placeholder="Name" value="<?php 
                    if (isset($name)) echo $name; ?>"/>
                    <input class="email" type="text" id="email" name="email" placeholder="Email" value="<?php 
                    if (isset($email)) echo $email;?>"/>
                    <input class="subject" type="text" id="subject" name="subject" placeholder="Subject" value="<?php 
                    if (isset($subject)) echo $subject;?>"/>
                    <textarea class="message" id="message" name="message" placeholder="Message"><?php 
                        if (isset($message)) echo htmlspecialchars($_POST['message']);
                    <input style="display:none" type="text" id="address" name="address" placeholder="Please leave blank" />
                    <input class="submit button" type="submit" value="Send">
            <?php }