This question already has an answer here:
- PHP mail function doesn't complete sending of e-mail 27 answers
- PHPMailer not working on 000webhost 2 answers
I want to make a simple contact form but I guess I am out of luck again. I tested it on a website I hosted on 000webhost. I don't receive any e-mails. I am pretty much a newbie at PHP, so what is the reason? Is it my code? Or it's the hosting? Thanks!
<?php
$recipient = "************@gmail.com";
$first_name = $last_name = $email = $subject = $message = "";
$first_name_empty = $last_name_empty = $email_invalid = $subject_empty = $message_empty = "";
$trust = 0;
function sending($input) {
$input = trim($input);
$input = stripslashes($input);
$input = htmlspecialchars($input);
return $input;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["first_name"]) || !preg_match("/^[a-zA-Z ]*$/", $first_name)) {
$first_name_empty = "First name is not specified or invalid.";
}
else {
$first_name = sending($_POST["first_name"]);
$trust++;
}
if (empty($_POST["last_name"]) || !preg_match("/^[a-zA-Z ]*$/", $last_name)) {
$last_name_empty = "Last name is not specified or invalid.";
}
else {
$last_name = sending($_POST["last_name"]);
$trust++;
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$email_invalid = "E-Mail is empty or invalid.";
}
else {
$email = sending($_POST["email"]);
$trust++;
}
if (empty($_POST["subject"])) {
$subject_empty = "Unspecified subject.";
}
else {
$subject = sending($_POST["subject"]);
$trust++;
}
if (empty($_POST["message"])) {
$message_empty = "Your message is empty.";
}
else {
$message = sending($_POST["message"]);
$trust++;
}
if ($trust == 5) {
mail($recipient, $subject, "From: " . $email . "
" . $message);
}
else {
$trust = 0;
}
}
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
<h1>Contact Me</h1>
<input name="first_name" placeholder="First Name" type="name" />
<input name="last_name" placeholder="Last Name" type="name" />
<input name="email" placeholder="E-Mail" type="email" />
<input name="subject" placeholder="Subject" type="text" />
<textarea cols="50" name="message" rows="12"></textarea>
<input id="send" name="send" type="submit" value="Send">
</form>
Also, kind of out of context, is my code vulnerable to SQL injection? How can I detect that?
</div>