Email contact form not sending

The “contact us” form that i’m using is not working. My website is hosted on hostinger. The MX records have been changed correctly. I contacted their support and everything is good on their end apparently. Hostinger’s platform even says there were 6 emails sent out (from me testing). The code seems to be working but supposedly the only issue can be due to the code. Been trying to fix this for hours and I have no clue what could be wrong.

It seems to all be sending correctly but I never receive anything in my email or spam folder.

Heres the PHP


$fname = trim($_POST['fname']);
$email = trim($_POST['email']);
$_message = trim($_POST['message']);

if ($fname == "") {
    $msg['err'] = "\n First name can not be empty!";
    $msg['field'] = "fname";
    $msg['code'] = FALSE;
} else if ($email == "") {
    $msg['err'] = "\n Email can not be empty!";
    $msg['field'] = "Email";
    $msg['field'] = "email";
    $msg['code'] = FALSE;
} else if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
    $msg['err'] = "\n Please put a valid email address!";
    $msg['field'] = "email";
    $msg['code'] = FALSE;

} else if ($_message == "") {
    $msg['err'] = "\n Message can not be empty!";
    $msg['field'] = "Message";
    $msg['field'] = "message";
    $msg['code'] = FALSE;
} else {
    $to = '[email protected]';
    $subject = 'Cynic Trendy - Contact Request';
    $message = '<html><head></head><body>';
    $message .= '<p>First Name: ' . $fname . '</p>';
    $message .= '<p>Email: ' . $email . '</p>';
    $message .= '<p>Message: ' . $_message . '</p>';
    $message .= '</body></html>';
    $headers = 'MIME-Version: 3.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'From:  '.$fname.' <'.$email.'>' . "\r\n";
    $headers .= 'cc: [email protected]' . "\r\n";
    $headers .= 'bcc: [email protected]' . "\r\n";

    mail($to, $subject, $message, $headers);

    $msg['success'] = "\n Email has been sent successfully.";
    $msg['code'] = TRUE;

echo json_encode($msg);

If your host is reporting that emails WERE in fact sent out… then I wouldnt think this is a FORM issue?

Perhaps more of a header/spam thing? Where your email or (other header info) is being flagged as spam perhaps?

Try using the hard coded DOMAIN email address for the FROM param perhaps? See if its tripping up there maybe?

meaning like set the FROM parameter to [email protected] ?

No. These emails are being sent from the mail server at your web hosting. The domain in the From: mail header must correspond to your sending mail server. You CAN put the email address submitted from the form, after validating it, into a Reply-to: mail header.

For your testing, the receiving mail server is receiving an email that states it is From itself, but the ip address it is being sent from corresponds to the mail server at your web hosting. Since this is impossible, the emails are probably being directly discarded upon receipt. In fact, continuing to do this will get your sending mail server black-listed at the receiving mail server so that even if you correct the from address, you may need to contact the administrators (post master) at the receiving mail server to get this blocking removed.

An alternative is to use smtp authentication (which the php mail() function does not support), authenticate against the To: mail box, and send the emails directly to that mail box from your php script. To do this, use one of the php mailer classes - phpmailer or swiftmailer

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service