Creating Custom WHMCS Email Fields

WHMCS provides the EmailPreSend hook for appending custom variables to be used when generating an email.

You wil first create a new file for storing your custom hook. The hook examines the relid provided and then performs a query to pull the project records that are available for view by the client.

# /includes/hooks/custom_email.php
function CustomEmail_EmailPreSend( $vars ){
  if ($vars['relid']) {

    // Initialize if necessary
    defined('DS') or define('DS', DIRECTORY_SEPARATOR );

    // Stage
    $db          = wbDatabase::getInstance();
    $nowTime     = wbTeamPro_Common::getTime();
    $clientId    = $db->getEscaped($vars['relid']);
    $projectList = array();

    // Query
      SELECT `project`.*
        , (SELECT SUM(`timelog_elapsed`) FROM `tbladdon_wbteampro_timelog` WHERE `project_id` = `project`.`project_id`) AS `project_timelog_elapsed`
      FROM `tbladdon_wbteampro_project` AS `project`
      WHERE `project`.`client_userid` = '{$clientId}'
        AND `project`.`client_visible` = 'visible'
        AND `project`.`project_function` = 'default'
      ORDER BY `date_activity` DESC
    $projectList = $db->getRows();

    // Append Results
    foreach ($projectList AS &$project) {
      $project['project_timelog_elapsed_report'] = wbTeamPro_Common::timeElapsed( $nowTime - (int)$project['project_timelog_elapsed'] );

    // Return Merge Fields
    return array(
      'wbteampro_projectlist' => $projectList


Then you will create or modify an email template to contain something like the following.

{if $wbteampro_projectlist}
  {foreach from=$wbteampro_projectlist item=project}
    Project {$project.project_id}: {$project.project_name}
    Time Elapsed: {$project.project_timelog_elapsed_report}