Chirag Jain Betala Chirag Jain Betala - 23 days ago 7
MySQL Question

Email Notification to users on Loggin in PHP

I am trying to code program which will notify users when they are logged in through PHP & MySQL.

I tried but it did not email login when the user logged in!

Email Notification is selected by Users. If users select yes, then only it should notify them.

And the user from which it is being tested is set to

yes


I tried:

signin.php

<?php
session_start();
require_once 'class.user.php';
$user_login = new USER();

if($user_login->is_logged_in()!="")
{
$user_login->redirect($web.$_SESSION['user_name']);
}

if(isset($_POST['btn-login']))
{
$uname = trim($_POST['txtuname']);
$upass = trim($_POST['txtupass']);

if($user_login->login($uname,$upass))
{

$message = "
Hello $uname,<br />
<p>You are Logged in!

<br /><br />
Thanks:)";

$subject = "Notifier";

$user_login->send_mail($email,$message,$subject);

$user_login->redirect($uname);
}
}
?>


class.user.php

public function login($uname,$upass)
{
try
{
$stmt = $this->conn->prepare("SELECT * FROM tbl_users WHERE userName=:username");
$stmt->execute(array(":username"=>$uname));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);

if($stmt->rowCount() == 1)
{
if($userRow['userStatus']=="Y")
{
if($userRow['userAccess']=="Y")
{
if($userRow['userPass']==md5($upass))
{
if($userRow['userNotify']=="Y")
{
return true;
}
$_SESSION['userSession'] = $userRow['userID'];
$_SESSION['loggedin_time'] = time();
$_SESSION['user_name'] = $userRow['userName'];
return true;
}

else
{
header("Location: signin.php?error");
exit;
}
}
else
{
header("Location: default.php");
exit;
}
}
else
{
header("Location: inactive.php");
exit;
}
}
else
{
header("Location: signin.php?error");
exit;
}
}
catch(PDOException $ex)
{
echo $ex->getMessage();
}
}

function send_mail($email,$message,$subject)
{
require_once('mailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->AddAddress($email);
$mail->Username="mail@gmail.com";
$mail->Password="password";
$mail->SetFrom('mail@gmail.com','Name');
$mail->AddReplyTo("mail@gmail.com","Name");
$mail->Subject = $subject;
$mail->MsgHTML($message);
$mail->Send();
}


Email Notification is selected by Users. If users select yes, then only it should notify them.

This is checked by

if($userRow['userNotify']=="Y")
{
return true;
}

Answer Source

Thanks for editing the question!

I went through your code and as others said, you have not defined $email and also if you define, your code then also will not work as

if($userRow['userNotify']=="Y")
                    {       
    return true;            
                }

will only check it will not do anything.

So as per your codes, I hope my codes will help you to sort out from all problems.

Keeping email function unchangeable in class.user.php my edit codes goes:

class.user.php

public function login($uname,$upass)
    {
        try
        {
            $stmt = $this->conn->prepare("SELECT * FROM tbl_users WHERE userName=:username");
            $stmt->execute(array(":username"=>$uname));
            $userRow=$stmt->fetch(PDO::FETCH_ASSOC);

            if($stmt->rowCount() == 1)
            {
                if($userRow['userStatus']=="Y")
                {
                    if($userRow['userAccess']=="Y")
                {
                    if($userRow['userPass']==md5($upass))
                    {
                     if($userRow['userNotify']=="Y")
                {
$email = $userRow['userEmail'];
$message = "Hello $uname,<br />
                        <p>You are Logged in!

                        <br /><br />
                        Thanks:)";

            $subject = "Notifier";

            require_once('mailer/class.phpmailer.php');
        $mail = new PHPMailer();
        $mail->IsSMTP(); 
        $mail->SMTPDebug  = 0;                     
        $mail->SMTPAuth   = true;                  
        $mail->SMTPSecure = "ssl";                 
        $mail->Host       = "smtp.gmail.com";      
        $mail->Port       = 465;             
        $mail->AddAddress($email);
        $mail->Username="mail@gmail.com";  
        $mail->Password="password";            
        $mail->SetFrom('mail@gmail.com','Name');
        $mail->AddReplyTo("mail@gmail.com","Name");
        $mail->Subject    = $subject;
        $mail->MsgHTML($message);
        $mail->Send();
            return true;            
            }
                        $_SESSION['userSession'] = $userRow['userID'];
                        $_SESSION['loggedin_time'] = time();
                        $_SESSION['user_name'] = $userRow['userName'];
                        return true;
                    }

                    else
                    {
                        header("Location: signin.php?error");
                        exit;
                    }
                }
                else
                {
                    header("Location: default.php");
                    exit;
                }   
            }
                else
                {
                    header("Location: inactive.php");
                    exit;
                }   
            }
            else
            {
                header("Location: signin.php?error");
                exit;
            }       
        }
        catch(PDOException $ex)
        {
            echo $ex->getMessage();
        }
    }

signin.php

<?php
session_start();
require_once 'class.user.php';
$user_login = new USER();

if($user_login->is_logged_in()!="")
{
    $user_login->redirect($web.$_SESSION['user_name']);
}

if(isset($_POST['btn-login']))
{
    $uname = trim($_POST['txtuname']);
    $upass = trim($_POST['txtupass']);

    if($user_login->login($uname,$upass))
    {
        $user_login->redirect($uname);
    }
}
?>