I use a script to do basically that. While it's not directly shareable on superuser, here are the basic steps it performs:
- Connect to the server through IMAP¹
SELECT a given folder²
SEARCH emails from a given range ((SINCE "<date>") (NOT DELETED))
FETCH for each email³ the fields (RFC822.SIZE BODY.PEEK[HEADER.FIELDS (FROM)])
- Group the results by source, summing the sizes
- Sort and print the result
For a one-off run you could even do that manually on a single session (that you are copying to a file for later parsing). Remember that IMAP requires you to prefix every command with an (arbitrary) tag,⁴ and that the reply will have that same tag, but there may be intermixed untagged responses as well.
The session may look like this:
* OK Gimap ready for requests
x1 LOGIN myusername mypassword
x1 OK LOGIN completed.
x2 SELECT INBOX
* 1234 EXISTS
* 11 RECENT
* FLAGS (\Seen \Answered \Flagged \Deleted \Draft)
* OK [PERMANENTFLAGS (\Seen \Answered \Flagged \Deleted \Draft)] Permanent flags
* OK [UNSEEN 1203] Is the first unseen message
* OK [UIDVALIDITY 14] UIDVALIDITY value
* OK [UIDNEXT 65530] The next unique identifier value
x3 OK [READ-WRITE] SELECT completed.
x4 UID FETCH 1:65530 (RFC822.SIZE BODY.PEEK[HEADER.FIELDS (FROM)])
* 1 FETCH (RFC822.SIZE 9999 BODY[HEADER.FIELDS (FROM)] {43}
From: "Jon Postel" <jpostel@example.com>
UID 3522)
- 2 FETCH (RFC822.SIZE 9989 BODY[HEADER.FIELDS (FROM)] {76}
From: "Super user question"
<superuserquestion1826542@invalid-email.com>
UID 7476)
- 3 FETCH (RFC822.SIZE 9889 BODY[HEADER.FIELDS (FROM)] {70}
From: "Stack Exchange"
<question1826542superuser@invalid-email.com>
UID 8084)
...
x5 LOGOUT
Note: The email sizes above are 9999, 9989 and 9889. The UID FETCH on the range 1:65530 is based on UIDNEXT being 65530.
¹ Easiest way in Gmail today would be to use an application password. That's currently only available with 2FA enabled.
² In your case you would be interested in the special "All mails" “folder” of Gmail. Use one with just a few items for testing.
³ Actually in batches of 100 emails.
⁴ x1, x2, x3, x4, x5 on the sample