Do shell script to download emails to separate files?

I would like to be able to download the last x number of emails sent to an email account, set up on dreamhost or gmail, to separate text files on my local disk. I would like the emails to remain on the email server and not be deleted. I suspect it will take some shell commands, or maybe an OSAX. Any thoughts on if this is possible, or any other tips, are appreciated, thanks!

Why not set up an IMAP account in Apple Mail and then script Apple Mail …

Thanks. Because in my experience Apple Mail has all sorts of problems with its stability, scripting, and usability, as well as some machines not having it set up or using Entourage or Outlook or other. I would like a lower-level solution that doesn’t rely on any of those programs.

these are high expectations. You’ve to start slowly and to project your idea first-i mean- consider carefully the features you really need. Sometimes you’ve to deal with lots of unexpected problems and subproblems to succeed in one of your steps before you’re able to proceed.
Just to start, look at this topic:

Thanks for the link and the good advice!