Prefilling gmail compose screen with HTML text

Check that your UrlEncode method really translates newlines into "%0a". Here's an example of a 2-line email body:

https://mail.google.com/mail/?view=cm&ui=2&tf=0&fs=1&to=WHOEVER%40COMPANY.COM&su=SUBJECTHERE&body=LINE1%0aLINE2


I used standard javascript encodeURIComponent() for encoding multiline body. It worked.

Also those who want to prepopulate generic gmail and not app domain specific account, use this URL instead:

https://mail.google.com/?view=cm&fs=1&tf=1&....


Gmail has support for what HTML5's registerProtocolHandler() emits.

Example:

var compose = "https://mail.google.com/mail/?extsrc=mailto&url=" + encodeURIComponent("mailto:?subject=test");

That's what Gmail expects you to use. Modify the beginning for the domain-specific compose URI of course.

However, that loads the compose form by itself (not integrated with the inbox page). So, if you want the compose form integrated into the inbox page, you need to load:

"https://mail.google.com/mail/?compose=1&view=cm&fs=1&to=1&su=2&body=3&cc=4&bcc=5"

instead. But, that requires you to parse the mailto URI first to get the hfvalues and fix their percent-encoding to make sure they're fit to send in an HTTP URI. See below for more info on that.

Now, what Gmail does with the HTML5 compose URI is percent-decode the url param to get the mailto URI. Then, it parses the mailto URI to get the hfvalues. Then, it uses those hfvalues to build a URI like the inbox-integrated one and redirects you to it.

The problem with Gmail's HTML5 method is that it doesn't percent-encode '+' characters in the hfvalues to "%2B". The end result of this is that '+' characters in the mailto URI (they're not spaces in a mailto URI) come out as spaces in the Gmail compose form.

To work around the Gmail bug, you just do:

var compose = "https://mail.google.com/mail/?extsrc=mailto&url=" + encodeURIComponent("mailto:?subject=test".replace(/\+/g, "%2B"));

instead.

The bug was reported years ago, but Gmail refuses to fix it.

As you should also see, for the inbox-integrated compose URI, if you had the mailto URI "mailto:?subject=1+2", you need to make sure that you emit su=1%2B2 in the compose URI and not su=1+2. The latter will cause a space to be in the subject field instead of '+'. This part isn't a bug with Gmail. That's just how HTTP works.

You can see the source of my Gmail Compose Extension for Opera (unzip it) to see how I do the HTML5 way. It's very simple. But, it doesn't contain the + to %2B workaround though.

However, you can see the newer version of the extension that I'm testing (just need someone to test the domain-specific option in preferences) for more advanced handling. This one uses my custom, generic mailto URI parser to normalize the mailto URI and its hfvalues to handle the '+' case, unsafe characters and duplicates hfvalues. It also offers an option to choose whether you want to use the HTML5 compose URI or not.

You can also take a look at this User JS script for Opera for how to do things.

For the duplicate hfvalue issue mentioned earlier and other mailto URI stuff, see my mailto URI spec, which was used for research and feedback for RFC6068.

Note: Just because the Gmail user has rich text editing turned on, that doesn't mean that Gmail will accept HTML markup in the compose URI and treat it as such. It's all interpreted as text.