-1

is there a way to validate an email in this form without rewriting it completely? It'd be great if I could just add something to this. I'm new to PHP and this form is taken from mmtuts video. If you know how to help, I'd be glad if you did.

<?php

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

    $mailTo = "misavoz@seznam.cz";
    $headers = "From: ".$mailFrom;
    $txt = "Pan/í ".$name." vám poslal/a zprávu. Telefonní číslo: ".$number."\n\n".$message;

    mail($mailTo, $subject, $txt, $headers);
    header("Location: index.php?mailsend");
}
Mausecat
  • 13
  • 1
  • 2
  • Depending on *how* strictly you want to validate it there are a number of ways - the most basic is simply changing the html `` type from `text` to `email` and just letting the frontend deal with it (not recommended). On the server you can use PHP's `filter_var()` function with `FILTER_VALIDATE_EMAIL` ([example](https://www.php.net/manual/en/filter.examples.validation.php)) to test the format of the string, and/or do an [MX lookup](https://www.php.net/manual/en/function.getmxrr.php) to ensure the domain has a valid mail exchange. Beyond that, you'd have to send the email. – CD001 Jan 02 '20 at 15:35

1 Answers1

3

In the past I have used:

if(filter_var($email, FILTER_VALIDATE_EMAIL))
{
    //send email
}
else
{
    //show error
}

Source: https://www.php.net/manual/en/function.filter-var.php

PHPology
  • 1,017
  • 6
  • 12