<?php
/*=======================================================================
 *
 * automailer_internal.php
 *
 * Autor: Marc Vollmann
 *
  =======================================================================*/

include_once ("../include/mcglobal.inc.php");
// include_once ("../include/image.inc.php");
include_once ('../include/email/htmlMimeMail.php');


// getSecHttpVars("0", array("auth", "errMsg"));

$auth           = trim($argv[1]);
$mailText       = trim($argv[2]);
$mailSubject    = trim($argv[3]);
$mailTo         = trim($argv[4]);
$mailFrom       = trim($argv[5]);
$mailCc         = trim($argv[6]);
$mailBcc        = trim($argv[7]);
$mailMode       = trim($argv[8]);
$mailLogContent = trim($argv[9]);
$mailLogFile    = trim($argv[10]);

$mailText = urldecode($mailText);
$mailSubject = urldecode($mailSubject);
$mailLogContent = urldecode($mailLogContent);

// Execution-Time for script
set_time_limit(120);

// Writes a string to a file optional with or without linefeed
function writeToInternalMailLogFile($fileName, $stringToOperate, $mode = 'a', $noLf = "") {
  $fileHandle = fopen("../log/" . $fileName, $mode);
  if ($noLf == "") : $stringToOperate .= "\n"; endif;
  $opCode = fwrite($fileHandle, $stringToOperate);
  fclose($fileHandle);
  return $opCode;
}

if ($mailLogFile == "" || $mailLogFile == "EMPTY") : $mailLogFile = "external_mail.log"; endif;
writeToInternalMailLogFile($mailLogFile, "To: " . $mailTo);
writeToInternalMailLogFile($mailLogFile, "Subject: [" . $mailSubject . "]");
writeToInternalMailLogFile($mailLogFile, "Text: " . $mailText);

if ($auth == "comegetsome97531" && $mailText != "" && $mailTo != "") :

   $dirName = dirname(__FILE__);
   $dirName = str_replace("\\", "/", $dirName);
   $lastSlashPos = strrpos($dirName,"/");
   $path = substr($dirName, 0, $lastSlashPos);

   $currentTime = getDateTime("0");
   $currentClientIP = trim($_SERVER['REMOTE_ADDR']);
   writeToInternalMailLogFile($mailLogFile, $currentTime . " | " . $currentClientIP);

   if ($mailLogContent != "" && $mailLogContent != "EMPTY") :
      writeToInternalMailLogFile($mailLogFile, $mailLogContent);
   endif;

   // Send mail to admin@assecutor.de
   $mailObj = new htmlMimeMail();
   if ($mailFrom == "" || $mailFrom == "EMPTY") :
      $mailFrom = "admin@assecutor.de";
   endif;
   $mailObj->setFrom($mailFrom);
   if ($mailCc != "" && $mailCc != "EMPTY") :
      $mailObj->setCc($mailCc);
   endif;
   if ($mailBcc != "" && $mailBcc != "EMPTY") :
      $mailObj->setBcc($mailBcc);
   endif;
   $mailObj->setSubject($mailSubject);
      
   if (strtolower($mailMode) == "html") :
      // HTML
      $mailObj->setHtml($mailText, null, "./");
   else :
      // PLAIN-TEXT
      $mailObj->setText($mailText);
   endif;

   $mailResult = $mailObj->send(array($mailTo), 'smtp');
   if ($mailResult) :
      writeToInternalMailLogFile($mailLogFile, "MAIL SENT!");
   else :
      writeToInternalMailLogFile($mailLogFile, "MAIL NOT SENT!");
   endif;
   $mailObj = NULL;
   writeToInternalMailLogFile($mailLogFile, "---------------------------------------------------");
endif;
?>
