Take the 2-minute tour ×
Web Applications Stack Exchange is a question and answer site for power users of web applications. It's 100% free, no registration required.

I really want to turn off the catch-all option in my Google Apps email service. Before I do that however, I would like to get an idea about the usernames that are actually received mail on a semi-regular basis. That way I can decide which ones I would like to keep (by making an alias) or lose (by just letting it bounce).

Is there a way to extract this information from a Gmail account, preferably without having to copy a significant chunk of my inbox over IMAP?

share|improve this question

3 Answers 3

up vote 4 down vote accepted
+50

Google has a dedicated site, called Data Liberation, from where you can get all the information you need on how to extract the data from their services.

The only way that you can solve your problem is to get somehow the data on your local machine, because there is nothing you can do through the UI that they are offering. For gmail that way is by using IMAP or POP.

share|improve this answer
1  
Your answer made me look at IMAP again with keener interest, and eventually lead to a rather workable solution. For this reason I'm awarding the bounty to you. The solution I ended up using is below. –  drxzcl Mar 17 '12 at 18:45
    
@Ranieri thank you :) I think it's the right way to do it.. –  Lipis Mar 17 '12 at 20:56

This is what I ended up doing.

The suggestion to use the IMAP service is absolutely correct. The trick is not to pull in all your mail, but use the features of IMAP to retrieve only the information you are interested in. In our case, that is the "Delivered-to:" header. The entire process only takes a few minutes on a full gmail account.

import imaplib

# Login to IMAP and get ALL message IDs.
mail  = imaplib.IMAP4_SSL("imap.gmail.com")
mail.login('xxxxxx','yyyyyy')
mail.select("[Gmail]/All Mail")
result,data = mail.search(None,"ALL")
ids = data[0]
id_list = ids.split()

# Retrieve the delivered-to headers in chunks of 100 and output them
while len(id_list)>0:
    nowlist = id_list[:100]
    id_list = id_list[100:]
    result, data = mail.fetch(",".join(nowlist),'(BODY.PEEK[HEADER.FIELDS (DELIVERED-TO)])')
    for dt in data:
        if len(dt) == 2: # We actually have such a header
            if ":" in dt[1]: # And it's not empty
                print dt[1].split(":")[1].strip() # print the address
share|improve this answer
    
your script will unfortunately stop at the first delivered-to field it encounters. Do you know how to fetch the other ones? Without answer here, I will probably have a mail client download all the mails and then grep its files for delivered-to lines. Anyway, after executing the script, it is useful to run sort delivered.txt | uniq -c | sort -n >> delivered_sorted.txt to have all the entries sorted and counted. Of course, one can do it in the python script above but this is something I don't master (even if it does't seem complicated). –  user24372 Sep 12 '12 at 21:17
    
Yes, I did the same sort-and-count operation on the output too. It did not even occur to me that there might be more than one delivered-to header. I think all the information you need is contained in the results, but I can't check at the moment. I would recommend inspecting the "dt" variable for messages with multiple delivered-to: headers. –  drxzcl Oct 27 '12 at 18:45
    
Your last line should be replaced by the following to see all the delivered-to: for r in findall("Delivered-To: {}\r\n", dt[1]): print ''.join(r) –  Patrick Nov 22 at 23:21

You could use the Advanced Search options and search for all mail that does not have your primary email address in the to or cc fields. (You can't filter on bcc unfortunately.)

That way you will get a list of emails within Gmail that you were either:

  • bcc
  • delivered via the catch all rule
  • delivered via an alias

Depending on the number of emails received this may help you.

Use the following search term:

!to:me !cc:me !from:me

share|improve this answer
    
this is really great!! Thanx @Barry –  Ashutosh Dave Mar 16 '12 at 7:13
    
That seems to bring up a lot of mass-mailings. Any tips on how to counteract that? –  drxzcl Mar 16 '12 at 16:02
    
@Ranieri: Unfortunately not. Mass mailings will usually use the bcc field which you are unable to filter on. Sorry –  Barry Mar 16 '12 at 16:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.