Basically, here is my code to extract and print all the email headers of 100+ emails in an .mbox file.
import mailbox
import pprint
f=open("results.txt","w")
mbox = mailbox.mbox('c:\documents and settings\student\desktop\mail\mailall.mbox')
for msg in mbox:
pprint.pprint(msg._headers, stream=f)
f.close()
I want to count how many emails have the header "To: [email protected]". (In other words, count how many emails were sent to the address [email protected]).
If i type count=0 outside the loop, and put count++ inside the loop, all that does is count how many times the code is repeating. How do i do this?
I'm guessing you can access the email address the message is sent to using the
msg
variable so you could create a method that would do that matching for you and only increment and write the headers in theresults.txt
file in that case: