1

I'm trying to get this PHP form to work, every time I submit the form the browser returns the else statement but doesn't execute the above if statement, what have I missed?

    <?php

if (isset($_POST['submit'])) {
    $name = $_POST['name'];
    $subject = $_POST['subject'];
    $mail = $_POST['mail'];
    $message = $_POST['message'];

    $to = '***.*****@gmail.com';
    $subject='Form submission';
    $message="Name: ".$name."\n". "Wrote the following: "."\n\n".$message;
    $headers="From: ".$mail;

    if (mail($to, $subject, $message, $headers)) {
    echo "<h1>Sent Successfully! Thank you"." ".$name.", I will contact you shortly.</h1>";
    }
    else {
        echo "Something went wrong! Contact me at alec.dannmayr@gmail.com";
    }
}

?>
        <input type="text" name="name" placeholder="Full Name">
        <input type="text" name="mail " placeholder="Email">
        <input type="text" name="subject" placeholder="Subject">
        <textarea name="message" placeholder="Message" cols="30" rows="10"></textarea>
        <input type="submit" name="submit"></input>
      </form>

1 Answers1

1

There is something wrong with your mail() function. Try looking in your maillog if anything is triggered at all. Usually located under /var/log/mail.log or /var/log/mail.err depending on your configuration. Have you tried sending the mail without the header (just to minimise possible errors).

It can be a pain to configure the mail() function to work properly. See this article, it might give you a hint. Is there a reason you want to use the mail() function a more advanced technique? You could use f.e. https://github.com/PHPMailer/PHPMailer which supports a better error/debugging management.

Amacado
  • 630
  • 5
  • 20
  • Nowadays, using `mail()` is a little bit odd as Amacado pointed. Another popular option is SwiftMailer. Although dedicated mailing classes are more complicated, they also give you a lot of advantages, like custom transport layers, better handling for attachments, etc. https://github.com/swiftmailer/swiftmailer – biesior Jul 11 '20 at 12:00
  • 1
    True words @biesior. I like swiftmailer even more then PHPMailer. – Amacado Jul 11 '20 at 12:02