简体   繁体   中英

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;            
}

If I got this correctly, your login action will always return true (of course, assuming the credentials match and everything else is correct) regardless of the user selecting or not the "Email Notification" option. It would cause the method $user_login->send_mail($email,$message,$subject); to be executed upon every successful login.

But your problem seem to be related to the fact that the e-mails are not hitting the inboxes of the recipients, right? Can you paste your send_mail() method? You can add debugs and breakpoints to make sure the flow is entering this desired method, and what data are being input.

I would also take a look at the configuration of your mail transfer agent on the server (postfix, sendmail etc.), assuming that the e-mails are not using any external SMTP service or anything like that.

在下面致电之前,您无处声明/初始化$email

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

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);
    }
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM