Jump to content

Hook preventing emails from a client group not working


adinase

Recommended Posts

We are trying to prevent all emails from being sent from whmcs for customers under " old customer " group

The below one fails to prevent emails, failing to figure this out !!

Can anyone help?? ...  where are getting it wrong ??

 

<?php

use WHMCS\Database\Capsule;

add_hook('EmailPreSend', 1, function($vars)
{
    $disallowedGroupID = '1'; // Insert Client Group ID
    $emailTemplates = array('Credit Card Payment Due', 'Automated Password Reset', 'Invoice Created', 'Invoice Modified', 'Upgrade Order Cancelled', 'Other Product/Service Welcome Email', 'Cancellation Request Confirmation', 'First Invoice Overdue Notice', 'Password Reset Validation', 'Invoice Payment Reminder', 'Second Invoice Overdue Notice', 'Third Invoice Overdue Notice', 'Domain Registration Confirmation', 'Domain Renewal Confirmation', 'Domain Transfer Completed', 'Domain Transfer Failed', 'Domain Transfer Initiated', 'Expired Domain Notice', 'Upcoming Domain Renewal Notice', 'Order Confirmation', 'Password Reset Confirmation', 'Client Signup Email', 'Client Email Address Verification', 'Default Notification Message', 'Hosting Account Welcome Email', 'Service Suspension Notification', 'Service Unsuspension Notification', 'Support Ticket Opened', 'Support Ticket Reply', 'Support Ticket Auto Close Notification''Credit Card Invoice Created', 'Credit Card Payment Confirmation', 'Credit Card Payment Failed', 'Credit Card Payment Pending', 'Direct Debit Payment Confirmation', 'Direct Debit Payment Failed', 'Direct Debit Payment Pending', 'Direct Debit Payment Failed', 'Credit Card Invoice Created', 'Credit Card Payment Confirmation', 'Credit Card Payment Failed', 'Credit Card Payment Pending', 'Direct Debit Payment Confirmation', 'Direct Debit Payment Failed', 'Direct Debit Payment Pending', 'Direct Debit Payment Failed'); // Email Templates to block

    if (in_array($vars['messagename'], $emailTemplates))
    {
        if (!Capsule::select(Capsule::raw('SELECT id FROM tblclients WHERE id = "' . $vars['relid'] . '" AND groupid = "' . $groupid . '" LIMIT 1')))
        {
            $output['abortsend'] = true;
            return $output;
        }
    }
});

 

Link to comment
Share on other sites

On 27/04/2021 at 09:50, adinase said:

We are trying to prevent all emails from being sent from whmcs for customers under " old customer " group

EmailPreSend would only prevent most templated emails from being sent.

On 27/04/2021 at 09:50, adinase said:

The below one fails to prevent emails, failing to figure this out !!

I take it that you're working off Kian's hook code...

https://github.com/Katamaze/WHMCS-Action-Hook-Factory/blob/master/hooks/PreventEmailSendingBasedOnClientGroup.php

On 27/04/2021 at 09:50, adinase said:

Can anyone help?? ...  where are getting it wrong ??

two obvious issues - there may be others.

  1. the relid value will be different I think depending on what email type the message is (e.g general, product, invoice, support etc) - the point being that it won't always be a client ID value.... depending on the type, you might need to use a database query to get the client ID value by using relid as a starting point.
  2. the query references a $groupid variable, but that variable is never defined in the hook.
Link to comment
Share on other sites

As stated here...

    $emailTemplates = array('... cut..'); // Email Templates to block (General Messages)

... by default the hook in question  works only with General Messages. If you want to prevent other types of emails, you'll need to change the query accordingly. You can use as reference sendEmail API. I quote:

Quote

 

What you must provide for the Related ID depends upon the type of email being sent. The available options are:

  • General Email Type = Client ID (tblclients.id)
  • Product Email Type = Service ID (tblhosting.id)
  • Domain Email Type = Domain ID (tbldomains.id)
  • Invoice Email Type = Invoice ID (tblinvoices.id)
  • Support Email Type = Ticket ID (tbltickets.id)
  • Affiliate Email Type = Affiliate ID (tblaffiliates.id)

 

 

 

Link to comment
Share on other sites

  • 1 month later...

This  one below is working now !!

 

<?php

use WHMCS\Database\Capsule;

function hook_disableAllEmails($vars)
{
    $disallowedGroupID = '1'; // Insert Client Group ID
    $emailTemplates = array('Credit Card Payment Due', 'Automated Password Reset', 'Invoice Created', 'Invoice Modified', 'Upgrade Order Cancelled', 'Other Product/Service Welcome Email', 'Cancellation Request Confirmation', 'First Invoice Overdue Notice', 'Password Reset Validation', 'Invoice Payment Reminder', 'Second Invoice Overdue Notice', 'Third Invoice Overdue Notice', 'Domain Registration Confirmation', 'Domain Renewal Confirmation', 'Domain Transfer Completed', 'Domain Transfer Failed', 'Domain Transfer Initiated', 'Expired Domain Notice', 'Upcoming Domain Renewal Notice', 'Order Confirmation', 'Password Reset Confirmation', 'Client Signup Email', 'Client Email Address Verification', 'Default Notification Message', 'Hosting Account Welcome Email', 'Service Suspension Notification', 'Service Unsuspension Notification', 'Support Ticket Opened', 'Support Ticket Reply', 'Support Ticket Auto Close Notification','Credit Card Invoice Created', 'Credit Card Payment Confirmation', 'Credit Card Payment Failed', 'Credit Card Payment Pending', 'Direct Debit Payment Confirmation', 'Direct Debit Payment Failed', 'Direct Debit Payment Pending', 'Direct Debit Payment Failed', 'Credit Card Invoice Created', 'Credit Card Payment Confirmation', 'Credit Card Payment Failed', 'Credit Card Payment Pending', 'Direct Debit Payment Confirmation', 'Direct Debit Payment Failed', 'Direct Debit Payment Pending', 'Direct Debit Payment Failed'); // Email Templates to block

    if (in_array($vars['messagename'], $emailTemplates))
    {
        if (!Capsule::select(Capsule::raw('SELECT id FROM tblclients WHERE id = "' . $vars['relid'] . '" AND groupid = "' . $groupid . '" LIMIT 1')))
        {
            $output['abortsend'] = true;
            return $output;
        }
    }
}
add_hook("EmailPreSend", 1, "hook_disableAllEmails");

?>

 

 

Question is how to prevent all emails without defining each and every email in the array ??

Link to comment
Share on other sites

On 07/06/2021 at 13:06, adinase said:

Question is how to prevent all emails without defining each and every email in the array ??

to prevent all *templated* emails, then you would just remove the in_array as it's that that is limiting which templates the hook applies to.

<?php

use WHMCS\Database\Capsule;

function hook_disableAllEmails($vars) {
	
	$disallowedGroupID = '1'; // Insert Client Group ID
	if (!Capsule::select(Capsule::raw('SELECT id FROM tblclients WHERE id = "' . $vars['relid'] . '" AND groupid = "' . $groupid . '" LIMIT 1'))) {
		$output['abortsend'] = true;
		return $output;
	}
}
add_hook("EmailPreSend", 1, "hook_disableAllEmails");
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