views:

176

answers:

4

Alright, this may take a moment or two to explain:

I'm working on creating an Email<>SMS Bridge (like Teleflip). I have a few set parameters to work in:

  • Dreamhost Webhosting
  • PHP 5 (without PEAR)
  • Postfix
  • MySQL (If Needed)

What I have right now, is a catch-all email address that forwards the email sent to a shell account. The shell account in turn forwards it to my PHP script.

The PHP script reads it, strips a few Email Headers in an effort to make sure it sends properly, then forwards it to the number specified as the recipient. [email protected] of course sends an SMS to +1 (555) 123-4567.

This works really well, as I am parsing the To field and grabbing just the email address it is sending to. However, what I realized that I did not account for is multiple recipients. For example, an email sent to both 5551234567 and 1235554567 (using the To line, the CC line, or any combination of those).

The way email works of course, is I get two emails received, end up parsing each of them separately, and 5551234567 ends up getting the same message twice.

What is the best way to handle this situation, so that each number specified in TO and CC can get one copy of the message.

In addition, though I doubt its possible: Is there a way to handle BCC the same way?

A: 

Stupid dirty solution: parse all recipients from the mail, then send them SMS, then put em all into temporary table with md5 of message text. And check all incoming mails against this table.

Col. Shrapnel
+1  A: 

Why not use something like imap to check the catch-all mailbox, loop through the messages and then delete them once finished? That way you don't need to forward them to a seperate account.

fire
This is what I've done with similar projects. The downside here is that I have to use bandwidth connecting and scrolling through IMAP, which is a lot dirtier, as well as having a cronjob set up to poll through IMAP. It also makes forwarding the emails dirtier (or my lack of knowledge). The way I have it setup now, it pushes the email directly to the script, and can be sent immediately upon receiving it.
Navarr
+2  A: 

If you check the headers of the mail, you should find a Message-ID field (according to RFC2822 - section 3.6.4). So you could test if you have already sent an SMS for a mail with the same Message-ID & phone number to prevent sending the same message to the same number twice.

wimvds
Message-ID is consistently static right?So lets say I have an email with two senders in the TO field and three in the CC field - I could store that Message-ID as a unique key in MySQL, then when i get the other four emails, i check to see if that ID is already in the database, and if it is I can just discard them?
Navarr
According to the RFC it should uniquely match one mail message, thus it should be consistently static and you could indeed discard copies with the same Message-ID.
wimvds
I just ran a simple test and it seems to be the case! Thank you.Now if only i could make BCC work, though I doubt that could ever happen because, well, its BCC.
Navarr
A: 

Although wimvds had the best answer here, I found out elsewhere that Dreamhost includes a "X-DH-Original-To" header in the way I'm running it through the system. Using this, I'm able to send to each number individually upon receipt of the email without checking it against a database. This should also work with Blind Carbon Copy (I don't know the specifics of how email works enough to tell you how that works).

Navarr