This one's been around for a while, just getting it updated to v8 today.
This hook will fire off an email notification to your clients when they login to your website
A few caveats to this
This hook will fire off an email notification to your clients when they login to your website
A few caveats to this
- This will not notify your clients if using the admin login functionality
- This will not notify 'accounts' (those created by clients directly), YET! I may add this down the road
- This will not notify the client (the creator of accounts) if a sub account logged in. Why? Well, what happens if you have two clients with the same account owner?
PHP:
<?php
/*
Client area login notifications for WHMCS (works with versions 6-8)
Created by whmcsguru
Contributions by brian!
*/
use WHMCS\Database\Capsule;
$myver = get_whmcs_version();
$isadmin = $_SESSION['adminid'];
if (!empty($isadmin))
{
//go no farther, get out of here.. No login notifications necessary
return;
}
function hook_client_login_notify($vars)
{
global $myver;
$myver = get_whmcs_version();
if ($myver < 8)
{
$userid = $vars['userid'];
send_login_notify($userid);
}
if ($myver >= 8)
{
$user = $vars['user'];
$userid = $user->id;
//a few paths we can take from here, handle it
//are we a client, or contact?
$real_owner_id = Capsule::table('tblusers_clients')
->where('auth_user_id', '=', $userid)
->where('owner', '=', 1)
->value('client_id');
if ($real_owner_id > 0)
{
send_login_notify($real_owner_id);
//nothing more to see here
return;
}
}
}
function send_login_notify($userid)
{
$ip = $_SERVER['REMOTE_ADDR'] ;
$hostname = gethostbyaddr($ip);
$userinfo = Capsule::table('tblusers')->select('first_name', 'last_name')->WHERE('id', $userid)->get();
//greet them
foreach ($userinfo as $userrow)
{
$firstname = $userrow->first_name;
$lastname = $userrow->last_name;
}
$command = "sendemail";
$values["customtype"] = "general";
$values["customsubject"] = "Account Login from $hostname";
$values["custommessage"] = "<p>Hello $firstname $lastname,<p>Your account was recently successfully accessed by a remote user. If this was not you, please do contact us immediately<p>IP Address: $ip<br/>Hostname: $hostname<br />";
$values["id"] = $userid;
$results = localAPI($command, $values);
}
function get_whmcs_version()
{
$theversion = Capsule::table('tblconfiguration')->where('setting', '=', 'Version')->value('value');
$retver = substr($theversion, 0,1);
return ($retver);
}
if ($myver < 8)
{
add_hook('ClientLogin', 1, 'hook_client_login_notify');
}
if ($myver >= 8)
{
add_hook('UserLogin', 1, 'hook_client_login_notify');
}