sivaprasad9394
Occasional Collector

Encoding ampersand (&) in <a> link href mailto subject text?

Jump to solution

Dear Team,

How to do encoding of & in the mailto subject in the FS 5.2.2109?

1.First Option:

--$$CMS_IF(fs_portal_pageTitle != null && !fs_portal_pageTitle.isEmpty)$$CMS_VALUE(fs_portal_pageTitle)$%0D%0A$CMS_END_IF$$--

After & symbol text and the remaining things in subject line is missing.seethe image below.

?subject=You have received a recommendation&body=Dear Sir or Madame,%0D%0Athe sender of this e-mail would like to recommend a link to you%0D%0A%0D%0ATest Module & Page Display%0D%0Ahttps://inside-ws-d.bosch.com/FIRSTspiritWeb/permlink/wcms_c_-standard_page_33-EN%0D%0A%0D%0AThank you

Test_1.png

Test_2.png

Test_3.png

2. Second Option:

--$$CMS_IF(fs_portal_pageTitle != null && !fs_portal_pageTitle.isEmpty)$$CMS_VALUE(fs_portal_pageTitle.replaceAll("&", "%26"))$%0D%0A$CMS_END_IF$$--

$CMS_VALUE(fs_portal_pageTitle.replace("&", "%26")$

?subject=You have received a recommendation&body=Dear Sir or Madame,%0D%0Athe sender of this e-mail would like to recommend a link to you%0D%0A%0D%0ATest Module &amp; Page Display 55%0D%0Ahttps://data.com/wcms_c_-standard_page_33-EN%0D%0A%0D%0AThank you

Image:

Image_1.png

3.Third Options:

$CMS_VALUE(fs_portal_pageTitle.replace("&", "&#38;").urlEncode,default:"")$

$CMS_VALUE(class("java.net.URLEncoder").encode(fs_portal_pageTitle.toString().trim(), "UTF-8"))$

Test_4.png

I have tried all the above options nothing worked for me.

Thank you.

0 Kudos
19 Replies

Hi Michael,

yea, you are correct and i have removed the "convertEntities" and the "manual" encoding and added .convert2 for the 'set_adviceBody'(This is outlook page body content of the email).

But still no help body is empty.

Remove_1.png

Remove_2.png

Remove_3.png

body content is completely empty now.

Thanks and Regards,

Siva

0 Kudos

Hi Siva,

  1. The "two part CMS_SET" does not define a string but rather a so called "TemplateDocument". To be able to use string methods on it, you must first force it's evaluation using .toString()
  2. You are using .convert2 here which is wrong as I explained earlier because here, you need urlEncode

So you just need to change the line to

<a href="mailto:?subject=$CMS_VALUE(set_adviceSubject)$&body=$CMS_VALUE(set_adviceBody.toString().urlEncode())$"

Michael

0 Kudos

Hello Michael,

I have tried the solution but it's not working properly.Also text formatting issue there.Attached image for your reference,

URL_3.png

URL_2.png

$CMS_SET(set_adviceSubject,if(#global.language.abbreviation.equals("DE"),"Sie haben eine Empfehlung erhalten","You have received a recommendation"))$

$CMS_SET(set_adviceBody)$

    $CMS_IF(#global.language.abbreviation.equals("DE"))$

        Sehr geehrte Dame,sehr geehrter Herr,Ader Absender dieser E-Mail möchte Ihnen eine Empfehlung aussprechen

    $CMS_ELSE$

        Dear Sir or Madame, the sender of this e-mail would like to recommend a link to you

    $CMS_END_IF$

    $CMS_IF(fs_portal_pageTitle != null && !fs_portal_pageTitle.isEmpty)$

        $CMS_VALUE(fs_portal_pageTitle)$

    $CMS_END_IF$

    $CMS_VALUE(ps_publishingHost)$$CMS_VALUE(ps_permLinkContext)$$CMS_RENDER(script:"permlinkcreator", element:#global.node)$

    $CMS_IF(#global.language.abbreviation.equals("DE"))$Vielen Dank$CMS_ELSE$Thank you$CMS_END_IF$

$CMS_END_SET$

<li><a href="mailto:?subject=$CMS_VALUE(set_adviceSubject)$&body=$CMS_VALUE(set_adviceBody.toString().urlEncode())$"

title="$CMS_VALUE(pt_langswitch.advisePage_help)$"><img src="$CMS_REF(media:"leer")$" class="sprite_empfehlenicon_liste">$CMS_VALUE(pt_langswitch.advisePage)$</a></li>

URL_1.png

Also tried - &body=$CMS_VALUE(set_adviceBody.toString().urlEncode().replaceAll("+", " "))$" but the result is same same above in the outlook.

Not sure any other configuration or some thing else required.

Thank you,

Regards,

Siva

0 Kudos

Hi Siva,

can you please post the generated source code for the link?

Michael

0 Kudos

Hi Michael,

Listed down Generated source and fs5staging content for your reference,

Generated source code(HTML):

Item.png

?subject=You have received a recommendation&body=%0A%09+%0A%09%09Dear+Sir+or+Madame,+the+sender+of+this+e-mail+would+like+to+recommend+a+link+to+you%0A%09%0A%09%0A%09%09Test+Module+&+Page+Display+&+Final+Test%0A%09%0A%09https://inside-ws-d.data.com/FIRSTspiritWeb/permlink/wcms_c_-standard_page_33-EN%0A%09Thank+you%0A

Generated code in fs5staging:

code.png

<li><a href="mailto:?subject=You have received a recommendation&body=%0A%09+%0A%09%09Dear+Sir+or+Madame%2C+the+sender+of+this+e-mail+would+like+to+recommend+a+link+to+you%0A%09%0A%09%0A%09%09Test+Module+%26+Page+Display+%26+Final+Test%0A%09%0A%09https%3A%2F%2Finside-ws-d.data.com%2FFIRSTspiritWeb%2Fpermlink%2Fwcms_c_-standard_page_33-EN%0A%09Thank+you%0A" title="Recommend this page by email."><img src="../../../../../media/system/layout/leer.gif" class="sprite_empfehlenicon_liste">Recommend page</a></li>

Thank you,

regards,

Siva

0 Kudos

Hi Siva,

the idea to replace the + is correct in general as some mail clients expect spaces to be encoded as %20. But as .replaceAll() works with regular expressions, you have to escape the + sign using \\+ (technically using \+ but as the \ must again be escaped, you have to use two of them).

The formatting problems originate in the tab characters (coming from the template) also being encoded (as %09) and the additional line breaks before/after the $CMS_IF$/$CMS_ELSE$ parts.

One solution would be to "comment them out" using $-- ... --$, but that would result in quite ugly template code. I personally prefer the "inline if" version.

I played around a little and I think a good solution would be to "centrally" define necessary replacements in a kind of list to avoid adding multiple .replace[All] calls. This ways you could even put them in a project settings configuration (not part of example below).

Change the $CMS_SET(set_adviceBody)$...$CMS_END_SET$ part by replacing $CMS_IF()$ with $CMS_VALUE(if(...))$ to avoid multiple line breaks introduced by the line breaks after the $CMS_IF$/$CMS_ELSE statements.

Then, after the $CMS_END_SET$ use the following code to do the needed replacements and render the link:

$CMS_SET(set_adviceBody)$

    $CMS_VALUE(if(#global.language.abbreviation.equals("DE"),

         "Sehr geehrte Dame,sehr geehrter Herr,Ader Absender dieser E-Mail möchte Ihnen eine Empfehlung aussprechen",

         "Dear Sir or Madame, the sender of this e-mail would like to recommend a link to you"

    ))$

    $CMS_VALUE(if(fs_portal_pageTitle != null && !fs_portal_pageTitle.isEmpty,fs_portal_pageTitle))$

    $CMS_VALUE(ps_publishingHost)$$CMS_VALUE(ps_permLinkContext)$$CMS_RENDER(script:"permlinkcreator", element:#global.node)$

    $CMS_VALUE(if(#global.language.abbreviation.equals("DE"),

         "Vielen Dank",

         "Thank you"

    ))$

$CMS_END_SET$

$CMS_SET(set_replacements, [

     ["\\t", ""],

     ["\\+", "%20"]

])$

$CMS_SET(set_encodedBody, set_replacements

     .fold(s:set_adviceBody.toString().trim(), x->s.replaceAll(x[0],x[1]))

     .urlEncode()

)$

<li><a href="mailto:?subject=$CMS_VALUE(set_adviceSubject)$&body=$CMS_VALUE(set_encodedBody)$"

...

Michael

0 Kudos
mikula
Crownpeak employee

Hello Siva, 

i guess that you did already find a solution, that works for you? If so, it would be great if you marked the response that helped you most as "correct answer" so that other community members can easily find the solution. Thank you for sharing your solutions.

Best regards 

Martin

0 Kudos

Hello  Michael,

Thank you for your detail replay. I have applied the same. Also there is no 'convertEntities="quote"' in form component attribute.

The out put looks like below,

Page title:

Title_1.png

output in outlook looks like below:

Title_2.png

For the next line we can add <br> tag but for removing the + symbol for white space is problem.Even + is is encoded as %20 here.?

Thank you.

0 Kudos

Hi Siva,

sorry, my mistake. Of course the replacement of "+" by %20 must be done after the urlEncode()... The following code should also remove leading whitespaces from each line.

$CMS_SET(set_replacements, [

     ["\\n\\s*", "\n"]

])$

$CMS_SET(set_encodedBody, set_replacements

     .fold(s:set_adviceBody.toString().trim(), x->s.replaceAll(x[0],x[1]))

     .urlEncode().replace("+","%20")

)$

Michael

0 Kudos

Hello Michael,

Thank you for your answer and time on this issue. really its helpful for me to understand the things much better.

convertEntities="quote" is removed for the below component,

      <CMS_INPUT_TEXTAREA  name="fs_portal_pageTitle" ....... ... .. . />

Every thing works except gg+g&h, its changed into gg%20g&h. This is shown in the previous thread also. Except this problem all the other things are working fine and good.

Result:

outlook.png

Also some change in the code works for me even with convertEntities="quote" is enabled for the component, This is working for me.

$-- Build the body content--$

$CMS_SET(set_adviceBodyPlain)$

      $CMS_VALUE(if(#global.language.abbreviation.equals("DE"), 

         "Sehr geehrte Dame,

  sehr geehrter Herr,Ader Absender dieser E-Mail möchte Ihnen eine Empfehlung aussprechen \n",  

         "Dear Sir or Madame,

the sender of this e-mail would like to recommend a link to you \n"

    ))$ 

$CMS_VALUE(if(fs_portal_pageTitle != null && !fs_portal_pageTitle.isEmpty,fs_portal_pageTitle))$ 

$CMS_VALUE(ps_publishingHost)$$CMS_VALUE(ps_permLinkContext)$$CMS_RENDER(script:"permlinkcreator", element:#global.node)$ 

$CMS_VALUE(if(#global.language.abbreviation.equals("DE"),        

"\n Vielen Dank", 

"\n Thank you"

    ))$ 

$CMS_END_SET$

$-- Unescapes a string containing entity escapes to a string containing the actual Unicode characters --$

$CMS_SET(set_adviceBodyHtmlDecoded)$$CMS_VALUE(class("org.apache.commons.lang.StringEscapeUtils").unescapeHtml(set_adviceBodyPlain.toString().trim()))$$CMS_END_SET$

$--  This method uses the supplied encoding scheme to obtain the bytes for unsafe characters.  --$

$CMS_SET(encodedBody)$$CMS_VALUE(class("java.net.URLEncoder").encode(set_adviceBodyHtmlDecoded.toString().trim(), "UTF-8"))$$CMS_END_SET$

$-- Spaces are converted into ‘+‘ signs. --$

    $CMS_SET(encodedBody, encodedBody.toString.replace("+", "%20"))$

<li><a href="mailto:?Content-Type=text/html&subject=$CMS_VALUE(set_adviceSubject)$&body=$CMS_VALUE(set_encodedBody)$" title="$CMS_VALUE(pt_l... ... .. . ) </li>

Thank you.

Regards,

Siva

0 Kudos