Search code examples
javascripthtmlemailmailto

How can I create a mailTo link whos body contains a link containing escape values


I have an api who expects the format:

/api/download/file%20name/file%2fpath

See the spaces and \ are escaped as %20 and %2f

When I create a maitTo link:

mailto:?subject=file%20name&body="example.com/api/download/file%20name/file%2fpath

the client (My requirement is outlook only) escapes these values when it creates the email replacing the escaped values in the url with true spaces and slashes, therefore invalidating the url.

End up with this example.com/api/download/file name/file/path as the body

Is there anyway to prevent this by encoding the mailto differently? I don't have a way to edit the apis format at the moment.


Solution

  • use encodeURIComponent to pass values in a url.

    var url = 'mailto:'+
        '?subject='+encodeURIComponent('file name')+
        '&body='+encodeURIComponent('example.com/api/download/file name/file/path')