Since a few months I’ve been using WHMCS for all invoicing for my company Exa-Omicron. I’ve altered the standard email templates, but there was one thing that would not work very nicely and the richtext editor is causing problems.

I wanted to use foreach loops in the email templates to loop over all the invoice items. This way the customer can see all the invoice line items in the email instead of opening the PDF. By using a table for the invoice items, I made the email look almost exactly the same as the invoice PDF. So for example:

<p><strong>Invoice Items</strong></p>

<table style=”border-collapse: collapse;”>
<tbody><tr style=”background-color: #000; color: #fff”><td style=”border: 1px solid black;”>Description</td><td align=”right” style=”border: 1px solid black;”>Total</td></tr>

{foreach from=$invoice_items item=invline}

<tr><td style=”border: 1px solid black;”>{$invline.description}</td><td align=”right” style=”border: 1px solid black;”>{$invline.amount}</td></tr>

{/foreach}

<tr><td align=”right”>Subtotal</td><td align=”right”><strong>{$invoice_subtotal}<strong></strong></strong></td></tr>
<tr><td align=”right”>{$invoice_tax_rate} VAT</td><td align=”right”>{$invoice_tax}</td></tr>
<tr><td align=”right”><strong>Total</strong></td><td align=”right” style=”border-top: 1px solid black;”><strong>{$invoice_total}</strong></td></tr>
</tbody></table>

But when reopening the email template, the richtext editor does not understand the Smarty template elements like foreach. The richtext editor tries to correct the HTML and moves the foreach lines outside of the <table> tags. but this makes the whole email template unusable, because it will not generate correct HTML anymore. My html knowledge is good enough to write templates without a richtext editor, so I searched for an option to disable the richtext editor completely. Unfortunately I could not find such an option in WHMCS. So I added a apache rewriteengine rule in the .htaccess file to forward the webrequest from the browser to always disable the richtext editor.

# Always disable richtext editor
RewriteCond %{REQUEST_URI} ^/admin/configemailtemplates.php$
RewriteCond %{QUERY_STRING} ^action=edit&id=([0-9]+)$
RewriteRule .* ./admin/configemailtemplates.php?action=edit&id=%1&noeditor=1 [L]

So when the browser requests the url /admin/configemailtemplates.php and the query string only holds a action = edit and a numeric id, then the browser is redirected to the exactl same URI, but with a &noeditor=1 added to the query string.

And this solved all my ‘richtext editor tries to correct html’ issues 🙂