Jump to content

EmailPreSend hook works for admin but not cron jobs?


Phil82cc

Recommended Posts

Hello,

We use WHMCS internally only for a system we have (ie its not accessible from the internet, only via a small internal network).   Ive been trying to get 2x email templates that get sent to clients, to contain a "wifipassword" , in the email, via a custom smarty tag {$userwifipass} in the email templates.

Im using the EmailPreSend hook,  and this all works perfect when i manually send an email from the admin section (ie whmcs admin webgui),  but when our daily cron runs,  it does not work (the email sends, but the smarty tag is blank).

I have added logActivity() into my hook in an attempt to debug some of the variables, and it appears that $vars['relid']; is not getting defined when cron runs.  

Any one have any ideas on the issue? 
(maybe its some kind of security setting i need to change so that whmcs cron username "System" has access?).  Below is my hook code and some info.  thanks for any help!

in 2x whmcs email templates im using the smarty tag {$userwifipass}  which gets defined from this hook below:
located in file: /var/www/html/whsite/includes/hooks/wifipassInEmailTemplates.php

	use WHMCS\Database\Capsule as DB;
	add_hook('EmailPreSend', 1, function($vars) {
     $merge_fields = [];
	$productid = $vars['relid'];
	logActivity('EmailPreSend Hook var productID is '.$productid.' ');
	$jocid=DB::table('tblorders')->where('id',$productid)->value('userid');
	logActivity('EmailPreSend Hook var jocid is '.$jocid.' ');
	$pass=DB::table('mod_wifimanager_clients')->where('cid',$jocid)->value('password');
	$merge_fields['userwifipass'] = decrypt($pass);
	logActivity('EmailPreSend Hook var wifi password is '.$merge_fields['userwifipass'].' ');
	    return $merge_fields;
});
	

and from the activity log, you can see when I (admin= pwayadminc) manually send the email template via "Send Message" in whmcs admin web gui, it works, as $productid = $vars['relid'];  gets defined.  But when cron / user=System runs, relid is not defined (so hook / smarty tag {$userwifipass} is blank):

 

image.png.db9f20d313857442b7360b5e09cbfe54.png

vs via CRON:

image.png.9f8801e3faa7391ef6d00488afaf381b.png

 

Edited by Phil82cc
Link to comment
Share on other sites

15 hours ago, Phil82cc said:

and from the activity log, you can see when I (admin= pwayadminc) manually send the email template via "Send Message" in whmcs admin web gui, it works, as $productid = $vars['relid'];  gets defined.  But when cron / user=System runs, relid is not defined (so hook / smarty tag {$userwifipass} is blank):

the obvious issue that I can see is that you're testing two different methods using two different templates... e.g you're testing a product template via the admin area and an invoice template via the cron...

now the first method works because it's a product template and therefore relid = product ID; the second likely fails because it's an invoice template and therefore, relid = invoice ID NOT product ID.

therefore, there is an issue with the hook and its assumption that relid is always equal to product ID - but that's only the case with product templates.

I would suggest using the messagename variable to specify different methods for each email template type - so for products, the above is fine... for invoices, you've only got the invoice id to work with and so you'll either have to work your way through the tables to get the value you want.. or simply just not bother including it in these payment confirmation emails. 🙂

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