Two solutions here:
- Google Drive Spreadsheet - Continue with using the speadsheet as linked, but limit the search area to avoid hitting the "Exceeded maximum execution time".
- Download via IMAP
1. Google Drive Spreadsheet
It's possible via a Google Drive Spreadsheet.
The original credit for Regular Expression based search of Gmail emails goes to Labnol.org.
Step by Step
Here are the step by step instructions from the original page modified to match the original questions requirements:
- Click here to make a copy of the Gmail RegEx sheet into your Google Docs account.
- Wait for 10-15 seconds and a new
Gmail RegEx
menu will appear in your new Google sheet.
- Choose
Initialize
from the menu and grant the necessary permissions as requested by the program.
- The program will search your entire mailbox by default but if you would like to limit the search to any particular label (say
Inbox
or Spam
), just put that label name in cell F3
. In this case if you sent the email to yourself Sent
might be the best label, see further notes below
- Now enter any regular expression in the cell
F4
. The regular expression you are looking for is ([A-Za-z0-9]{5}(?:-[A-Za-z0-9]{5}){4})
- Choose
Search Mailbox
from the Gmail RegEx menu to begin searching.
- Wait and wait some more.
In my test case the first result took over 4 minutes to retrieve.
Exceeded maximum execution time
You may experience the Exceeded maximum execution time
error.
In this case I would limit your search by creating a custom label covering a specific time period in GMail before commencing the search.
I.e:
- Open Gmail
- Search between 2012/01/01 (Jan 1st) and 2012/04/01 (April 1st) in
Sent
.
- Mark all emails with label
searchblock1
.
This alternative label (searchblock1
) should be entered in cell F3
instead of Sent
.
Other limits
The original article dated July 2012 also notes (although limits might have changed):
Please do note that Google Apps Script allows you to perform 10,000
read operations in Gmail per day.
Multiple Google Accounts
Also note that looking at the code this only searches the "primary" user account i.e. https://mail.google.com/mail/u/0/
if you are using multiple Google accounts you may want to change this to https://mail.google.com/mail/u/1/
etc.
2. Download via IMAP
You will need to ensure IMAP support is enabled for your GMail account in settings for these.
- Use a IMAP client that supports regular expression search
- OR IMAP backup tool to download as a ".mbox" file and search the plain text output locally.
Not tested but Thunderbird add-on Expression Search / GMailUI says it supports regex searches.
And for IMAP backup the first one that comes up is IMAP Backup Tool.
Either of these could be used with a limited search marked by initially using Gmail to label a range of interesting emails (say by date and sender) and then only downloading / searching those locally via the IMAP folder / label.