<?php
/*=======================================================================
 *
 * email.inc.php
 *
 * Autor: Marc Vollmann
 *
 =======================================================================*/



function sendMail ($to, $subject, $fromName, $fromEmail, $msgBody = "", $headers = "", $wrapWord = "", $cc = "", $bcc = "") {
  $ret = FALSE;

  // Check for global activation
  if (EMAIL_ENABLED == "1") :

     // Check existence of addressee and validity of the mail format
     if ($to != "" && checkEmailFormat($to)) :

        if ($subject == "") : $subject = "votian"; endif;     
     
        if ($fromEmail != "" && checkEmailFormat($fromEmail)) :

           if ($fromName == "") : $fromName = "votian"; endif;
           $headers .= "From: \"" . $fromName . "\" <" . $fromEmail . ">\n";
        endif;

        /*
        $headers .= "MIME-Version: 1.0\n";
        $headers .= "Content-type: text/plain; charset=iso-8859-1\n";
        $headers .= "X-Priority: 1\n";
        $headers .= "X-MSMail-Priority: High\n";
        $headers .= "X-Mailer: php\n";
        */

        // if ($cc != "") : $headers .= "Cc: ".$cc."\n"; endif;
        if ($bcc != "") : $headers .= "Bcc: ".$bcc."\n"; endif;
    
        if ($wrapWord != "" && is_numeric($wrapWord))
        $msgBody = wordwrap($msgBody, $wrapWord);

        $ret = mail($to, $subject, $msgBody, $headers);
     endif;
  endif;
  return $ret;
}


function prepareSendMailPear ($smtpServer, $user, $password, $to, $subject, $fromName, $fromEmail, $msgBody = "", $contentType = "", $wrapWord = "", $cc = "", $bcc = "") {
  global $mail_object;

  if ($wrapWord != "" && is_numeric($wrapWord))
  $msgBody = wordwrap($msgBody, $wrapWord);

  $headers["From"]    = $fromEmail;
  $headers["To"]      = $to;
  $headers["Subject"] = $subject;
  if ($contentType != "") :
     if ($contentType == "html") : $contentType = "text/html; charset=iso-8859-1\n"; endif;
     $headers["Content-type"] = $contentType;
  endif;

  $params["host"] = $smtpServer;
  $params["port"] = "25";
  $params["auth"] = true;
  $params["username"] = $user;
  $params["password"] = $password;
  
  return array($params, $to, $headers, $msgBody);
}

