tags:

views:

42

answers:

4

I'm in the process of writing a small php-cli script that will loop over my personal inbox and then send me an SMS via a gateway.

The question I have is:

As will have the script launch via cron every 10 minutes, if there is an email sitting in my inbox that is not read before the next script launch then I will receive 2 sms.

Does any one (pseudocode will do) have any idea what the best practice would be in php5 to ensure only 1 SMS is sent?

What I am currently learning towards is storing the message ID in a sqlite DB and flagging a field whether an SMS has been sent or not - but wondering if there is an easier way?

A: 

What I am currently learning towards is storing the message ID in a sqlite DB and flagging a field whether an SMS has been sent or not

This is one of the most simple and straightforward ways to do this. Regardless of storage method, you'll have to keep the list of seen-but-unread message IDs somewhere. May as well be in a simple, self-contained SQLite file. I suppose you could just keep them in a list in a flat file. That is by definition more simple, but is it really easier?

Charles
A: 

I don't see you getting away without persisting something somewhere.

That something should be of ever-incrementing nature - message ID maybe, timestamp for sure. As long as you can look at it and determine if new messages have arrived since.

If all you wish to communicate with the SMS is that there are unread messages, I don't see the need to persist more then a single ID or a timestamp.

Lauri Lehtinen
A: 

After a few hours sleep (up since 4am watching Australia vs Germany, World Cup) I've come up with an even better method, a bit more complex then just using a sqlite DB but has some extra benefits.

I'm using imap_mail to send the SMS to the gateway, and storing the message_id that the SMS is notifying to as a custom "X-Relationship:" header in the outgoing email and then copying the message to my Sent Items.

Then the notifier simply scans my Sent Items for the presence of the X-Relationship header within my Sent Items.

This what I have a record (Sent Item) of the email sent to the SMS Gateway - seems to be working well.

Michael Pasqualone
+1  A: 

You shouldn't need to store anything. If I understand you correctly, you simply want your program to send you a message if a new, unread email has arrived within the last 10 minutes. In that case, could you not do the following?

foreach($inbox[] as $message){
    if(($message.isUnread) && ($message.receiveTime > NOW() - 10 minutes)){
        $sendMessage = TRUE;
    }
}

if($sendMessage){
    sendMessage();
}

I don't know how you're able to access the data, so the above is unlikely to be usable as-is. Assuming you can filter your inbox's contents by read status and date arrived, however, shouldn't that work in principle?

DeathMagus