Can someone help me, my contact form does not working. It will send successfully, but the email recipient can't receive the message. Here is my code:

<?php

$email_to =   'name@example.com'; //the address to which the email will be sent
$name     =   $_POST['name'];  
$email    =   $_POST['email'];
$subject  =   $_POST['subject'];
$message  =   $_POST['message'];

$headers = 'From:' . $email;

if(mail($email_to, $subject, $message, $headers)){
    echo 'sent'; // we are sending this text to the ajax request telling it that the mail is sent..      
}else{
    echo 'failed';// ... or this one to tell it that it wasn't sent    
}

?>

Firstly, I'd suggest you edit your post to remove your real email address from the code.

https://www.php.net/manual/en/function.mail.php

Some things to try.

  1. Headers is an optional parameter, can you send without the headers parameter? Many examples I've seen suggest there should be a space after the :, as in 'From: '
  2. Sanitise your subject and/or message. You can't in general feed whatever was typed into your dialog directly into mail.

Does a very simple inline code work?
mail('my@my.address.com','Testing...','1 2 3');

Firstly, I'd suggest you edit your post to remove your real email address from the code.

I have replaced their email address with a dummy email.

Sanitise your subject and/or message. You can't in general feed whatever was typed into your dialog directly into mail.

Salem is referring to passing each of those variables in htmlspecialchars() to make sure there is not any HTML or Javascript embedded in the strings that could potentially be executed. Or, alternatively, you can use PHP's filter_var() function to sanitize user input.

WEB security now rejects mail sent from any computer. Select some mail provider and send mail using it. Using certificated WEB serve is an another solution. Obtain legal certificate, register it on WEB server (IIS, Apache, NGINX, ...)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.