I am designing a program that will run on a mailserver. It is intended to monitor email sent to a particular username and act on input received through the email messages.
My idea is to run this program from a cron job every X minutes, check for new email, act on the email if it’s present, and delete the email.
Of course, I could easily open and read /var/spool/mail/username directly as a regular text file, then truncate the file once I’ve read through it. But what’s the proper way deal with the situation without stepping on sendmail? Another email might show up for that user either while I’m still reading the file or while I’m truncating it.
Generally what you’re trying to do is better accomplished through server-side filtering as the mail arrives rather than trying to search through a mailbox every so often. It’s complex and if you get it wrong, you end up losing mail.
Instead, look to server side filtering like procmail or similar to accomplish what you want.