-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 = "[email protected]"; $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"); } 
1
  • Depending on how strictly you want to validate it there are a number of ways - the most basic is simply changing the html <input /> 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) to test the format of the string, and/or do an MX lookup to ensure the domain has a valid mail exchange. Beyond that, you'd have to send the email. Commented Jan 2, 2020 at 15:35

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.