Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prefilling gmail compose screen with HTML text

I found out that in order to open a Gmail compose screen you'd have to be logged in and open the following link:

https://mail.google.com/a/domain/?view=cm&fs=1&tf=1&source=mailto&to=WHOEVER%40COMPANY.COM&su=SUBJECTHERE&cc=WHOEVER%40COMPANY.COM&bcc=WHOEVER%40COMPANY.COM&body=PREPOPULATEDBODY

Replacing the variables fills in the corresponding places on the compose form. However if I want to enter into the body multiline text or line breaks its just not working even if I urlencode it. Any ideas here?

like image 906
Ali Avatar asked Apr 06 '10 09:04

Ali


2 Answers

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

like image 126
Anthony Faull Avatar answered Oct 12 '22 13:10

Anthony Faull


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.

like image 29
Shadow2531 Avatar answered Oct 12 '22 13:10

Shadow2531