26

One of my favourite features of Gmail is the ability to bookmark urls to certain messages like this:

https://mail.google.com/mail/#all/124c8f386d41fd3a

What I would like to do is write a script that accesses my Gmail account via IMAP and creates an HTML page with links like the above to each message I am interested in.

However, it seems there is no way to find the "124c8f386d41fd3a" ID from the IMAP envelope or UUID properties. All the message-ids and uuids I am finding are of a different format and can't be used to produce valid links to my inbox.

Does anybody know how to find those url-IDs in IMAP?

-- Felix Geisendörfer aka the_undefined

PS: I am using Ruby and my previous attempts included:

imap.fetch(message_id, "UID")
imap.fetch(message_id, "ENVELOPE")
imap.fetch(message_id, ...)

I tried all properties listed for FetchData in the ruby imap docs

Felix Geisendörfer
  • 2,902
  • 5
  • 27
  • 36
  • Update: I'm giving a $25 Amazon Gift Certificate to anybody who knows the answer to this! – Felix Geisendörfer Nov 06 '09 at 14:40
  • Hi I was wondering if you foudn a solution to this issue - I'm in a similar mess and would like any ideas you may have.. basically I need some kind of method to uniquely identify emails from an application and be able to query them based upon that method. – Ali Mar 24 '10 at 13:32
  • nope, I didn't get further with this - sorry. – Felix Geisendörfer Mar 27 '10 at 13:12
  • Heya, posted the answer down below if you're still looking for it. I spent two days just staring at these numbers until it hit me :) – jamie-wilson May 28 '11 at 04:37
  • I think it is possible to do it via IMAP (and that the accepted answer is incorrect). See my answer below. – kkurian Feb 03 '12 at 17:29

9 Answers9

18

Gmail provides an attribute: X-GM-THRID for thread id.

You can use imap fetch function for getting the thread id.

Also see the documentation here.

Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
Vivek Goel
  • 22,942
  • 29
  • 114
  • 186
11

Found something. May be someone need. I don't know how to use ruby, I want use php, but don't know how to extend standart imap functions in php.

openssl s_client -crlf -connect imap.gmail.com:993
. login username password
. select inbox
. FETCH 1 (X-GM-THRID)

you'll get something like this * 1 FETCH (X-GM-THRID 1327644190303473294) next you need to convert it from decimal to hexadecimal value:

