Jump to content

Recommended Posts

  • WHMCS Technical Analyst

WHMCS users and client accounts have separate contact information and they require separate updates to change details like the email addresses. The  user management system allows a single user to access multiple client accounts. This separates authentication and authorization from services, billing, and support.

To learn how to change the client account and the user account email address so they match please review this help guide:
https://help.whmcs.com/m/managing/l/1681243-updating-user-and-client-account-details

With this hook added to your WHMCS installation the system will now "Sync" the Client Account Email Address to match the User Account Email Address only when the change is made under the Account Details page via the client area.

This hook adds a little "Note" under the Email Address field under the Hello Client! > Account Details section of the client area to inform that when this Email Address is changed, they will be logged out and they will need to log back in using the new Email Address they just set since this hook is updating both the client account and user account email addresses.

Here is an example of the Account Details page Email Address field with this note:
Screenshot2024-04-13at11_11_40PM.png.9ff96235f3d8ad85d45bf963c3e7a480.png
Via the Admin Area, when a client does this change and the hook was used it will make a log entry just like this:

Quote

Client/User Email Sync Script - Emails Successfully Changed and Synced. The e-mail address is set to dannydip@v89.test.whmcs.rocks for the Client Account and the Owners User ID: 4

This entry indicates that the Client/User Email Sync Script hook was a success.
Now both the Client Account Email Address and the User Account Email Address match for that client account.

If there are multiple Users associated with the Client Account, this will only change the Email Address of the Owner of the account.

 

<?php
/*
This script will update both the Client Account Profile email address and the user account email address when the change is made to the Account Details page for the email address field.

Otherwise, you would have to update the email in both places and follow this article: https://help.whmcs.com/m/managing/l/1681243-updating-user-and-client-account-details

Upload this file to your /includes/hooks directory of your WHMCS installation.

There will be a Log entry when this script runs.

@WHMCSDanny
*/


add_hook('ClientAreaHeadOutput', 1, function($vars) {
    
	// Only run if the on the Account Details page via the client area.
	// The action is going to be "details". This will make sure this message does not show anywhere else.

    $action = $_GET['action'];
    if($action == "details") {

	//Input the message under the Email Address field that they will be logged out after making the change.
    return <<<HTML
    <script type="text/javascript">
        $(document).ready(function() {
            jQuery("input[name='email']").after('<span style="color:red; font-size:9pt;"><b>Note:</b> Changing your email address here will sync the email with your User Account. You will be logged out after you change the email. You must login using the new email address you just set.</span>');
        });

    </script>

HTML;
	};

});


// prevent file from being loaded directly
if (!defined("WHMCS")) {
	die("This file cannot be accessed directly.");
}
else {
	
	function clientowneruseremailsync_changeUserEmail(int $client_id, string $client_email){
		// call the API and grab the owner user ID
		$command = 'GetClientsDetails';
		$postData = array(
			'clientid' => $client_id,
			'stats' => false,
		);

		$results = localAPI($command, $postData);
		
		if ($results['result'] == 'success') {
			// success!
			$client_owner_user_id = $results['client']['owner_user_id'];
			
			if (is_numeric($client_owner_user_id)){
				// got a number, so it should be a valid owner user ID
				
				// now to perform the update to the user account to match the email set for the client account
				$command = 'UpdateUser';
				$postData = array(
					'user_id' => $client_owner_user_id,
					'email' => $client_email,
				);
				
				$results = localAPI($command, $postData);
				
				if ($results['result'] == 'success') {
					logActivity("Client/User Email Sync Script - Emails Successfully Changed and Synced. The e-mail address is set to $client_email for the Client Account and the Owners User ID: $client_owner_user_id", $client_id);
				}
				else {
					logActivity("Client/User Email Sync Script - Failed to change the e-mail address to $client_email for the Owners User ID: $client_owner_user_id . Results: ". $results, $client_id);
				}
			}
		}
		else {
			logActivity("Client/User Email Sync Script - Failed to verify that an e-mail change occurred on the clients profile. Results: ". $results, $client_id);	
		}
	}
	
	add_hook('ClientEdit', 1, function($vars) {
		// Only run if the clients account profile email address is being changed.

		if ($vars['email'] != $vars['olddata']['email']){
			// email is being changed. Update owning user accordingly.

			// get the client ID. It should be $vars['userid']
			$client_id = $vars['userid'];
			
			// get the new e-mail address
			$client_email = $vars['email'];

			// call our helper function
			clientowneruseremailsync_changeUserEmail($client_id, $client_email);
		}
	});
	

	
}
?>



Enhanced Version - Added a checkbox and tooltip

