Not getting mail from mail() function in php

Hello there,
Actually i’m facing a problem regarding a contact form. I just want to receive the data send from my webviewers through mail. I have created a form and i want to send that data through mail but unfortunately it is showing mail is sent successfully but i’m not getting any mail. i have check my spam folder and did not find any mail there.
Here is my code-

<div class="hire">
  <font > <center>Contact Us</center></font><div class="line"></div>
  <form action="" method="POST">
    <table cellpadding="10">
      <tr><td>Name :</td>
      <td><input type="text" name="name" class="hirebtn" placeholder="Name"></input></td></tr>
      <tr>
        <td>Mail :</td>
        <td><input type="text" name="mail" class="hirebtn" placeholder="Mail"></input></td>
      </tr>
      <tr>
        <td>Contact No : </td>
        <td><input type="text" name="phn" class="hirebtn" placeholder="Contact No"></input></td>
      </tr>
      <tr>
        <td>Describe :</td>
        <td><textarea name="des" class="hirebtn" placeholder="Describe your Project"></textarea></td>
      </tr>
      <tr><td><input type="submit" name=submit class="btnn btnn2" value="submit"></input></td></tr>
    </table>
  </form>
  <?php

$your_email = "mymail@mail.com"; // email address to which the form data will be sent
$subject = "lex"; // subject of the email that is sent

if (isset($_POST["submit"])) {
  $nam = $_POST["name"];
  $pho = $_POST["phn"];
  $ema = trim($_POST["mail"]);
  $com = $_POST["des"];

  if (get_magic_quotes_gpc()) { 
  $nam = stripslashes($nam);
  $ema = stripslashes($ema);
  $pho = stripslashes($pho);
  $com = stripslashes($com);
  }

$email_body = 
  "Name: $nam\
\
" .
  "Email: $ema\
\
" .
  "Telephone: $pho\
\
" .
    "MESSAGE:\
\
" .
  "$com" ; 

// Assuming there's no error, send the email and redirect to Thank You page

if (isset($_REQUEST['des'])) {
mail ($your_email, $subject, $email_body, "From: $nam <$ema>" . "\\r\
" . "Reply-To: $nam <$ema>");
echo "Mail sent";
}  
else{
  echo "Not sent";
}
}
?>

please check above code and if find solution :slight_smile:

Thanks .

Does your email server allow you to send emails using the form-fillers details as the “from” address? Many will prevent that kind of thing, an easy check on that would be to just hard-code the from-address to your own to experiment. Normally you would use an email address of your own to send the email (say, “webform@yourdomain.com”) and put the users email in the Reply-to header.

Other than that, every other time the mail() function has come up on here, the general advice is to use something like PHPMailer as it works better than the built-in function. I have no personal experience of it.

1 Like

The first thing that I see that’s wrong with your code is that you’re putting all the PHP code at the bottom which isn’t a good idea. You should be putting all PHP codes that have to deal with data processing at the top of your page. The more you stash everything that’s PHP related at the bottom, the more easier you’ll start to get errors relating to header already sent message. Meaning that you outputted HTML before the header was sent, causing the header already sent error message. To avoid this, you would always want to put all your PHP if necessary at the top of your page.

The second thing, as @droopsnoot has already mentioned, the default mail() function is not reliable. It will fail at times and it will work at times. To avoid this from happening, it is best to use PHPMailer as it is well managed and does not fail. The only times it fails is if you messed up the configuration or your server cannot send emails.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.