Zum Hauptinhalt gehen

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

Kommentare

19 Kommentare

  • Zendesk API User
    Author: kohlbrecher - 9/24/2018 14:37

    Hi Siva,

    can you please check your conversion rule. Maybe this is the reason why %26 gets &.

    Best regards

    Jan

    0
  • Zendesk API User
    Author: sivaprasad9394 - 9/25/2018 4:33

    Hi  Jan,

    Thank you for your reply and time. Looks like conversion rules given properly.

    Listed out the image for your reference,

    --$$CMS_IF(fs_portal_pageTitle != null && !fs_portal_pageTitle.isEmpty)$$CMS_VALUE(fs_portal_pageTitle.convert2.replace("&", "%26").urlEncode,default:"")$%0D%0A$CMS_END_IF$$--

    Output: Test+Module+%26amp;amp;+Page+Display+3

    How to make it to display & in between like below,

    Test Module & Page Display 3

    Thank you,

    Regards,

    Siva

    0
  • Zendesk API User
    Author: mbergmann - 9/25/2018 19:32

    Hi Siva,

    I think you are mixing too many things/conversions together here (especially by combining "manual" encoding with method calls). In general, .convert2 encodes content to be displayed in HTML and is the wrong conversion for URL parameters.

    My advice is to first build up the complete content of the mail body in a plain string variable (without any replacements, manual "encoding" of blanks etc. or conversions!) and then as a last step just do the urlEncode for the complete string.

    Example:

    $CMS_SET(set_adviceBody)$

    ...

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

    ...

    $CMS_END_SET$

    ...

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

    Michael

    0
  • Zendesk API User
    Author: sivaprasad9394 - 9/26/2018 6:26

    Hi Michael,

    Thank you for your detailed reply and time.

    My problem is &(ampersand) symbol not displaying in the email properly.(Test Module & Page Display 1) Without & my existing code works fine.

    I have all the (manual encoding and blanks and conversions, but need to consider next line of text and other format) and created a plain content which need to generated in mailto option.

    Below image for your reference,

    $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("Test Module & Page Display 1".toString())$

        $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>

    Below image is the Result of Above code:

    Actual Result Should be like below image (of mail to):

    0
  • Zendesk API User
    Author: mbergmann - 9/26/2018 6:40

    Hi Siva,

    I suppose „fs_portal_pageTitle“ comes from an input component. Can you post it‘s code please?

    Or does it even not work when you use the version without input component?

    Michael

    0
  • Zendesk API User
    Author: sivaprasad9394 - 9/26/2018 7:07

    Hi Michael,

    Yes its a input component from Standardpage. convertEntities value is 'quote'.

    Its nothing but page title as shown in the below image -- 'fs_portal_pageTitle' .

    Thank you.

    Regards,

    Siva

    0
  • Zendesk API User
    Author: mbergmann - 9/26/2018 7:19

    Hi Siva,

    that‘s at least a part of the problem. I generally recommend to NEVER use converEntities in a form definition but instead only use the proper conversion in the output channel, e.g. .convert2 for the HTML output on a page.

    convertEntities leads to various problems especially like yours ;-)

    In your case the convertEntities already does a conversion when accessing the variable which is wrong here as you have to urlEncode the original string, not an already converted one.

    Michael

    0
  • Zendesk API User
    Author: sivaprasad9394 - 9/26/2018 7:49

    Hi Michael,

    Yes the specific component is already converted and again converting it leads to problem.

    Finally resolved the problem as like below,

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

    Thank you for your kind support Mr.Michael. Really it is helpful to understand the problem more in detail for FirstSpirit topics.

    Regards,

    Siva

    0
  • Zendesk API User
    Author: mbergmann - 9/26/2018 8:01

    Hi Siva,

    you‘re welcome. But to be honest I think your approach is a little „ugly“ because you are just „manually undoing“ a generally wrong conversion.

    This may lead to problems in the future - e.g. if the title contains a < or > etc. because your solution is just an incomplete workaround ;-)

    I‘d really recommend the „clean“ approach of removing the convertEntities and the „manual“ encoding and adding .convert2 for the page output.

    Michael

    0
  • Zendesk API User
    Author: sivaprasad9394 - 9/26/2018 11:00

    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.

    body content is completely empty now.

    Thanks and Regards,

    Siva

    0
  • Zendesk API User
    Author: mbergmann - 9/26/2018 11:25

    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
  • Zendesk API User
    Author: sivaprasad9394 - 9/27/2018 6:23

    Hello Michael,

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

    $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>

    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
  • Zendesk API User
    Author: mbergmann - 9/27/2018 6:35

    Hi Siva,

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

    Michael

    0
  • Zendesk API User
    Author: sivaprasad9394 - 9/27/2018 7:59

    Hi Michael,

    Listed down Generated source and fs5staging content for your reference,

    Generated source code(HTML):

    ?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:

    <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
  • Zendesk API User
    Author: mbergmann - 9/27/2018 11:05

    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
  • Zendesk API User
    Author: mikula - 10/15/2018 18:42

    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
  • Zendesk API User
    Author: sivaprasad9394 - 10/25/2018 11:22

    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:

    output in outlook looks like below:

    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
  • Zendesk API User
    Author: mbergmann - 10/26/2018 11:40

    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
  • Zendesk API User
    Author: sivaprasad9394 - 10/29/2018 3:20

    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:

    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

Bitte melden Sie sich an, um einen Kommentar zu hinterlassen.