What you need is called a "pipe" to send the emails to a script. What you do with the script is up to you. Here are a couple of links that might be helpful:
Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.