0

I am trying to send an email using PHP. I have the code below:

$action=$_REQUEST['action'];
   if ($action=="")    /* display the contact form */
   {
   ?>
   <form  action="" method="POST" enctype="multipart/form-data">
   <input type="hidden" name="action" value="submit">
   Your name:<br>
   <input name="name" type="text" value="" size="30"/><br>
   Your email:<br>
   <input name="email" type="text" value="" size="30"/><br>
   Your message:<br>
   <textarea name="message" rows="7" cols="30"></textarea><br>
  <input type="submit" value="Send email"/>
  </form>
  <?php
  } 

else                /* send the submitted data */
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($message==""))
    {
    echo "All fields are required, please fill <a href=\"\">the form</a> again.";
    }
else{       
    $from="From: $name<$email>\r\nReturn-path: $email";
    $subject="Message sent using your contact form";
    mail("testing@gmail.com", $subject, $message, $from);
    echo "Email sent!";
    }
}  

I filled up the missing fields and clicked the submit button. The page displays that the email is sent, but when I checked my email there was no email received. What is wrong with my code?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
qazzu
  • 361
  • 3
  • 5
  • 16

3 Answers3

1

First of all check to your mail code is working or not

try like this.

if(mail("testing@gmail.com", $subject, $message, $from)){
    echo "success";
}else{
    echo "fail";
}

and check what message are come from this condition.

Nikhil Vaghela
  • 2,088
  • 2
  • 15
  • 30
1

try this working fine..!

<form  action="" method="POST" enctype="multipart/form-data">
   <input type="hidden" name="action" value="submit">
   Your name:<br>
   <input name="name" type="text" value="" size="30"/><br>
   Your email:<br>
   <input name="email" type="text" value="" size="30"/><br>
   Your message:<br>
   <textarea name="message" rows="7" cols="30"></textarea><br>
  <input type="submit" name="submit" value="Send email"/>
  </form>
  <?php
if(isset($_POST["submit"])){
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($message==""))
    {
    echo "All fields are required, please fill <a href=\"\">the form</a> again.";
    }
else{       
    $from="From: $name<$email>\r\nReturn-path: $email";
    $subject="Message sent using your contact form";
    mail("testing@gmail.com", $subject, $message, $from);
    echo "Email sent!";
    }
}
1

The first thing is, you should check the result of mail function, as it could failed and return a false to tell you that,

http://php.net/manual/en/function.mail.php#refsect1-function.mail-returnvalues

So you need to:

if (mail("testing@gmail.com", $subject, $message, $from)) {
    echo "Email sent!";
} else {
    echo "Can't send the email!";
}

If it display Can't send the email, check the log file, make sure:

  • Everything is properly configured in PHP & site setting.
  • You can connect the mail server from your host.

But what if the it display Email sent!, while you still can't receive the email?

Well, the mail function will return true when:

the mail was successfully accepted for delivery

Notice the word *accepted* for delivery, which means: Ok, it's on the mail server now, my part of job done. It's still uncertain if the mail will reach to the user.

You see, Email system is complicated. The email you just "sent" may go through multiple servers to reach it's destination. Every each server in the middle could delay or even cancel the transportation.

Even worse, your user or user's Email Provider may filtered 'n' dropped your email. For security measure, they let you know nothing about it.

So you could never actually know if your email is reached your user even if the mail function or the PHPMailer is returning true result. Only thing you can make sure is, the mail is delivered to a capable mail server.

In this case:

NICK
  • 96
  • 7