Feature Requests
Share ideas, discuss and vote on requests from other users in community
 

Send invoice related mail to Invoice contact only (if exists)

WHMCS-addons.eu shared this idea 5 years ago
Under Consideration

We've ad several clients complain about this.

If a Billing contact has been defined, any billing related mail should only go to that person.

Especially with bigger companies, but more and more with the smaller companies aswell, they have a dedicated address for billing / invoicing related E-mail.

We or they define it as such in the system, but to their (and our) surprise the main contact is still receiving all billing E-mail aswell and they find this very annoying as well as that it doesn't help clarity or streamlining payment of the invoices.

When a billing contact has been defined (or another contact that has billing ticked aswell), that contact should get billing related issues, and the main contact shouldn't anymore.

I hope this gets implemented really soon.

Thanks

Comments (26)

photo
1

Agree, this is needed for us and our clients. Excellent work to

ramf for all his hard work and detailed notes.

photo
1

Thanks! I really hope WHMCS will fix this issue.

photo
photo
1

Just came here for this. Simple: If there is a contact for billing set don‘t send mail to other contacts.

Please fix this. Now.

photo
1

While we wait for WHMCS to fix this, I've written a bit of a hack in php to achieve it. Add this to your includes->hooks folder. Note that it has minimal testing so use at your own risk and/or suggest improvements. It may also break in future WHMCS updates.

This updates the email address of the primary account to that of the Billing contact whenever a new invoice is created. It then resets the original email address after the emails have sent.

  1. /*
  2. * Temporarily updates the primary account's email address to that of the
  3. * company's Billing contact upon creation of a new invoice so that invoices
  4. * are not sent to the primary account holder (reminders are not affected).
  5. */
  6. function update_account_email_for_invoices($vars){
  7. $inv_user = $vars['user'];
  8. //lookup this user's account to see if it has a billing contact
  9. $billing_contact = Capsule::table('tblclients')->where('id', $inv_user)->pluck('billingcid');
  10. if( is_array($billing_contact) ){
  11. $billing_contact = $billing_contact[0];
  12. }
  13. if( 0 == $billing_contact || null == $billing_contact || '' == $billing_contact )
  14. return; //no billing contact set for this client
  15. $billing_email = Capsule::table('tblcontacts')->where('id', $billing_contact)->pluck('email');
  16. if( is_array($billing_email) ){
  17. $billing_email = $billing_email[0];
  18. }
  19. if( null === $billing_email || '' === $billing_email )
  20. return; //a bit of error handling - don't update if the billing contact has no email address or has been deleted
  21. //lookup the current user's primary account email address and save as a transient - this lets us restore it later
  22. $curr_email = Capsule::table('tblclients')->where('id', $inv_user)->pluck('email');
  23. if( is_array($curr_email) ){
  24. $curr_email = $curr_email[0];
  25. }
  26. $saved = Capsule::table('tbltransientdata')->where('name', 'clientEmailUpdates')->pluck('data');
  27. if( is_array($saved) ){ //get saved transient and add to array (in-case multiple invoices are being generated)
  28. $saved = $saved[0];
  29. }
  30. if( '' != $saved && null != $saved ){ //decode serialised string
  31. $saved = unserialize($saved);
  32. } else {
  33. $saved = array();
  34. }
  35. $saved[$inv_user] = $curr_email; //add current userID and primary account email address to array
  36. //update the primary account contact email to the billing contact's email address
  37. if( Capsule::table('tblclients')->where('id', $inv_user)->update( array('email' => $billing_email) ) ){
  38. //save the previous primary email address as a transient value so that we can restore it later
  39. if( !Capsule::table('tbltransientdata')->where('name', 'clientEmailUpdates')->update( array('data' => serialize($saved), 'expires' => time() + (6*60*60)) ) ){
  40. Capsule::table('tbltransientdata')->insert(array(
  41. 'name' => 'clientEmailUpdates',
  42. 'data' => serialize($saved),
  43. 'expires' => time() + (6*60*60)
  44. ));
  45. }
  46. }
  47. return;
  48. }
  49. add_hook("InvoiceCreation",2,"update_account_email_for_invoices");
  50. function fix_auto_invoice_emails( $whatever ){
  51. //lookup all of the email addresses which need to be updated
  52. $saved = Capsule::table('tbltransientdata')->where('name', 'clientEmailUpdates')->pluck('data');
  53. Capsule::table('tbltransientdata')->where('name', 'clientEmailUpdates')->update( array('data' => serialize(array()) ) );
  54. if( is_array($saved) ){
  55. $saved = $saved[0];
  56. }
  57. if( '' != $saved && null != $saved ){ //decode serialised string
  58. $saved = unserialize($saved);
  59. } else {
  60. $saved = array();
  61. }
  62. if( !empty($saved) ){
  63. foreach( $saved as $user_id => $email ){ //loop through each account to update
  64. if( !Capsule::table('tblclients')->where('id', $user_id)->update( array('email' => $email) ) ){ //restore primary email address
  65. //error - maybe do some error recovery here (later)
  66. }
  67. }
  68. }
  69. }
  70. //lots of hooks to make sure it's run (no idea if all of these are really neccessary, but it works)
  71. add_hook("AdminAreaFooterOutput",2,"fix_auto_invoice_emails");
  72. add_hook("ClientAreaFooterOutput",2,"fix_auto_invoice_emails");
  73. add_hook("AfterCronJob",2,"fix_auto_invoice_emails");
  74. add_hook("EmailPreSend",2,"fix_auto_invoice_emails");
  75. add_hook("PostAutomationTask",2,"fix_auto_invoice_emails");

photo
1

Update: I've created a GIST for this with some updates to try to get it working for auto-generated invoices as well as reminder notifications and payment confirmation emails.

View this at: https://gist.github.com/Pierowheelz/1aa6b1ede10073552df62475084a1e75

photo
1

Thanks Peter. I assume that this hack is compatible with all versions of WHMCS? I am running two versions of WHMCS: 7.4.1 and 7.5.

photo
1

Thanks, Peter!

photo