<?php echo dechex(1327644190303473294); ?> //return 126cbd5b5f264e8e
Shkur
  • 379
  • 3
  • 10
  • how to use FETCH 1 (X-GM-THRID) command from php imap functions? anybody know? – Shkur Apr 18 '11 at 19:06
  • 1
    [zend imap supports custom command and fetch](http://stackoverflow.com/questions/5644552/custom-imap-command-in-php/5673600#5673600) – Shkur Apr 18 '11 at 19:12
  • 1
    This is the correct answer. Gmail uses decimals internally but in the browser it uses hexadecimals. – Marek Stój Mar 04 '12 at 12:18
  • [here you can find some about howto find Gmail url-IDs via IMAP php](http://mrshkur.blogspot.com/2011/09/usrbinpythonprint-hello.html) – Shkur Mar 15 '12 at 08:28
6

Seems that the google link (https://mail.google.com/mail/#all/124c8f386d41fd3a) points to the whole conversation.

IMAP itself does not have such feature (grouping conversations)

[Update]

It is possible with X-GM-THRID Gmail's IMAP extension.

124c8f386d41fd3a is Gmail's thread-id in hex.

You can read more here: http://www.limilabs.com/blog/create-gmail-url-id-via-imap

Pawel Lesnikowski
  • 6,264
  • 4
  • 38
  • 42
  • I'm not married to IMAP, but I do need some sort of way I can automate searching through my entire inbox and link to the matching items. – Felix Geisendörfer Nov 06 '09 at 15:20
  • You would think that maybe all the mail items might have the same thread id. – reconbot Nov 06 '09 at 15:23
  • OP mentioned trying Ruby, unfortunately Ruby Net::IMAP throws exception when parsing a response containing this attribute. I worked around this by patching Ruby's NET::Imap library: https://gist.github.com/1404434 – Wojtek Kruszewski Nov 29 '11 at 11:36
3

This seems to be something internal to GMail's web UI. I can imagine a workaround like this:

  • log in to GMail using the basic HTML mode, and grab the session cookie
  • use curl, wget or anything similar with this session cookie to fetch the page

    https://mail.google.com/mail/h?s=q&q=2AE41111.1234123@gmail.com

where the thing after the 'q=' part is the Message-ID of the e-mail from IMAP.

Now you can scrape the "GMail ID" of the message you need from the HTML, search for a link with a target URL that looks like this:

?v=c&s=q&q=2AE41111.1234123%40gmail.com&th=124ae57b77769275

The part after the 'th' is what you need.

Nasty, probably very inefficient, but this may very well be the closest you get to a solution.

If you are not that desperate, you can use the search URL, which, in its most simple form, and using the standard UI, looks like this:

https://mail.google.com/mail/#search/2AE41111.1234123@gmail.com

The last part is the value of the Message-ID header field again. This way, you get a single search result, but you still have to click on it to view.

AttishOculus
  • 1,439
  • 1
  • 11
  • 18
  • Ok, the search for message-id is somewhat acceptable. E-Mail /PM me and I'll send you a $15 gift cert for the cleanest partial solution, ok? – Felix Geisendörfer Nov 11 '09 at 14:05
  • 1
    Note you actually need to use the ```rfc822msgid:``` keyword in the search. So the actual URL would be ```https://mail.google.com/mail/u/0/#search/rfc822msgid%3A2AE41111.1234123@gmail.com```. – studgeek Jun 02 '13 at 02:56
2

I think the accepted answer is incorrect (at this point, maybe it was right at the time).

If you look at the atom feed (https://gmail.google.com/gmail/feed/atom), you'll see that the entries look like this:

http://mail.google.com/mail?account_id=[EMAIL_ADDRESS]&message_id=1353f6fb621714da&view=conv&extsrc=atom

The message_id is probably the X-GM-MSGID in hex. You can retrieve the X-GM-MSGID via IMAP, so you ought to be able to contruct the URLs you want via IMAP, without using the atom feed.

kkurian
  • 3,844
  • 3
  • 30
  • 49
1

I use a Mac menu bar app called Notify which shows me new GMail messages, which I can double-click to be taken to them on the GMail web site. Both IMAP and POP are disabled in my GMail settings, so therein may be the solution.

The URL that takes me to the message looks like this:

http://mail.google.com/mail/?fs=1&source=atom#all/124fb7xxxxx06752

(somewhat redacted in case it's personal)

I wonder if source=atom could be helpful to you, since this application seems to have access to the ID you are looking for.

carillonator
  • 4,715
  • 3
  • 29
  • 39
1

It's been a while but anyone who finds themselves looking for this information should read this:

Gmail provides a unique message ID for each email so that a unique message may be identified across multiple folders. Retrieval of this message ID is supported via the X-GM-MSGID attribute on the FETCH command. The message ID is a 64-bit unsigned integer and is the decimal equivalent for the ID hex string used in the web interface and the Gmail API.

The following is an example transcript of a call to retrieve the X-GM-MSGID of a message with the FETCH command:

a006 FETCH 1 (X-GM-MSGID)
* 1 FETCH (X-GM-MSGID 1278455344230334865) a006 OK FETCH (Success)

The X-GM-MSGID attribute may also be used in the SEARCH or UID SEARCH commands to find the sequence numbers or UID of a message given Gmail's message ID. The following is an example transcript of a call to retrieve the UID of a message using the UID SEARCH command:

 a007 UID SEARCH X-GM-MSGID 1278455344230334865
 * SEARCH 1 a007 OK SEARCH (Success)

the above is an extract from the following source: https://developers.google.com/gmail/imap_extensions?csw=1#access_to_the_gmail_unique_message_id_x-gm-msgid

Ciaran
  • 169
  • 2
  • 8
0

I was struggling with this too, and then i found the UID is actually the same thing as the Google Message ID.

The UID is an Integer and the Google Message ID is the Hexadecimal version of the that.

Unsure about conversion in ruby, but try this: Converting an integer to a hexadecimal string in Ruby

Community
  • 1
  • 1
jamie-wilson
  • 1,925
  • 21
  • 38
-2

Actually, the only official method for getting a direct link to a message is through the atom feed gmail provides for unread messages (https://gmail.google.com/gmail/feed/atom)... The only difficulty is that you have to authenticate, which is not so common using feeds, and there's currently a limitation of like 15 new messages, so any newer message will "kick out" the oldest! I hope they'll soon provide it in some other way, be it through IMAP or API...

Takhion
  • 36
  • 1