2

So this is my code for sending an email. When I try to send an email i get right message, in this case "User Exist" and "Email is sent", but i can't receive an email. So can somebody find mistake and try to help me?

<form action="forgot2.php" method="post">
Enter you email ID: <input type="text" name="email">
<input type="submit" name="submit" value="Send">
</form>
error_reporting(0);
$email=$_POST["email"];
$Subject = "GOSPodin covek";

if($_POST['submit']=='Send')
{
    mysql_connect('localhost','root','') or die(mysql_error);
    mysql_select_db('login');
    $query="select * from clanovi where email='$email'";
    $result=mysql_query($query) or die(error);
    if(mysql_num_rows($result))
    {
        echo "User exist"; 
    }
    else
    { 
        echo "No user exist with this email id";
    }
}
if(mysql_num_rows($result))
{
    $code=rand(100,999);
    $message="You activation link is: http://localhost/login_form/forgot.php?          email=$email&code=$code";
    mail($email, "Subject Goes Here", $message);
    echo "Email sent"; 
}
else
{
    echo "No user exist with this email id";
}
BrotherSrbin
  • 43
  • 2
  • 9
  • 1
    While not the answer to your question, please read about sql injections, because your code allows them: https://en.wikibooks.org/wiki/PHP_Programming/SQL_Injection_Attacks and https://xkcd.com/327/ – Tilman Hausherr Jun 27 '15 at 17:26
  • You do realize that, outside the SQL injection issue, the code is highly hackable? `$code=rand(100,999);` makes the recovery URL in no way unique or linked to the user. Generate a unique recovery hash and store it in a column in your user table instead. – Markus AO Jun 27 '15 at 17:31
  • 1
    /* Watch this space for comments about not using the deprecated `mysql_*` extension and using `mysqli_*` instead. */ – Markus AO Jun 27 '15 at 17:33

5 Answers5

1

First of all, you need to check if the mail is actually sent:

if (mail($email, "Subject Goes Here", $message)) {
    // then it's actually sent.
}

If mail() doesn't return false, then the mail was passed to your local mail server for delivery. Presume you already have a mail server set up on your local test box? If not, problem is obvious...

If PHP checks out OK and you still don't receive the mail (and it's not in your spam folders etc.), you need to investigate your server's mail logs for possible issues outside PHP.

...and for the love of code and all things sacred to debugging, do NOT use error_reporting(0); if you're actually trying to figure out what's wrong! (Error messages are kinda helpful for that.)

Markus AO
  • 3,587
  • 1
  • 12
  • 21
0

Are you sure your script is actually sending the mail?

Replace:

mail($email, "Subject Goes Here", $message);
echo "Email sent"; 

By:

if(@mail($email, "Subject Goes Here", $message)) {
  echo "Email sent";
} else {
  echo "Email not sent";
}
Eduardo Escobar
  • 2,881
  • 1
  • 16
  • 14
0

Just try

I use this works fine for me

$headers = 'From: support@example.com';
mail($payer_email, 'Tokens', "Thank you for purchase tokens $additional-message", $headers);
Deepak Kumar
  • 413
  • 1
  • 4
  • 11
0

Assuming mail() does return true and that the email is a valid one, you want to make you have a valid smtp server configured in php.ini in the [mail function] section.

mcorne
  • 77
  • 2
0

change this one.

$query="select * from clanovi where email='".$email."'";
Cyclonecode
  • 26,179
  • 11
  • 66
  • 86
GULIM SHAH
  • 184
  • 1
  • 8
  • 1
    What difference will that make? PHP is already going to interpolate the variable into the string, as it's using double quotes – andrewsi Jun 27 '15 at 17:39