In order to create a link to an email, I do a search query with the specific RFC822 MSGID of the email message I would like to link to.
In gmail, this can be done by putting the following into the search field:
Rfc822msgid:<CA+74-1MkR-JbPCBzaGLz4=_GEcKj-TbG14Y0+S-ojpt5ZjzDfw@mail.gmail.com>
The message/thread that matches/contains this message ID will appear as the only search result.
The link that I used to do this for the above example msgid is:
https://mail.google.com/mail/u/0/#search/Rfc822msgid%3A%3CCA+74-1MkR-JbPCBzaGLz4=_GEcKj-TbG14Y0+S-ojpt5ZjzDfw@mail.gmail.com%3E
The problem is, that when I open this link in a web browser, although the link in the web browser is obviously identical to the one in the URL, the msgID that is placed into Gmail's search bar is different.
This is what appears in Gmail's search bar
Rfc822msgid:<CA 74-1MkR-JbPCBzaGLz4=_GEcKj-TbG14Y0 S-ojpt5ZjzDfw@mail.gmail.com>
Notice how the + signs disappeared compared to the original msgID.
This is a problem, because the link no longer works, if there is a + sign in the msgID.
I realized that the URL uses ASCII codes. The ASCII code for + is 2B and therefore, the URL should contain %2B wherever there is a + sign.
So the correct link should be:
https://mail.google.com/mail/u/0/#search/Rfc822msgid%3A%3CCA%2B74-1MkR-JbPCBzaGLz4=_GEcKj-TbG14Y0%2BS-ojpt5ZjzDfw@mail.gmail.com%3E
Now, of course, I could very simply do
url = url.replace('+', '%2B')
However, I imagine there could be other characters that I haven't discovered yet that also needs to be replaced with its ascii equivalent.
I was wondering if someone would know of a sure way to encode these URLs in python?
Here is an article from Google about URL encoding and it actually explains why I was seeing a space where I had a + sign. Is there some library I can call?