In this new updated version of this hook, I added a checkbox/tooltip for the end-users to decide if they want to use this option to sync the Email Address under the Profile page too. Otherwise, nothing happens and WHMCS works as normal.

The checkbox needs to be checked before it will run the same hook code to update both email addresses in both locations. (Account Details and Profile sections via the client area)
 

Screenshot2024-04-15at12_01_36AM.png.4d6e70bb231e81f73a2fe6e4eef02768.png

 

<?php
/*
This hook script will update both the Client Account Profile email address and the User Account email address
When the change is made to the Account Details page for the email address field only. It does not work for the 
Profile page.

This version adds a new checkbox with a tooltip to let the end-user decide if they want to use this option or not.
The checkbox needs to be checked for the hook to execute.
If the checkbox does not get checked WHMCS works as expected and updates just the Account email.
The Profile email account will still need to be updated if they want it to be the same.

Otherwise, you would have to update the email in both places and follow this article: 
https://help.whmcs.com/m/managing/l/1681243-updating-user-and-client-account-details

Upload this file to your /includes/hooks directory of your WHMCS installation.

There will be a Log entry in the admin area when this script executes.

@WHMCSDanny

*/


add_hook('ClientAreaHeadOutput', 1, function($vars) {
    
	// Only run if the on the Account Details page via the client area.
	// The page action is "details". This will make sure this message does not show anywhere else.

    $action = $_GET['action'];
    if($action == "details") {

	//Input the checkbox and tooltip under the Email Address field
    return <<<HTML
    <script type="text/javascript">
        $(document).ready(function() {
            jQuery("input[name='email']").after('<input type="checkbox" name="syncEmails" id="syncEmails"> <span style="color:red; font-size:9pt;"><b>Sync Email with your User Account Email</b></span><span class="form-group"> &nbsp; <i class="far fa-question-circle" data-toggle="tooltip" data-placement="top" title="This option will sync your Email Address Here with your Profile Email Address. You will be logged out and will need to login with your new email address. If you do not check this option you will need to update it under the Your Profile page as well"></i></span>');
        });

    </script>

HTML;
	};

});


// Prevent file from being loaded directly
if (!defined("WHMCS")) {
	die("This file cannot be accessed directly.");
}
else {
    
if (isset($_POST['syncEmails'])) {
   // Checkbox is checked
   // Perform actions and the logic to check the emails and replace them with the new one

	function clientowneruseremailsync_changeUserEmail(int $client_id, string $client_email){
		// call the API and grab the owner user ID
		$command = 'GetClientsDetails';
		$postData = array(
			'clientid' => $client_id,
			'stats' => false,
		);

		$results = localAPI($command, $postData);
		
		if ($results['result'] == 'success') {
			// Success we have the owners user ID from the database!
			$client_owner_user_id = $results['client']['owner_user_id'];
			
			if (is_numeric($client_owner_user_id)){
				// We have the ID number, so it should be a valid owner user ID
				
				// Perform the update to the user account to match the email set for the client account
				$command = 'UpdateUser';
				$postData = array(
					'user_id' => $client_owner_user_id,
					'email' => $client_email,
				);
				
				$results = localAPI($command, $postData);
				
				if ($results['result'] == 'success') {
					logActivity("Client/User Email Sync Script - Emails Successfully Changed and Synced. The e-mail address is set to $client_email for the Client Account and the Owners User ID: $client_owner_user_id", $client_id);
				}
				else {
					logActivity("Client/User Email Sync Script - Failed to change the e-mail address to $client_email for the Owners User ID: $client_owner_user_id . Results: ". $results, $client_id);
				}
			}
		}
		else {
			logActivity("Client/User Email Sync Script - Failed to verify that an e-mail change occurred on the clients profile. Results: ". $results, $client_id);	
		}
	}
	
	add_hook('ClientEdit', 1, function($vars) {
		// Only run if the clients account detaoils email address field is being changed.

		if ($vars['email'] != $vars['olddata']['email']){
			// Wmail is being changed. 
			// Get the client ID. It should be $vars['userid']
			$client_id = $vars['userid'];
			
			// Get the new e-mail address
			$client_email = $vars['email'];

			// Call the helper function to make the change
			clientowneruseremailsync_changeUserEmail($client_id, $client_email);
		}
	});
	
}
	
}
?>


 

At the time of writing this post, this process was tested on the latest stable release of WHMCS 8.9.0

I hope you find this useful. If you have any feedback or questions, please feel free to reply to this thread!


WHMCSDanny

Link to comment
Share on other sites

  • WHMCS John changed the title to Sync Client Account Email with User Account Email

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.

×
×
  • 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