Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Can someone tell me how I can do the following:

My company built its own project management system with Linux, Apache, MySQL and PHP. In the web application's database, there is a table called t_project that stores information on all the company's projects. My boss wants me to devise a solution where if someone sends an email to (t_project.project_id)@mysite.com, the web application will write the email to a database table called t_project_message.

How do I solve this problem? What do I need to learn?

Additional notes I just noticed that the text in my question dropped "(t_project.project_id)@mysite.com" which I've added back. So does this mean i will need as many email accounts as there are projects in my database? That could be tens of thousands...

share|improve this question

4 Answers 4

up vote 2 down vote accepted

What email server are you running on the PHP host? For several PHP applications I am responsible for I use Exim, and with Exim I setup a transport&router. This means Exim will accept the message and then it will run a PHP script and pass the message to the PHP script STDIN. The PHP script then does things based on the contents of the message.

The below router/config will accept messages to helpdesk@helpdesk.example.org, helpdesk-bounce@helpdesk.example.org, and tckt-{id#}@helpdesk.example.org. With Exim the transport and router configuration is extremely flexible, the below is just one possible way that you might configure this. I doubt what I have configured would apply directly to your project, you probably would need to read up on Exim. I am also pretty sure other mail servers are also able to do the same thing. I am just not familiar with them.

### router/050_helpdesk_router
#################################
helpdesk_router:
  driver = accept
  domains = helpdesk.example.org
  caseful_local_part = true
  local_parts = lsearch;/etc/exim4/helpdesk
  local_part_suffix_optional
  local_part_suffix = -*
  transport = helpdesk_transport


### transport/20_helpdesk_router
#################################

helpdesk_transport:
  driver = pipe
  command = /usr/bin/php ${lookup{$local_part}lsearch{/etc/exim4/helpdesk}} $local_part '${if def:local_part_suffix {$local_part_suffix}}'
  # return the data from the script on a failure
  return_fail_output = true
  # temporary error
  temp_errors = 10
  # set a timeout for 30 seconds
  timeout = 30s
  current_directory = /home/helpdeskadmin/
  home_directory = /home/helpdeskadmin/
  user = helpdeskadmin
  group = helpdeskadmin

### /etc/exim4/helpdesk
#################################
helpdesk:   /srv/www/helpdesk.example.org/www/helpdesk/mail/recv.php
tckt:       /srv/www/helpdesk.example.org/www/helpdesk/mail/recv.php

To give a starting point on the mail parsing portion of the question. I use Zend_Mail for parsing the incoming message, and sending mail.

Would you have time to also tell me advantages/disadvantages of your method vs. Ben's method?

With a hook into the MTA like I describe.

  • There is no polling inteval. When the sending mail server connects your process will immediatly be started to accept the message. With a mailbox and polling you will have to wait until the next polling cycle.
  • You can reject or respond to invalid messages immediatly and not risk the potential of your application being a source of back-scatter. Perhaps you may want to bounce the message if the address is not quite right.
  • You said you had lots of projects. With a transport you don't have to create lots of mailboxes, and poll each mailbox seperately, if you define your transport properly you can get one transport configuration to handle everything.

Typically with PHP one does mailbox polling from cron. From cron you would have to be very careful to make sure that your polling does some form of locking so that you don't respond to or process the same message twice.

share|improve this answer
    
Thanks Zoredache, I had a feeling that maybe I should head in a direction similar to this one. I've never hosted my own mail server before and have been using google mail server all this time. So if I start now, I could use Exim. Would you ahve time to also tell me advantages/disadvantages of your method vs. Ben's method? –  John Nov 19 '10 at 3:29

The way I've done this frequently in the past is to run fetchmail every X minutes via cron. Fetchmail will retrieve the mail via IMAP or POP3 and can then pipe the mail through a handler/parser script of some type (usually written in perl, python, etc.). The parser script would pull out any data you need out of the message and then insert that information into the appropriate places in your database.

share|improve this answer
    
Hi ErikA, I had to edit my question and provide more details.. –  John Nov 19 '10 at 1:01

Dependant on your mail server, you could write a PHP script that will poll a mailbox on the server with IMAP and take actions accordingly.

There is an IMAP extension available for PHP which can get email messages from the server, as well as move them around on the server or delete them completely when you've committed it to the database.

You can schedule this as appropriate with cron.

share|improve this answer
    
I just noticed that the text in my question dropped "(t_project.project_id)@mysite.com" which I've added back. So does this mean i will need as many email accounts as there are projects in my database? That could be tens of thousands... –  John Nov 19 '10 at 1:01
1  
I don't know if it's appropriate for your project, but have you considered plus@ email addresses (sorry don't know the real name)? i.e 123+projects@domain.com where 123 is the project id. –  Ben Pilbrow Nov 19 '10 at 1:07
1  
Tens of thousands of email accounts really isn't anything to bat an eyelash at, especially if your application deletes messages from the inbox when it's done with them. The real burden is in message volume, and I can't imagine an application like that would be receiving the mail volume of a standard inbox by a long shot. –  jgoldschrafe Nov 19 '10 at 2:29

While you could poll a POP mailbox its a very inefficient solution. Assunming you can route the mail to the target system, a better solution is to handle the request sychronously. While you could just use the .forward mechanism, I'd recommend using procmail as the MDA - it allows you to script complex behaviours like running programs, forward copies, conditional replies and more.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.