I have been trying to make this contact form work but couldn't receive emails. Everything looks fine to me. I am using a html template and trying to customize it with my limited coding knowledge. Any help is appreciated.
Here is my html code:
<form class="form-horizontal" id="contactForm" action="contact.php" method="post">
<!-- Notifications -->
<p class="success cf text-center"><i class="fa fa-check"></i> <strong>Mesajınız Gönderilmiştir.</strong></p>
<p class="failed cf text-center"><i class="fa fa-exclamation-circle"></i><strong> Bir hata Oluştu! Lütfen Tekrar Deneyiniz.</strong></p>
<div class="form-group w-50">
<input type="text" class="form-control" id="cfName" placeholder="İsim" required="">
<input type="email" class="form-control" id="cfEmail" placeholder="E-Mail" required="">
</div>
<div class="form-group">
<input type="text" class="form-control" id="cfSubject" placeholder="Başlık" required="">
</div>
<div class="form-group">
<textarea id="cfMessage" rows="5" class="form-control" placeholder="Mesajınız..." required=""></textarea>
</div>
<div class="form-group m-b-0">
<button type="submit" class="btn btn-shadow btn-green" style="border-radius: 4px; font-size: 15px; box-shadow: rgb(72, 125, 101) 0px 0px 0px 0px; border: 3px solid rgb(219, 141, 23); background-color: rgb(219, 141, 23);">Gönder</button>
</div>
</form>
and this is my php code:
<?php
// Variables
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$subject = trim($_POST['subject']);
$message = trim($_POST['message']);
// Email address validation - works with php 5.2+
function is_email_valid($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
if( isset($name) && isset($email) && isset($message) && isset($subject) && is_email_valid($email) ) {
// Avoid Email Injection and Mail Form Script Hijacking
$pattern = "/(content-type|bcc:|cc:|to:)/i";
if( preg_match($pattern, $name) || preg_match($pattern, $email) || preg_match($pattern, $message) || preg_match($pattern, $subject) ) {
exit;
}
// Email will be send
$to = "arda@toptal.com"; // Change with your email address
// HTML Elements for Email Body
$body = <<<EOD
<strong>Name:</strong> $name <br>
<strong>Email:</strong> <a href="mailto:$email?subject=feedback" "email me">$email</a> <br> <br>
<strong>Message:</strong> $message <br>
EOD;
//Must end on first column
$headers = "From: $name <$email>\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// PHP email sender
mail($to, $subject, $body, $headers);
}
?>
Thanks.