Patty Posted February 24, 2015 Share Posted February 24, 2015 Hi there. I'm trying to create a hook to send an email to client when he logs in, but using the code below nothing happens. What am I doing wrong? Hope someone can help me. TIA! <?php function email_getUserID($table, $id){ $userquery = mysql_query("SELECT userid FROM $table WHERE id = '".$id."'"); $userid = mysql_result($userquery, 0); return $userid; } function email_getAdmin(){ $queryadmin = mysql_query("SELECT username FROM tbladmins WHERE roleid = 1"); $admin = mysql_result($queryadmin, 0); return $admin; } function email_sendclient($vars) { if (isset($vars['userid'])){ $id = $vars['userid']; $table = "tblclients"; $emailtpl = "news"; $command = 'SendEmail'; $adminuser = email_getAdmin(); $id = email_getUserID($table, $id); $values = array('messagename' => $emailtpl, 'customtype' => $general, 'id' => $id); localAPI($command, $values, $adminuser); } } add_hook("ClientLogin",1,"email_sendclient"); ?> 0 Quote Link to comment Share on other sites More sharing options...
Patty Posted February 24, 2015 Author Share Posted February 24, 2015 Tried this one too, but no luck. <?php function email_getAdmin(){ $queryadmin = mysql_query("SELECT username FROM tbladmins WHERE roleid = 1"); $admin = mysql_result($queryadmin, 0); return $admin; } function email_sendclient(){ $result = mysql_query("SELECT id FROM tblclients"); $emailtpl = 'news'; $command = 'SendEmail'; $adminuser = email_getAdmin(); while($data = mysql_fetch_array($result)){ $values = array('messagename' => $emailtpl, 'customtype' => $general, 'id' => $data["id"]); localAPI($command, $values, $adminuser); } } add_hook("ClientLogin",1,"email_sendclient"); ?> 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted February 24, 2015 Share Posted February 24, 2015 I see that there are some problems with your script. First of all let's start with this function. function email_getUserID($table, $id){ $userquery = mysql_query("SELECT userid FROM $table WHERE id = '".$id."'"); $userid = mysql_result($userquery, 0); return $userid; } $userid is an array and not a string. The ID of the client is in $userid["userid"]. Said that even if you return $userid["userid"] you'll get nothing since userid column doesn't exist in tblclients. Your function should be updated to: function email_getUserID($table, $id){ $userquery = mysql_query("SELECT id FROM $table WHERE id = '".$id."'"); $userid = mysql_result($userquery, 0); return $userid["id"]; } Similarly you did the same mistakes in the other function. function email_getAdmin(){ $queryadmin = mysql_query("SELECT username FROM tbladmins WHERE roleid = 1"); $admin = mysql_result($queryadmin, 0); return $admin; } $admin is still an array. You should return $admin["username"]. Anyway here the problem is more complex. You are selecting the username where roleid is equal to "1" and you are expecting just 1 row as result. What if you have more than just one admin account? Secondly it's not necessarily true that an admin with roleid = 1 has API permissions. You should look inside tbladminperms table for permid 81 (API Access permission). Alternatively you could do it in the lazy way with this: localAPI("SendEmail", $values, "yourusernamehere"); - - - Updated - - - I've seen also the other script you posted. Please don't do that No offense but the script is really bad and so damn wrong. Basically you are sending an email to ALL YOUR CLIENTS everytime one of them login into clientarea. You're sending spam. And also... $values = array('messagename' => $emailtpl, 'customtype' => $general, 'id' => $data["id"]); $general is a null variable, it doesn't exist and $emailtpl cointains just "news". Why? 0 Quote Link to comment Share on other sites More sharing options...
Patty Posted February 24, 2015 Author Share Posted February 24, 2015 (edited) Tks a lot for your reply, Kian. I'm not a coder and I'm just putting together bits and pieces of hooks I've found. I'll try to correct it based on your comments. As for 'news', it's the email template I want to send when the client logs in. And 'general' is the template category. Tks a lot for your help, I'll post the results later on. Edited February 24, 2015 by Patty 0 Quote Link to comment Share on other sites More sharing options...
Patty Posted February 24, 2015 Author Share Posted February 24, 2015 OK, the email_getAdmin function works fine in other hooks, so this shouldn't be an issue. function email_getAdmin(){ $queryadmin = mysql_query("SELECT username FROM tbladmins WHERE roleid = 1"); $admin = mysql_result($queryadmin, 0); return $admin; } I've also changed a few things in the email_sendclient function, but it's still not working. What should I change in this script to make it work? TIA for any help. function email_sendclient(){ $result = mysql_query("SELECT id FROM tblclients"); $table = "tblclients"; $emailtpl = 'news'; $command = 'SendEmail'; $adminuser = email_getAdmin(); while($data = mysql_fetch_array($result)){ $values = array('messagename' => $emailtpl, 'customtype' => 'general', 'id' => $data["id"]); localAPI($command, $values, $adminuser); } } add_hook("ClientLogin",1,"email_sendclient"); 0 Quote Link to comment Share on other sites More sharing options...
Patty Posted February 25, 2015 Author Share Posted February 25, 2015 OK, this is the last one I've tried, still no joy. Somebody help, please??? <?php function email_getUserID($table, $id){ $userquery = mysql_query("SELECT id FROM $table WHERE id = '".$id."'"); $userid = mysql_result($userquery, 0); return $userid["id"]; } function email_getAdmin(){ $queryadmin = mysql_query("SELECT username FROM tbladmins WHERE roleid = 1"); $admin = mysql_result($queryadmin, 0); return $admin; } function email_sendclient($vars){ $table = "tblclients"; $emailtpl = 'news'; $command = 'SendEmail'; $adminuser = email_getAdmin(); $id = email_getUserID($table, $id); $values = array('messagename' => $emailtpl, 'customtype' => $general, 'id' => $id); localAPI($command, $values, $adminuser); } add_hook("ClientLogin",1,"email_sendclient"); ?> 0 Quote Link to comment Share on other sites More sharing options...
Patty Posted April 21, 2015 Author Share Posted April 21, 2015 Still can't get this to work. Can somebody shed a light, please? TIA 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.