I am facing an issue with the PHP mail() function where it's not working at all. I have tried multiple MTA (Mail Transfer Agent) solutions like sendmail, postfix, and exim, but none of them seem to work for me. I have also searched extensively online for a solution, but nothing seems to work.
Here is the HTML code for the form I am using:
<!-- contact form area start -->
<div
class="col-lg-8 col-md-6 col-sm-12 m-25px-b"
data-aos="fade-right"
data-aos-duration="1000"
data-aos-delay="200"
>
<div class="contact-form-box">
<form id="contact-form" method="post" action="php/sendmail.php">
<div class="message col">
<p class="email-loading alert alert-warning">
<img
src="assets/images/loading.gif"
alt=""
/> Sending...
</p>
<p class="email-success alert alert-success">
<i class="icofont-check-circled"></i> Your quote has
successfully been sent.
</p>
<p class="email-failed alert alert-danger">
<i class="icofont-close-circled"></i> Something went
wrong!
</p>
</div>
<div class="mb13">
<input
name="name"
class="contact-name"
id="contact-name"
type="text"
placeholder="Your Name"
required=""
/>
</div>
<div class="mb13">
<input
name="email"
class="contact-email"
id="contact-email"
type="email"
placeholder="Your Email"
required=""
/>
</div>
<div class="mb13">
<input
name="subject"
class="contact-subject"
id="contact-subject"
type="text"
placeholder="Subject"
required=""
/>
</div>
<div class="mb30">
<textarea
name="message"
class="contact-message"
id="contact-message"
placeholder="Your Message"
required=""
></textarea>
</div>
<button class="button button__primary align-items-center">
<span>Send Now</span>
</button>
</form>
</div>
</div>
<!-- contact form area end -->
And here is the sendmail.php code I have used for the form:
<?php
// Variables
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$subject = trim($_POST['subject']);
$message = trim($_POST['message']);
if( isset($name) && isset($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) ) {
exit;
}
// Email will be send
$to = "testmail@gmail.com"; // email address
$sub = "$subject from test"; // email subject
// 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, $sub, $body, $headers);
}
?>
I would greatly appreciate any help or guidance in resolving this issue. Thank you in advance!