Email notifier in PHP

March 4th, 2009

I like writing simple and practical scripts that solve real problems. I needed an e-mail monitor for checking new messages with SMS notification. Since SMS messages cost money and I get lots of spams every day, it is very important that I am notified only when an important e-mail arrives.

And the solution:

For sending SMS messages I chose to use SmsBug (smsbug.com) SMS gateway as I am satisfied with its quality and price.
The script includes a class written by me for interacting with SmsBug’s service.
I use PHP IMAP extension to retrieve the list of e-mails. List of e-mails are filtered by the sender’s address. Allowed senders (e-mails from these senders are probably important) are specified by PERL regular expression. Concrete e-mail addresses can be listed or e-mail addresses under a certain domain name. Let us see an example on how to by notified when an e-mail is received from either w3net.eu, cnn.com, or gmail.com domains:

The email’s header is checked for a match with the given regular expression. If matches, the e-mail is considered to be important. I must say that I was lazy to check the sender’s address, I look for matches in the entire e-mail header (maybe in the next version).

Here comes another example. Say we want to be notified when we receive an e-mail from our boss or an e-mail from any gmail account.

Schedule the script

The script always looks for e-mails received yesterday (you can change this). It is sufficient for me to be notified of e-mails received yesterday. I scheduled the script to be executed every day at 10 AM. When I am at work I usually have downloaded my e-mails by 10AM. When the notifier is executed at 10AM, the e-mail box is already empty by that time. But when I am on holiday (or in hospital), e-mails are not downloaded, so the script will find the e-mails and would notify my by SMS message if the e-mail is from an important sender. The SMS message contains only the sender’s e-mail address, time, and subject of the message.

If you wish to monitor your GMail account you need to use the configuration I put between comments. I tested it with my GMail account, it appeared to work fine with.

Almost a year after the first release of the Easy PHP File uploader PHP script I released a new version.
I improved the script to allow multiple file upload. This feature is disabled by default, so you need to enable it. It is not a bug fix release. Those of you who are not interested in this new feature do not need to upgrade their code.

How to enable multiple file upload?

  1. Uncomment the following line in index.php file.
    <a href="#" id="lnkAddNewFileInputBox">more files</a>
  2. Replace value none with decimal in the following line in main.css file.

    
    #fileBoxesLst li {
        list-style-type: none; /* decimal */
    }
    

It is also possible to have a fixed number of file upload input boxes. Remove or comment the "more files" link.
Then add as many file upload boxes as you need. For example:

more-file-input-boxes.png

Links

A hitelesítés (felhasználói autentikáció) lényege, hogy minden
beérkező SOAP lekérésről megállapítjuk, hogy melyik felhasználóhoz tartozik.
Erre azért van szükség, mert nem szeretnénk, hogy bárki hozzáférjen mások adataihoz.

Lássunk három megvalósítási módot.

1. Message-level security

A legegyszerűbb megvalósítás, ha minden SOAP üzenet tartalmazni fogja a jelszót ill. felhasználónevet.
Például:

Input fields for GetSellerPayments call request
Argument Type Meaning
apiKey string
password string
entriesPerPage int

Ennek a megoldásnak a legnagyobb előnye az egyszerű megvalósítás.
Másik előnye, hogy ez a megvalósítás állapotmentes, azaz követi a
"statelessness SOA principless"-t.

Building stateless services encourage loose coupling, reusability, and composability. [1]

Hátrányai közé sorolnám, hogy a felhasználót azonosító adatok titkosítás nélkül továbbítódnak minden egyes SOAP lekéréssel (amennyiben nem használunk SSL-t).
További hátránya, hogy az összes függvény argumentumlistája a fehasználónév/jelszó párossal kezdődik,
csak utánuk jönnek a hívásra specifikus argumentumok (call-specific fields). Ez sokaknak nem túl elegáns megoldásnak tűnhet.

2. SOAP fejléc

A felhasználót azonosító adatokat (felhasználónév,jelszó) megadhatjuk a SOAP üzenet Header elemében is.

A SOAP üzenet fő elemei

Az előnye az, hogy a webszolgáltatást igénybe vévő klienseknek
nem kell a tagfüggvények meghívásakor megadni a felhasználót azonosító adatokat,
így csak a tagfüggvényekre specifikus adatokat kell megadni. A PHP SOAP kiterjesztése lehetővé
teszi azt is, hogy a kliensnek csak egyszer kelljen beállítani a SOAP fejléc tartalmát,
az összes további SOAP-lekérésbe automatikusan beilleszti a megadott fejlécet (példakód).
További előnye, hogy ez a megoldás is állapotmentes, azaz követi a
"statelessness SOA principless"-t.

Kisebb hátránya, hogy a klienseknek bonyolultabb a dolguk, mert nem biztos,
hogy mindenki tudja, hogyan kell SOAP fejlécet beállítani.

3. SOAP fejléc + munkamenet kezelése

Az előbb ismertetett megoldások mindegyike állapotmentes volt, és a kiszolgáló
minden egyes SOAP lekéréskor hitelesítést végzett.
Most csak egyszer végzünk hitelesítést (login metódus), majd minden további kérelem
egy egyedi munkamenet (session) azonosítót fog tartalmazni a SOAP Header elemben (SOAP fejléc). A
munkamenet azonosítót (sessionId) a webszolgáltatás Login metódusa adja vissza a kliensnek.

Ennek a módszernek két előnye is van.

  • Biztonságosabb megoldásnak mondható, hiszen
    a titkos jelszót a kliens csak egyetlen SOAP lekérés alkalmából küldi el. Ezt akár
    SSL kapcsolaton is küldheti, a többi lekérést pedig titkosítatlan HTTP-n
    (persze csak akkor, ha a biztonság kérdése nem létfontosságú és fontosabb a teljesítmény).
  • Léteznek olyan adatok (például a bevásárlókocsi tartalma), amelyeket meg kell
    őrizni a kérelmek között. Használhatjuk például a PHP beépített munkamenet támogatását, azaz session változókat.

[1] Yuli Vasiliev, SOA and WS-BPEL