0

I am trying to send email from a xampp server with below php code. Php code seems to work fine as it gives all success and error messages but I am not recieving any mails on email I provide in form.

<?php
if(isset($_POST['submit'])){
$email  = $_POST['email'];
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {// Validate email address
    $message =  "Invalid email address please type a valid email!";
}
else
{
    $query = $con->prepare("SELECT username FROM members where email=:email");
    $query->execute(array(':email'=>$email));
    $row = $query->fetch(PDO::FETCH_ASSOC);

    if($query->rowCount() == 1) {
        $encrypt = md5(1290*3+$row['username']);
        $message = "Your password reset link has been send to your e-mail address. Check your mail and click on the link therein.";
        $to=$email;
        $subject="Password Recovery";
        $from = 'mydemo.com';
        $body='Hi, <br/> <br/>Your username is '.$row['username'].' <br><br>Click here to reset your password http://localhost/mydemo/reset.php?encrypt='.$encrypt.'   <br/> <br/>--<br>';
        $headers = "From: " . strip_tags($from) . "\r\n";
        $headers .= "Reply-To: ". strip_tags($from) . "\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

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

    }
    else
    {
        $message = "Account not found please enter email you provided during sign up!";
    }
    }
    }

<form action="" method="POST">
<legend>Forgot your password!</legend>
<input type="email" name="email" id="email" required="required" maxlength="35" placeholder="Enter your email here..."/>
<input type="submit" name="submit" id="submit" value="Submit"/>
</form>
<div id="message"><?php echo $message; ?></div>

I have also made following changes to C:\xampp\php\php.ini and c:\xampp\sendmail\sendmail.ini actually I had two php.ini development and production. I made changes to php-ini-development

SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = gmailid@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

[sendmail]

smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=gmailid@gmail.com
auth_password=gmailpassword
force_sender=my-gmailid@gmail.com
bɪˈɡɪnə
  • 1,087
  • 2
  • 23
  • 46
  • Have you checked in your email if it's in the spam folder? (emails sent from the php `mail()` function often get marked as spam - if you want to avoid this, check out SendGrid or Mailgun) – morganbaz Oct 25 '15 at 06:56
  • take a look at this post http://php.codeindepth.com/php-sending-mail/ – Cody Oct 26 '15 at 18:13

2 Answers2

1
if (!mail($to,$subject,$body,$headers)) {
   /*Here you can log error*/
   print_r(error_get_last());
}
Wizard
  • 10,985
  • 38
  • 91
  • 165
0

You don't know if it works fine because you don't check result of function mail(). Add one if

if (false === mail($to,$subject,$body,$headers)) {
   $message = "Mail was not sent";
}

From manual:

Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.

Community
  • 1
  • 1
Robert
  • 19,800
  • 5
  • 55
  • 85