I'm trying to create a php script to send a filled form from a website to a specific email. I tried several ways but I don't get the email on my webmail. I also don't get any errors so I am not sure where the problem is. The files are in the same directory and the webmail seems to work well. I would be grateful for your input.
This is email.php file:
<?php
$error = ""; $successMessage = "";
if ($_POST) {
if (!$_POST["name"]) {
$error .= "A name is required.<br>";
}
if (!$_POST["email"]) {
$error .= "An email address is required.<br>";
}
if (!$_POST["choice"]) {
$error .= "Select a trial session.<br>";
}
if (!$_POST["date"]) {
$error .= "Select a date.<br>";
}
if ($_POST['email'] && filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false) {
$error .= "The email address is invalid.<br>";
}
if ($error != "") {
$error = '<div class="alert alert-danger" role="alert"><p>There were error(s) in your form:</p>' . $error . '</div>';
} else {
$emailTo = "my@addressemail.com";
$subject = "trial session";
$name = $_POST['name'];
$choice = $_POST['choice'];
$date = $_POST['date'];
$headers = "From: ".$_POST['email'];
if (mail($emailTo, $subject, $name, $choice, $date, $headers)) {
$successMessage = '<div class="alert alert-success" role="alert">Your message was sent, we\'ll get back to you ASAP!</div>';
} else {
$error = '<div class="alert alert-danger" role="alert"><p><strong>Your message couldn\'t be sent - please try again later</div>';
}
}
}
?>
<div class="container">
<h1>Get in touch!</h1>
<div id="error"><? echo $error.$successMessage; ?></div>
<form method="post" action=”email.php”>
<fieldset class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" >
</fieldset>
<fieldset class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter email">
<small class="text-muted">We'll never share your email with anyone else.</small>
</fieldset>
<div class="mb-3">
<div class="control-label" id="choice" name="choice">Which trial session you would like to book?</div>
<select class="form-select" required aria-label="select example" id="choice" name="choice">
<option value="">I'm interested in...</option>
<option value="1">option1</option>
<option value="2">option2</option>
</select>
</div>
<div class="form-group"> <!-- Date input -->
<label class="control-label" for="date">Date</label>
<input class="form-control" id="date" name="date" placeholder="MM/DD/YYY" type="text"/>
</div>
<button type="submit" id="submit" class="btn btn-primary">Submit</button>
</form>
</div>