// Triggers the script for sending an external mail
function sendExternalMailExtended ($mailContent, $mailSubject, $mailTo, $mailFrom = "EMPTY", $mailCc = "EMPTY", $mailBcc = "EMPTY", $mailLog = "EMPTY") {
	global $hq_id, $currentHqId, $debug;

	if ($mailContent != "" && $mailSubject != "" && $mailTo != "") :

	   $path = getAbsoluteSystemPath();
	   if (!isset($currentHqId) || $currentHqId == "0") :
	      $currentHqId = $hq_id;
	   endif;

	   if ($debug) :
	      echo "path = " . $path . "<br>";
	      echo "currentHqId = " . $currentHqId . "<br>";
        echo "mailSubject = " . $mailSubject . "<br>";
        echo "mailTo = " . $mailTo . "<br>";
        echo "mailFrom = " . $mailFrom . "<br>";
        echo "mailCc = " . $mailCc . "<br>";
        echo "mailBcc = " . $mailBcc . "<br>";
        echo "mailLog = " . $mailLog . "<br>";
        echo "mailContent = " . $mailContent . "<br>";
        // die();
     endif;

     // BEGIN mail text
     $mailCssFontType = getParameterValue("0", "MAIL_CSS_FONT_TYPE", $hq_id);
     if ($mailCssFontType == "") : $mailCssFontType = "Verdana, Arial, Helvetica, sans-serif"; endif;
     
     $mailtext  = "<html><head><meta text/html>";
     $mailtext .= "<style type=\"text/css\">";
     $mailtext .= ".f8np1 { font-family: " . $mailCssFontType . "; font-size: 8pt; font-weight: normal; padding: 1px}";
     $mailtext .= ".f8bp1 { font-family: " . $mailCssFontType . "; font-size: 8pt; font-weight: bold; padding: 1px}";
     $mailtext .= ".f10np1 { font-family: " . $mailCssFontType . "; font-size: 10pt; font-weight: normal; padding: 1px}";
     $mailtext .= ".f10bp1 { font-family: " . $mailCssFontType . "; font-size: 10pt; font-weight: bold; padding: 1px}";
     $mailtext .= ".f8np1_red { font-family: " . $mailCssFontType . "; font-size: 8pt; font-weight: normal; padding: 1px; color: #FF0000;}";
     $mailtext .= "</style>";
     $mailtext .= "</head><body><h4>";
     
     $mailCssFontClass = getParameterValue("0", "MAIL_CSS_FONT_CLASS", $currentHqId);
     if ($mailCssFontClass == "") : $mailCssFontClass = "f8bp1"; endif;
     
     $mailPreSalutationText = getParameterValue("0", "MAIL_PRE_SALUTATION_TEXT", $currentHqId);
     if ($mailPreSalutationText == "") : $mailPreSalutationText = getLngt("Sehr geehrte Damen und Herren,"); endif;
     $mailtext .= "<div class=\"" . $mailCssFontClass . "\">" . $mailPreSalutationText . "</div>";
     $mailtext .= "<div style=\"width:100%; height:10px;\"> </div>";

     $mailtext .= $mailContent;
     $mailtext .= "<div style=\"width:100%; height:10px;\"> </div>";
     
     // Signature
     $mailTextRegards = getParameterValue("0", "MAIL_TEXT_REGARDS", $currentHqId);
     if ($mailTextRegards == "") : $mailTextRegards = getLngt("Mit freundlichem Gruß,"); endif;
     $mailtext .= "<div class=\"" . $mailCssFontClass . "\">" . $mailTextRegards . "</div>";
     if ($mailDisplayModeEmployeeData == "1" && $mailEmployeeData[0] != "" && $mailEmployeeData[1] != "") :
        $mailtext .= "<div style=\"width:100%; height:10px;\"> </div>";
        $mailtext .= "<div class=\"" . $mailCssFontClass . "\">" . $mailEmployeeData[0] . " " . $mailEmployeeData[1] . "</div>";
     else :
        $mailSalutationText = getParameterValue("0", "MAIL_SALUTATION_TEXT", $currentHqId);
        $mailtext .= "<div class=\"" . $mailCssFontClass . "\">" . getLngt($mailSalutationText) . "</div>";
     endif;
     $mailtext .= "<div style=\"width:100%; height:10px;\"> </div>";
     
     // HOMEPAGE
     $mailFooterWebPage = getParameterValue("0", "MAIL_FOOTER_WEB_PAGE", $currentHqId);
     
     // LOGO
     $logoName = getParameterValue("0", "IMG_LOGO_NAME", $currentHqId);
     $logoHeight = getParameterValue("0", "IMG_LOGO_HEIGHT", $currentHqId);
     $logoWidth = getParameterValue("0", "IMG_LOGO_WIDTH", $currentHqId);
     if ($logoName != "" && $logoHeight != "" && $logoWidth != "") :
        $mailtext .= "<div style=\"width:100%; height:10px;\"> </div>";
        if ($mailFooterWebPage != "") : 
           $mailtext .= "<div><a href=\"http://" . $mailFooterWebPage . "\"><img src=\"../images/external/" . $logoName . "\" border=\"0\" height=\"" . $logoHeight . "\" width=\"" . $logoWidth . "\"></a></div>";
        else :
           $mailtext .= "<div><img src=\"../images/external/" . $logoName . "\" border=\"0\" height=\"" . $logoHeight . "\" width=\"" . $logoWidth . "\"></div>";
        endif;
        $mailtext .= "<div style=\"width:100%; height:10px;\"> </div>";
     endif;
     
     $mailFooterEnabled = getParameterValue("0", "MAIL_FOOTER_ENABLED", $currentHqId);
     if ($mailFooterEnabled == "1") :
        $mailFooterAddress = getParameterValue("0", "MAIL_FOOTER_ADDRESS", $currentHqId);
        if ($mailFooterAddress != "") :
           $mailtext .= "<table style=\"font-family: Arial; font-size: 10pt; font-weight: normal; padding: 1px; color: #000000;\">";
           $mailtext .= $mailFooterAddress;
           $mailtext .= "</table>";
           $mailtext .= "<div style=\"width:100%; height:10px;\"> </div>";
        endif;
        $mailFooterContact = getParameterValue("0", "MAIL_FOOTER_CONTACT", $currentHqId);
        if ($mailFooterContact != "") :                  
           if ($mailDisplayModeEmployeeData == "1" && ($mailEmployeeData[2] != "" || $mailEmployeeData[3] != "" || $mailEmployeeData[4] != "")) :
              $mailtext .= "<table>";
              if ($mailEmployeeData[2] != "") : 
                 $mailtext .= "<tr><td><span class=\"" . $mailCssFontClass . "\">" . getLngt("Telefon") . ": " . $mailEmployeeData[2] . "</span></td></tr>";
              endif;
              if ($mailEmployeeData[4] != "") : 
                 $mailtext .= "<tr><td><span class=\"" . $mailCssFontClass . "\">" . getLngt("Fax") . ": " . $mailEmployeeData[4] . "</span></td></tr>";
              endif;
              if ($mailEmployeeData[3] != "") : 
                 $mailtext .= "<tr><td><span class=\"" . $mailCssFontClass . "\">" . getLngt("Email") . ": " . $mailEmployeeData[3] . "</span></td></tr>";
              endif;
              // $mailFooterWebPage = getParameterValue("0", "MAIL_FOOTER_WEB_PAGE", $currentHqId);
              if ($mailFooterWebPage != "") : 
                 // $mailtext .= "<tr><td><span class=\"" . $mailCssFontClass . "\">" . $mailFooterWebPage . "</span></td></tr>";
                 $mailtext .= "<tr><td><span class=\"" . $mailCssFontClass . "\"><a href=\"http://" . $mailFooterWebPage . "\">" . $mailFooterWebPage . "</a></span></td></tr>";
              endif;
              $mailtext .= "</table>";
           else :
              $mailtext .= "<table style=\"font-family: Arial; font-size: 10pt; font-weight: normal; padding: 1px; color: #000000;\">";
              $mailtext .= $mailFooterContact;
              $mailtext .= "</table>";
           endif;                  
           $mailtext .= "<div style=\"width:100%; height:10px;\"> </div>";
        endif;
        $mailFooterImpressum = getParameterValue("0", "MAIL_FOOTER_IMPRESSUM", $currentHqId);
        if ($mailFooterImpressum != "") :
           $mailtext .= "<table style=\"font-family: Arial; font-size: 7pt; font-weight: normal; padding: 1px; color: #000000;\">";
           $mailtext .= $mailFooterImpressum;
           $mailtext .= "</table>";
           $mailtext .= "<div style=\"width:100%; height:10px;\"> </div>";
        endif;
        $mailFooterResponsibility = getParameterValue("0", "MAIL_FOOTER_RESPONSIBILITY", $currentHqId);
        if ($mailFooterResponsibility == "") : $mailFooterResponsibility = getParameterValue("0", "MAIL_FOOTER_RESPONSIBILITY", "0"); endif;
        if ($mailFooterResponsibility != "") : 
           $mailtext .= "<div style=\"font-family: Arial; font-size: 7pt; font-weight: normal; padding: 1px; color: #000000;\">" . $mailFooterResponsibility . "</div>";
           // Extended text
           $mailFooterResponsibility2 = getParameterValue("0", "MAIL_FOOTER_RESPONSIBILITY_2", $currentHqId);
           if ($mailFooterResponsibility2 == "") : $mailFooterResponsibility2 = getParameterValue("0", "MAIL_FOOTER_RESPONSIBILITY_2", "0"); endif;
           if ($mailFooterResponsibility2 != "") : 
              $mailtext .= "<div style=\"font-family: Arial; font-size: 7pt; font-weight: normal; padding: 1px; color: #000000;\">" . $mailFooterResponsibility2 . "</div>";
              $mailFooterResponsibility3 = getParameterValue("0", "MAIL_FOOTER_RESPONSIBILITY_3", $currentHqId);
              if ($mailFooterResponsibility3 == "") : $mailFooterResponsibility3 = getParameterValue("0", "MAIL_FOOTER_RESPONSIBILITY_3", "0"); endif;
              if ($mailFooterResponsibility3 != "") : 
                 $mailtext .= "<div style=\"font-family: Arial; font-size: 7pt; font-weight: normal; padding: 1px; color: #000000;\">" . $mailFooterResponsibility3 . "</div>"; 
                 $mailFooterResponsibility4 = getParameterValue("0", "MAIL_FOOTER_RESPONSIBILITY_4", $currentHqId);
                 if ($mailFooterResponsibility4 == "") : $mailFooterResponsibility4 = getParameterValue("0", "MAIL_FOOTER_RESPONSIBILITY_4", "0"); endif;
                 if ($mailFooterResponsibility4 != "") : 
                    $mailtext .= "<div style=\"font-family: Arial; font-size: 7pt; font-weight: normal; padding: 1px; color: #000000;\">" . $mailFooterResponsibility4 . "</div>";
                    $mailFooterResponsibility5 = getParameterValue("0", "MAIL_FOOTER_RESPONSIBILITY_5", $currentHqId);
                    if ($mailFooterResponsibility5 == "") : $mailFooterResponsibility5 = getParameterValue("0", "MAIL_FOOTER_RESPONSIBILITY_5", "0"); endif;
                    if ($mailFooterResponsibility5 != "") : 
                       $mailtext .= "<div class=\"" . $mailFooterImpressum . "\">" . $mailFooterResponsibility5 . "</div>";
                    endif;
                 endif;
              endif;
              $mailtext .= "<div style=\"width:100%; height:10px;\"> </div>";
           endif;
        endif;
        $mailFooterThinkGreen = getParameterValue("0", "MAIL_FOOTER_THINK_GREEN", $currentHqId);
        if ($mailFooterThinkGreen != "") :
           $mailtext .= $mailFooterThinkGreen;
           $mailtext .= "<div style=\"width:100%; height:10px;\"> </div>";
        endif;
        $mailtext .= "<div style=\"width:100%; height:30px;\"> </div>";               
     endif;
     
     $mailtext .= "</body></html>";
     // END mail text

     include('../include/email/htmlMimeMail.php');

     // Send mail to admin@assecutor.de
     $mailObj = new htmlMimeMail();
     if ($mailFrom == "" || $mailFrom == "EMPTY") :
        $mailFrom = getParameterValue("0", "MAIL_SENDER_ADDRESS", $currentHqId);
        if ($mailFrom == "")  :
           $mailFrom = "support@assecutor.de";
        endif;
     endif;
     $mailObj->setFrom($mailFrom);
     if ($mailCc != "" && $mailCc != "EMPTY") :
        $mailObj->setCc($mailCc);
     endif;
     if ($mailBcc != "" && $mailBcc != "EMPTY") :
        $mailObj->setBcc($mailBcc);
     endif;
     $mailObj->setSubject($mailSubject);
        
     $mailObj->setHtml($mailtext, null, "./");
     
     $mailResult = $mailObj->send(array($mailTo), 'smtp');
     $mailObj = NULL;
  endif;

  return $mailResult;
}

?>
