EMailRobot

[ home ] | [ info ] | [ demo ] | [ downloads ] | [ documentation ] | [ description ]

info
purpose E-mail robot, takes letters, parse and forms answer.
implementation PHP
date 22 aug 2005
version 0.3
keywords php oo uml
url http://emailrobot.elementalcms.org
author Ilya Nemihin (nemilya@mail.ru)

downloads
EMailRobot03.zip 10kb

demo
You can to see email robot in action, by follow: send email to demo1 address with follow body (2 lines as plain text):
$a: 1
$b: 2
if all ok, you will get answer from robot ( a + b ):
  1 + 2 = 3

documentation

About

This is simple framework for handle emails by some robot, perform some action based on params extracted from email body, and generate answer email with some result.

Demo

You can to see email robot in action, by follow: send email to demo1 address with follow body (2 lines as plain text):

$a: 1
$b: 2

if all ok, you will get answer from robot:
1 + 2 = 3

I.e. robot:

  1. get your email message,
  2. parse it's body,
  3. extract params 'a' and 'b' (with values '1' and '2'),
  4. perform action 'sum', get result ('3'),
  5. send result back to sender

Files, directories

When you unzip archive, you'll found follow structure:

  EMailRobot03/
    bin/
    include/
    test/
    readme.txt

'bin' -- here is placed 'robot.php' script for what need to configure email pipe
'include' -- here are placed all classes
'test' -- here you can to test system without sending and getting emails, just start 'test.php' from console, and it handle 'email.txt' file as input email message, and prints result of action to console.

About email pipe

When you send email to demo1@... it will arrive to computer where 'emailrobot.elementalcms.org' domain is placed, then come to some email-daemon programm, and this programm have to see to some 'aliaces' or 'email forwarding' file, in this file email-daemon have to find the rule for 'demo1' name. In this example in file there is 'pipe'-rule, this rule mean - "all incoming email messages need to pass to script what is specified in pipe". I.e. on left side of the pipe will be email message on right side - runing script.

How to configure

I have cPanel admin for manage my account, and there in: "Email Management Tools / Aliases/Forwarding", I've "Add Forwarder", with name 'demo1', and in field for forwarding set:

| /usr/bin/php -q /absolute_place_to_folder/EMailRobot03/bin/robot.php

where sign '|' mean "pipe", and 'absolute_place_to_folder' is real absolute path to folder where I've unzipped EMailRobot03, and 'robot.php' is script what will be started no all incoming to 'demo1@...' email, and email message will be passed to 'stdin' stream.

How it works

In 'robot.php' file the script read all 'stdin' stream and pass it to email robot object, and then run 'work' method what will do all work:

  $stdin = file( "php://stdin" );
  $m = implode( "", $stdin );

  $e_robot = new EMailRobot();
  $e_robot->setEMailData( $m );
  $e_robot->work();

EMailRobot class

This is main class, see EMailRobot/EMailRobot.php for detail explanation.

There is 'Message' and 'Action' abstract classes.

Message class

The purpose of 'Message' class is parse text, and extract pairs: 'name1': 'value1', 'name2': 'value2', etc.

This class have 2 public functions:

  - setData( $data ):
    initialize Message class by text
  - getField( $name ):
    get field named '$name'

abstract:
  - parse() - parse $data, and initialize pairs (params)

private:
  - setField( $name, $value ) - called from 'parse'

For example see 'MessageFormat1.php' this class implement 'parse' method for parse follow format:

  $param_name1:param_value1
  $param_name2:param_value2
  ...

Action class

Purpose of 'Action' class is to make some action based on params (this class have access to 'Message' object), and set result.

Have public methods:

  - setMessage( &$message ):
    set object of 'Message' class (parsed email-body)
  - getResult():
    returns result of action

Abstract method:
  - make():
    any action based on 'Message' object

In this demo there is 2 action classes:

  • ActionSum.php - get 'a', and 'b' params (from 'Message') and write in result summ of them
  • ActionMult.php - get 'a', and 'b' params (from 'Message') and write in result multiple of them

The EMailRobot class configured to 'ActionSum.php' you can to configure to ActionMult.php class -- see 'EMailRobot/EMailRobot.php' file lines 81-83.

description
UML diagrams for product classes overview.

Use Case diagram
EMailRobot packages
EMailRobot class diagram
main Sequence daigram
work Sequence daigram
package EMailRobot
package Action
package EMail
package Message

Use Case diagram

EMailRobot packages

EMailRobot class diagram

main Sequence daigram

work Sequence daigram

package EMailRobot

package Action

package EMail

package Message