Location: PHPKode > scripts > Email Robot > emailrobot/include/EMailRobot/EMailRobot.php
<?php

//==================================================
// project: EMailRobot
// version: 0.2
// author: ilya nemihin (hide@address.com)
// date: 27-aug-2002
// url: www.nemilya.narod.ru/erobot/
//==================================================

/*


E-mail robot, takes letters, parse and forms answer.

For the specific tasks it is necessary to make:
1. inheritance Message abstract class and finish parse() method.
2. inheritance Action abstract class and finish make() method.

in this example there is MessageFormat1 class and ActionSum ActionMult.

   Use case fro EMailRobot class:

   $emailRobot = new EMailRobot();
   
   // file test:
   $data = join('', file('email.txt'));
   $emailRobot->emailSender->setMode( 'test' ); // answer will be on console
   
   // real for email-script:
   //$stdin = file( "php://stdin" );
   //$data = implode( "", $stdin );
   
   $emailRobot->setEMailData( $data );
   
   $headers = "Mime-Version: 1.0\nContent-Type: text/plain; charset=Windows-1251\nContent-Transfer-Encoding: 8bit";
   $emailRobot->emailSender->setHeaders( $headers );
   $emailRobot->work();

============================

work():

1. $this->parseEmail();
2. $this->parseMessage();
3. $this->makeAnswer();
4. $this->sendAnswer();


*/

require_once('../include/EMail/EMailMessage.php');
require_once('../include/Email/EMailSender.php');
require_once('../include/Message/MessageFormat1.php');
require_once('../include/Action/ActionSum.php');

class EMailRobot{

var $emailMessage;
var $emailSender;
var $message;
var $emailData;
var $answer;
var $action;

function EMailRobot(){
  $this->emailMessage = new EMailMessage();
  $this->emailSender = new EMailSender();
  $this->message = new MessageFormat1(); // editable (with reuqire_once command)
  $this->action = new ActionSum(); // editable (with reuqire_once command)
  $this->emailData = '';
  $this->answer = '';
}

  function setEMailData( $emailData ){
    $this->emailData = $emailData;
  }

  function work(){
    $this->parseEmail();
  
    $this->parseMessage();
  
    $this->makeAnswer();
  
    $this->sendAnswer();
  }

  function parseEmail(){
    $this->emailMessage->setData( $this->emailData );
    $this->emailMessage->parse();
  }

  function parseMessage(){
    $this->message->setData( $this->emailMessage->getBody() );
    $this->message->parse();
  }

  function makeAnswer(){
    $this->action->setMessage( $this->message );
    $this->action->make();
    $this->answer = $this->action->getResult();
  }

  function sendAnswer(){
    $this->emailSender->setTo( $this->emailMessage->getFrom() );
    $this->emailSender->setSubject( 'answer robot' );
    $this->emailSender->setBody( $this->answer );
    $this->emailSender->send();
  }

}

?>
Return current item: Email Robot