Jump to content

Hook notification works, but unable to retrieve client/sender mail account


Recommended Posts

 I'm configuring a WHMCS "TicketOpen" hook to send a notification to our Synology Chat using a webhook. It actually works, the issue here is that we would like to include in the notification sender's mail account or who opened the ticket. We would rather just the mail account because we have several clients who send us tickets from unregistered email accounts. Our notifications look like  this when they are received:

New ticket created by: (we get nothing here, expected output:) [ mailaccount@domain.com ] or [ CLIENT_NAME CLIENT_SURNAME]
Ticket ID: (example) IHC-261848
Subject: (example) Support request!

New ticket created by:  Unknown sender (expected output:) [ account@domain.com ] or [ CLIENT_NAME CLIENT_SURNAME]
Ticket ID: (example) IHC-261848
Subject: (example) Support request!
Mail Account: Unknown

 

We are using the official reference hook guide and the available parameters (ticketid, ticketmask, userid, deptid, deptname, subject, message, priority) don't include any to retrieve sender's mail account so we are posting this to see if you can give us any advice on how to do this.

Here is the hook script we have so far:

<?php
add_hook('TicketOpen', 1, function($vars) {
    // Debug log
    $logFile = '/tmp/whmcs_ticket_debug.log';

    // Save $vars structure in the log file to verify what data is being received
    file_put_contents($logFile, "Received data: " . print_r($vars, true), FILE_APPEND);

    // Get ticket ID
    $ticketMask = $vars['ticketmask'] ?? 'No ID';
    file_put_contents($logFile, "Ticket Mask: $ticketMask\n", FILE_APPEND);

    // Default client's name is 'Unknown'
    $clientName = 'Unknown sender';
    $clientEmail = 'Unknown';  // Additional variable for email

    // If the ticket is sent by a registered user (userid isn't empty)
    if (!empty($vars['userid'])) {
        // Get client details
        $clientDetails = localAPI('GetClientsDetails', ['clientid' => $vars['userid']]);
        // If it finds a name or a mail account it uses it
        $clientName = $clientDetails['fullname'] ?? 'Unknown client';
        $clientEmail = $clientDetails['email'] ?? 'Unknown';
        file_put_contents($logFile, "Registered client: $clientName, Mail account: $clientEmail\n", FILE_APPEND);
    } else {
        // If there is no userid, try to retrieve it from the mail itself
        if (!empty($vars['message'])) {
            if (preg_match('/[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}/', $vars['message'], $matches)) {
                $clientEmail = $matches[0];
                $clientName = 'Sender: ' . $clientEmail;
                file_put_contents($logFile, "Unregistered client, mail account found: $clientEmail\n", FILE_APPEND);
            } else {
                file_put_contents($logFile, "It wasn't possible to find a mail account in the message.\n", FILE_APPEND);
            }
        }
    }

    // Get ticket's subject
    $subject = $vars['subject'] ?? 'Unknown subject';
    file_put_contents($logFile, "Subject: $subject\n", FILE_APPEND);

    // Write the notification message for Synology Chat
    $message = "New ticket created by: $clientName\nTicket ID: $ticketMask\nSubject: $subject\nMail Account: $clientEmail";
    file_put_contents($logFile, "Notification preview: " . $message . "\n", FILE_APPEND);

    // Synology Chat webhook URL
    $url = 'https://CENSORED FOR OBVIOUS REASONS...';

    // Create payload
    $payload = ['payload' => json_encode(['text' => $message])];

    // Start up curl
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));

    // Run curl request
    $response = curl_exec($ch);

    // Log curl response
    file_put_contents($logFile, "Curl output: $response\n", FILE_APPEND);

    // Close curl session
    curl_close($ch);
});
?>

 

And this is the last logged response from the debug log we added:

Received data: Array
(
    [ticketid] => 81
    [ticketmask] => STW-189085
    [userid] =>
    [deptid] => 2
    [deptname] => Technical Support
    [subject] => 04/04 10:45 second test
    [message] => This is a test.
    [priority] => Medium
)
Ticket Mask: STW-189085
It wasn't possible to find a mail account in the message
Subject: 04/04 10:45 second test
Notification preview: New ticket created by: Unknown sender
Ticket ID: STW-189085
Subject: 04/04 10:45 second test
Mail Account: Unknown

Link to comment
Share on other sites

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