#native_company# #native_desc#

How to build a mailbot in PHP

By Michael Galloway
on November 6, 2002

The fascination with having software automate your mundane tasks never ceases. One such tedious activity lies in email parsing. Email accounts can be a useful tool for enabling people to enact a request or issue a command that may not require the intervention of humans. One such example is the, now all to common, “unsubscribe” request to harmless lists and nefarious spammers. The automated program that handles these requests is called a mailbot. Though it was adopted early, and has been retired in many cases in favor of web-interfaces, mailbots still represent a good technique for performing a number of useful tasks. Nearly anything can be done with enough creativity, good code, and patience.
First lets talk about the environment I have set up:
  • Linux (Redhat 7.3)
  • CGI version of PHP 4.2.1
  • Sendmail 8.12.5
The setup is very standard. I haven’t done anything fancy to the system or the programs. Everything you need to know I’ll explain here.
Let me give you an understanding of the process we are about to go through. When mail comes in it is received by Sendmail and diverted to the appropriate user’s inbox (or offsite email address). Sendmail will check the user account to look for a .forward file. If one is found, Sendmail will deliver the mail to each recipient in the file (usually separated by newlines, but commas should work as well). This is where the mailbot gets called. The mail is “delivered” to the mailbot, which reads it in from stdin, parses the header of the mail, and then determines a course of action.
Sounds simple? It actually is. There are only a few catches that I’ll guide you through. So let’s get to it.