Find Gmail url-IDs via IMAP
Asked Answered
I

9

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

Indefinite answered 6/11, 2009 at 12:59 Comment(5)
Update: I'm giving a $25 Amazon Gift Certificate to anybody who knows the answer to this! </bribe>Peonage
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.Declinature
nope, I didn't get further with this - sorry.Peonage
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 :)Gatling
I think it is possible to do it via IMAP (and that the accepted answer is incorrect). See my answer below.Bedrabble
A
-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...

Arun answered 27/7, 2010 at 12:23 Comment(1)
I guess that's the correct answer, unfortunately : ). I was hoping for a way to do it via IMAP.Peonage
H
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.

Hermia answered 12/4, 2011 at 4:54 Comment(2)
You seem to have experience with IMAP, wanted to get in touch for a project, are you available?Llamas
@3zzy Check your email.Hermia
E
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
Eme answered 18/4, 2011 at 7:44 Comment(4)
how to use FETCH 1 (X-GM-THRID) command from php imap functions? anybody know?Eme
zend imap supports custom command and fetchEme
This is the correct answer. Gmail uses decimals internally but in the browser it uses hexadecimals.Xeres
here you can find some about howto find Gmail url-IDs via IMAP phpEme
P
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

Preliminary answered 6/11, 2009 at 14:38 Comment(3)
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.Peonage
You would think that maybe all the mail items might have the same thread id.Cinderella
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: gist.github.com/1404434Silly
M
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&[email protected]

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/[email protected]

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.

Moralist answered 9/11, 2009 at 17:0 Comment(2)
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?Peonage
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%[email protected].Keeleykeelhaul
B
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.

Bedrabble answered 2/2, 2012 at 19:32 Comment(2)
Unfortunately I think the message_id in this URL is actually X-GM-MSGID rather than the IMAP/RFC822 ID.Keeleykeelhaul
@studgeek: Yep, that's what I said.Bedrabble
P
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.

Paralytic answered 16/11, 2009 at 5:22 Comment(0)
C
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

Clapboard answered 19/9, 2014 at 12:4 Comment(0)
G
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

Gatling answered 28/5, 2011 at 4:36 Comment(0)
A
-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...

Arun answered 27/7, 2010 at 12:23 Comment(1)
I guess that's the correct answer, unfortunately : ). I was hoping for a way to do it via IMAP.Peonage

© 2022 - 2024 — McMap. All rights reserved.