Using Double Query Strings to Send Mail in Address Bar

Viewed 40

I have a very simple Node.js Video Chat that I am using to call my friends. I want it to interface directly with Mail (open up Mail with a template loaded). I have this so far:

let obsPass = /* helpers.generateObsPassword() < randomly generated char */ 'zH1mA4pR2hF7wJ5'
var text = location.href + '&obs=' + obsPass;
window.open(`mailto:?subject=Join%20meeting&body=${text}`);

It works! The body for this Mail should be https://example.com?room=myroom&obs=zH1mA4pR2hF7wJ5, however, when I use this to open mail, the body doesn't say https://example.com?room=myroom&obs=zH1mA4pR2hF7wJ5, it says https://example.com?room=myroom. I think it has to do with the double query strings that I have (mailto:? and example.com?) which is causing the browser to think that the &obs=zH1mA4pR2hF7wJ5 is being applied to the bigger "website", mailto:.

Is there a fix for this? I could manually send the email, but I am hoping to integrate this into my site. Thanks!

1 Answers

You need to encode the URL in the message body.

Instead of writing

window.open(`mailto:?subject=Join%20meeting&body=${text}`);

write

window.open(`mailto:?subject=Join%20meeting&body=${encodeURIComponent(text)}`);

Without this, your mailto: URL would end up as

mailto:?subject=Join%20meeting&body=https://example.com?room=myroom&obs=zH1mA4pR2hF7wJ5

In a URL query-string, & denotes the end of one parameter value and the start of the next parameter. Hence, this mailto: URL has three parameters in the query string, subject, body and obs, and the value of the body parameter is https://example.com?room=myroom.

If you want the & character after myroom to be part of the value of body, then you need to encode it. Encoding it prevents the URL parser in your mail program from interpreting it as the end of one of the parameters in the mailto: URL. Your mail program should then decode the value it pulls out of the body parameter in your mailto: URL before putting it in the message body of the email.

Related