How to quickly extract emails from your mailbox.

Hi, I have a mailbox configured on my mac to fetch mails from all my account. I today search net for quickly build an email list, and had found a lot of paid solution, being a programmer myself I tried to figure out ways to programmatically do it and here follow the suit to quickly build your mail list.

On Mac, got to

cd ~/Library/Mail/V5/
tree -L 2

You will see a lot of folders like.

[email protected]  7 user  staff   224 Sep 30 06:09 AE6B5820-C738-42E9-B8C4-6BFE440C36FA
 [email protected] 22 user  staff   704 Sep 30 06:09 D3B029AA-8EA8-4784-A9D7-298099CD1113
 [email protected] 14 user  staff   448 Oct  2 20:50 D57E382D-9852-41B8-AA47-E68E811A51D7
 [email protected]  6 user  staff   192 Oct  2 20:50 F7D35325-3867-40A3-BAF7-544B6FF9540F
 [email protected]  5 user  staff   160 Sep 30 06:09 F9414529-5EBC-4511-A2B5-F217C39478F1
 [email protected]  4 user  staff   128 Sep 30 06:09 FD11C773-496E-4E56-B92C-7322F3ABB32D 

Yours will be different, Iterate to one directory at a time and build your mail list.

Run the following command in each directory and see the magic happens.

cd AE6B5820-C738-42E9-B8C4-6BFE440C36FA # Yours will be different
grep -hR . . | perl -MMIME::QuotedPrint -pe '$_=MIME::QuotedPrint::decode($_)' | egrep -oi "\b[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,6}\b" | rev | sort -u | rev > ~/Desktop/mails.txt

The same thing can be done using python as below.

import imaplib, email
 
def split_addrs(s):
    #split an address list into list of tuples of (name,address)
    if not(s): return []
    outQ = True
    cut = -1
    res = []
    for i in range(len(s)):
        if s[i]=='"': outQ = not(outQ)
        if outQ and s[i]==',':
            res.append(email.utils.parseaddr(s[cut+1:i]))
            cut=i
    res.append(email.utils.parseaddr(s[cut+1:i+1]))
    return res
 
mail=imaplib.IMAP4_SSL('imap.gmail.com')
mail.login(userid,password)
mail.select("INBOX")
result,data=mail.search(None,"ALL")
ids=data[0].split()
msgs = mail.fetch(','.join(ids),'(BODY.PEEK[HEADER])')[1][0::2]
addr=[]
for x,msg in msgs:
    msgobj = email.message_from_string(msg)
    addr.extend(split_addrs(msgobj['to']))
    addr.extend(split_addrs(msgobj['from']))
    addr.extend(split_addrs(msgobj['cc']))

Your addresses are now in the list addr in the form:
[(‘Jerry Neumann’,’[email protected]’),
 (…,…),…]

If you want just unique addresses, then addr=set(addr).

this same process will work for Linux, please make sure you are using Maildir format for storing your messages.

Another quick method is to use gmail’s export contact feature.

Steps:

1. Click the Gmail drop-down menu in the navigation pane on the left after logging in to your account. Select Contacts.

2. Click the More drop-down menu above your contacts and select Export.

3. Click Go to Old Version (in case it displays the dialog box). It’ll redirect to the older version of Google Contacts. Click on More drop-down. Click Export.

4. Click The Group and select the group you wish to extract from the drop-down menu. Alternatively, click ‘All Contacts’ to extract all contacts.

6. Click Outlook CSV Format or vCard Format if you plan to use the list outside of Gmail. Otherwise, the default Google CSV Format allows you to import it into another Gmail account.

5. Click Export.

6. Click Save File, select OK, choose a location where you want to save the file and give the file a name. Click Save to export the file.

Published by Om Prakash Singh

DevOps Engineer experienced in Architecting, Automating and Optimizing large infrastructure, Proficient in configuration management tools, and in developing CI/CD pipelines.