Search This Blog

Tuesday 5 July 2016

Escaping the “&” character in a submission email


Within a mailto URL, as used by the EmailSubmitButton, the character "&" has a special meaning as a separator between the headers (to, cc, subject, etc).  In an HTML page a "&" character within the header value can be escaped as %26, but in the mysterious world of LiveCycle Designer forms the “%” needs to be escaped as well, giving us %2526.

 For example;

mailto:?subject=Q%2526A

Generates an email with the subject of “Q&A”.

This sample form uses the following code in the "Submit by Email" button to generate the mailto URL and uses two calls the encodeURIComponent generate the correct escaping for the "&" character .

var encodedAmpersand = encodeURIComponent(encodeURIComponent("&"));
var headers = [];    

if (!To.isNull) headers.push("to=" + To.rawValue);
if (!CC.isNull) headers.push("cc=" + CC.rawValue);
if (!BCC.isNull) headers.push("bcc=" + BCC.rawValue);
if (!Subject.isNull) headers.push("subject=" + Subject.rawValue.replace(/&/g, encodedAmpersand));

if (!Body.isNull) headers.push("body=" + Body.rawValue.replace(/&/g, encodedAmpersand));
        
var mailtoTarget = "mailto:?" + headers.join("&");
 
EmailSubmitButton.event__click.submit.target = mailtoTarget;
EmailSubmitButton.execEvent("click");

Note: This only applies to submission emails using an Email Submit Button.  If you are using app,mailMsg() or a mailto URL in a rich text hyperlink then you just use %26.

The sample form, mailto&.pdf, just tests the above code
Event propagation

The sample form shows the XML of the Email Submit Button as you type into the form fields for To, CC, BCC, Subject and Body.  This was done using event propagation, so the sample will only work in Reader 9.1 or later, though the technic of escaping the “&” will work in earlier versions.  The trick here was to have the change event on a subform.  A subform does not support the change event so Designer does not allow you to enter the code.  However, it does still seem to work you just need to enter the code in the XML Source view.