1

Email is not sending to Gmail account forgot_pass_identity is set on the table. Can anyone tell me what I am doing wrong? I check the code I did not find any mistake.

if(isset($_POST['forgotSubmit'])){
//check whether email is empty
    if(!empty($_POST['email'])){
    //check whether user exists in the database
    $prevCon['where'] = array('email'=>$_POST['email']);
    $prevCon['return_type'] = 'count';
    $prevUser = $user->getRows($prevCon);
    if($prevUser > 0){
        //generat unique string
        $uniqidStr = md5(uniqid(mt_rand()));;

        //update data with forgot pass code
        $conditions = array(
            'email' => $_POST['email']
        );
        $data = array(
            'forgot_pass_identity' => $uniqidStr
        );
        $update = $user->update($data, $conditions);

        if($update){
            $resetPassLink = 'http://example.com/resetPassword.php?fp_code='.$uniqidStr;

            //get user details
            $con['where'] = array('email'=>$_POST['email']);
            $con['return_type'] = 'single';
            $userDetails = $user->getRows($con);

            //send reset password email
            $to = $userDetails['email'];
            $subject = "Password Update Request";
            $mailContent = 'Dear '.$userDetails['first_name'].', 
            <br/>Recently a request was submitted to reset a password for your account. If this was a mistake, just ignore this email and nothing will happen.
            <br/>To reset your password, visit the following link: <a href="'.$resetPassLink.'">'.$resetPassLink.'</a>
            <br/><br/>Regards,
            <br/>mysite';
            //set content-type header for sending HTML email
            $headers .= "Content-type:text/html;charset=UTF-8" ;
            //additional headers
            //send email
            mail($to,$subject,$mailContent,$headers);

            $sessData['status']['type'] = 'success';
            $sessData['status']['msg'] = 'Please check your e-mail, we have sent a password reset link to your registered email.';
        }else{
            $sessData['status']['type'] = 'error';
            $sessData['status']['msg'] = 'Some problem occurred, please try again.';
        }
    }else{
        $sessData['status']['type'] = 'error';
        $sessData['status']['msg'] = 'Given email is not associated with any account.'; 
    }

}else{
    $sessData['status']['type'] = 'error';
    $sessData['status']['msg'] = 'Enter email to create a new password for your account.'; 
}
//store reset password status into the session
$_SESSION['sessData'] = $sessData;
//redirect to the forgot pasword page
header("Location:forgotPassword.php");
}

Please help me how to resolve this issue. Thanks in advance :)

kunal
  • 4,122
  • 12
  • 40
  • 75
Piyu
  • 43
  • 2
  • 8
  • You might be sending the mail, and Gmail could be receiving it, but then it trashes it as spam. Your mail probably doesn't have a SPF record or DKIM. See: https://support.google.com/mail/answer/6227174?hl=en&ref_topic=6259779 – KIKO Software Dec 25 '17 at 08:52

1 Answers1

1

Try adding following headers first.

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: '.$from. "\r\n"; // Valid from address

Sending mail not only is linked to code, but actually depends on other parameters as well.

Do refer below links if this does not work.

PHP mail function doesn't complete sending of e-mail

http://form.guide/email-form/php-script-not-sending-email.html

Hope this helps !

shubham nigam
  • 94
  • 1
  • 8