Jump to content

Send email to client upon login


Patty

Recommended Posts

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");

?>

Link to comment
Share on other sites

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");

?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by Patty
Link to comment
Share on other sites

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");

Link to comment
Share on other sites

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");

?>

Link to comment
Share on other sites

  • 1 month later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated