<?php
// emailhtml.php
// RJM Programming 
// March, 2019
// Email HTML attachment

$phpcont="";
$body="";
$to="";
$cc="";
$bcc="";
$subj="";

$fns=[""];
$fnc=[""];

$filename="";
$hcont="";
$content="";

function fillarraysmaybe($bdin) {
  global $phpcont, $fns, $fnc, $body;
  if (substr(($bdin . "<"),0,1) == "<") {
  return $bdin;
  } else {
  $ii=0;
  if (trim($phpcont) != "") { $ii=1; }
  $bd=" " . str_replace("\n"," ",str_replace("<br>"," ",str_replace("#"," ",str_replace("&"," ",str_replace("?"," ",str_replace(","," ",$bdin)))))) . " ";
    //file_put_contents("inv.inv9", $bd);
  $htbits=explode(" http", str_replace(" HTTP", " http", $bd));
  if (sizeof($htbits) > 1) {
    for ($hi=1; $hi<sizeof($htbits); $hi++) {
     $ilen=strlen(explode(".",    explode(" ", substr($htbits[$hi],0))[0]  )[-1 + sizeof(explode(".",    explode(" ", substr($htbits[$hi],0))[0]  ))]);
     if ($ilen <= 4 && $ilen > 0 && strpos(substr($htbits[$hi],0),".") !== false && strlen(substr($htbits[$hi],0)) > 2) {
     $fcc="";
     if (strtolower(substr($htbits[$hi],0,1)) == "s") {
       try {
     $fcc=file_get_contents("http" . explode(" ", substr($htbits[$hi],1))[0]);
       } catch (Exception $ee) {
       $fcc="";
       } 
     } else {
       try {
     $fcc=file_get_contents("http" . explode(" ", substr($htbits[$hi],0))[0]);
       } catch (Exception $ee) {
       $fcc="";
       } 
     }
     if ($fcc != "") {
     $fnc[$ii]=chunk_split(base64_encode($fcc));
     $fns[$ii]=explode("#", explode("?", explode("/", explode(" ", substr($htbits[$hi],0))[0])[-1 + sizeof(explode(" ", substr($htbits[$hi],0)))])[0])[0];
     $ii++;
     }
     }
    }
  }
  $htbits=explode(" ", $bd);
  if (sizeof($htbits) > 1) {
    for ($hi=1; $hi<sizeof($htbits); $hi++) {
     $ilen=strlen(explode(".",    explode(" ", substr($htbits[$hi],0))[0]  )[-1 + sizeof(explode(".",    explode(" ", substr($htbits[$hi],0))[0]  ))]);
     if ($ilen <= 4 && $ilen > 0 && strpos(substr($htbits[$hi],0),".") !== false &&  strlen(substr($htbits[$hi],0)) > 2 && strpos(strtolower(" " . substr($htbits[$hi],0)), " http") === false) {
    //file_put_contents("inv.inv99", "" . $ilen);
       $fcc="";
       if (strpos(strtolower(" " . substr($htbits[$hi],0)), " //") !== false) {
       try {
       $fcc=file_get_contents("http:" . explode(" ", substr($htbits[$hi],0))[0]);
       } catch (Exception $ee) {
       $fcc="";
       } 
       } else if (file_exists(substr($htbits[$hi],0))) {
    //file_put_contents("inv.inv999", "" . explode(" ", substr($htbits[$hi],0))[0]);
       try {
       $fcc=file_get_contents(explode(" ", substr($htbits[$hi],0))[0]);
    //file_put_contents("inv.inv9999", "" . strlen($fcc));
       } catch (Exception $ee) {
       $fcc="";
    //file_put_contents("inv.inv99990", "" . strlen($fcc));
       } 
       }
       if ($fcc != "") {
    //file_put_contents("inv.inv999909", "" . explode("#", explode("?", explode("/", explode(" ", substr($htbits[$hi],0))[0])[-1 + sizeof(explode(" ", substr($htbits[$hi],0)))])[0])[0]);
       $fnc[$ii]=chunk_split(base64_encode($fcc));
       $fns[$ii]=explode("#", explode("?", explode("/", explode(" ", substr($htbits[$hi],0))[0])[-1 + sizeof(explode(" ", substr($htbits[$hi],0)))])[0])[0];
       $ii++;
       }
     }
    }
  }
  if ($fns[0] != "" && $phpcont == "") {  $phpcont=" "; }  
  return $bdin;
  } 
}

$getpostfound=false;
foreach( $_POST as  $name=>$val ) {
    //file_put_contents("inv.preinv0", "inv.inv0");
  if ($val != "") {
  $getpostfound=true;
    //file_put_contents("inv.inv0", "inv.inv0");
  if (strpos($name, "subj") !== false) {
    $subj=str_replace("+"," ",str_replace("&lt;","<",str_replace("&gt;",">",urldecode($val))));
  } else if (strpos(str_replace("<br>"," ",str_replace("+"," ",str_replace("&lt;","<",str_replace("&gt;",">",strtolower(urldecode($val)))))), "<") !== false && strpos(str_replace("<br>"," ",str_replace("+"," ",str_replace("&lt;","<",str_replace("&gt;",">",strtolower(urldecode($val)))))), ">") !== false) {
    //file_put_contents("inv.inv00", "inv.inv00");
    $phpcont=str_replace("+"," ",str_replace("&lt;","<",str_replace("&gt;",">",urldecode($val))));
    $pbits=explode('"data:', $phpcont);
    for ($ii=1; $ii<sizeof($pbits); $ii++) {  $phpcont=str_replace(explode('"',$pbits[$ii])[0],str_replace(" ","+",explode('"',$pbits[$ii])[0]),$phpcont);      }
    $phpcont=str_replace(' src="//', ' src="http://', $phpcont);
    $phpcont=str_replace(" src='//", " src='http://", $phpcont);
    $phpcont=str_replace(' src=//', ' src=http://', $phpcont);
    $phpcont=str_replace(' href="//', ' href="http://', $phpcont);
    $phpcont=str_replace(" href='//", " href='http://", $phpcont);
    $phpcont=str_replace(' href=//', ' href=http://', $phpcont);
    $body="";
    //file_put_contents("inv.inv00", $phpcont);
  } else if (strpos(str_replace("+"," ",str_replace("&lt;","<",str_replace("&gt;",">",strtolower(urldecode($val))))), "@") !== false) {
     //file_put_contents("inv.inv000", "inv.inv000");
   if ($to == "") { 
    $to=str_replace("+"," ",str_replace("&lt;","<",str_replace("&gt;",">",urldecode($val))));
    } else if ($cc == "") {
    $cc=str_replace("+"," ",str_replace("&lt;","<",str_replace("&gt;",">",urldecode($val))));
    } else {
    $bcc=str_replace("+"," ",str_replace("&lt;","<",str_replace("&gt;",">",urldecode($val))));
    }
  } else if (str_replace('0','',str_replace('1','',str_replace('2','',str_replace('3','',str_replace('4','',str_replace('5','',str_replace('6','',str_replace('7','',str_replace('8','',str_replace('9','',$subj)))))))))) == "") {
    $subj=str_replace("+"," ",str_replace("&lt;","<",str_replace("&gt;",">",urldecode($val))));
    if (str_replace('0','',str_replace('1','',str_replace('2','',str_replace('3','',str_replace('4','',str_replace('5','',str_replace('6','',str_replace('7','',str_replace('8','',str_replace('9','',$subj)))))))))) == "") {
      $subj="";
    }
  } else if (substr(($phpcont . " "),0,1) != "<") {
    //file_put_contents("inv.postinv0", $body);
     //file_put_contents("inv.inv0000", "inv.inv0000");
    $body=str_replace("+"," ",str_replace("&lt;","<",str_replace("&gt;",">",urldecode($val))));
    //file_put_contents("inv.postinv0", $body);
    //file_put_contents("inv.inv", $body);
    fillarraysmaybe($body);
    //file_put_contents("inv.ouv", "" . sizeof($fnc) . " " . $body);
  }
  }
}
foreach( $_GET as $name=>$val ) {
  if ($val != "") {
  $getpostfound=true;
  if (strpos($name, "subj") !== false) {
    $subj=str_replace("+"," ",str_replace("&lt;","<",str_replace("&gt;",">",urldecode($val))));
  } else if (strpos(str_replace("<br>"," ",str_replace("+"," ",str_replace("&lt;","<",str_replace("&gt;",">",strtolower(urldecode($val)))))), "<") !== false && strpos(str_replace("<br>"," ",str_replace("+"," ",str_replace("&lt;","<",str_replace("&gt;",">",strtolower(urldecode($val)))))), ">") !== false) {
    $phpcont=str_replace("+"," ",str_replace("&lt;","<",str_replace("&gt;",">",urldecode($val))));
    $pbits=explode('"data:', $phpcont);
    for ($ii=1; $ii<sizeof($pbits); $ii++) {  $phpcont=str_replace(explode('"',$pbits[$ii])[0],str_replace(" ","+",explode('"',$pbits[$ii])[0]),$phpcont);      }
    $phpcont=str_replace(' src="//', ' src="http://', $phpcont);
    $phpcont=str_replace(" src='//", " src='http://", $phpcont);
    $phpcont=str_replace(' src=//', ' src=http://', $phpcont);
    $phpcont=str_replace(' href="//', ' href="http://', $phpcont);
    $phpcont=str_replace(" href='//", " href='http://", $phpcont);
    $phpcont=str_replace(' href=//', ' href=http://', $phpcont);
    $body="";
  } else if (strpos(str_replace("+"," ",str_replace("&lt;","<",str_replace("&gt;",">",strtolower(urldecode($val))))), "@") !== false) { 
    if ($to == "") { 
    $to=str_replace("+"," ",str_replace("&lt;","<",str_replace("&gt;",">",urldecode($val))));
    } else if ($cc == "") {
    $cc=str_replace("+"," ",str_replace("&lt;","<",str_replace("&gt;",">",urldecode($val))));
    } else {
    $bcc=str_replace("+"," ",str_replace("&lt;","<",str_replace("&gt;",">",urldecode($val))));
    }
  } else if (str_replace('0','',str_replace('1','',str_replace('2','',str_replace('3','',str_replace('4','',str_replace('5','',str_replace('6','',str_replace('7','',str_replace('8','',str_replace('9','',$subj)))))))))) == "") {
    $subj=str_replace("+"," ",str_replace("&lt;","<",str_replace("&gt;",">",urldecode($val))));
    if (str_replace('0','',str_replace('1','',str_replace('2','',str_replace('3','',str_replace('4','',str_replace('5','',str_replace('6','',str_replace('7','',str_replace('8','',str_replace('9','',$subj)))))))))) == "") {
      $subj="";
    }
  } else if (substr(($phpcont . " "),0,1) != "<") {
    $body=str_replace("+"," ",str_replace("&lt;","<",str_replace("&gt;",">",urldecode($val))));
    //file_put_contents("inv.inv2", $body);
    fillarraysmaybe($body);
    //file_put_contents("inv.ouv2", "" . sizeof($fnc) . " " . $body);
  }
  } 
}


if (($phpcont . $body) != "" && $to != "") {
                $subject = $subj;
                if ($subject == "") { $subject="HTML Email Attachment"; } 
                $eol = PHP_EOL;
                $headers = 'From: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;
                if ($cc != "") { $headers .= 'CC: ' . $cc . $eol; }
                if ($bcc != "") { $headers .= 'BCC: ' . $bcc . $eol; }
                $headers .= 'Reply-To: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;

                if (trim($phpcont) != "") {
                $filename="htmlattachment.html";
                $hcont=$phpcont; //'<html><body><div id="dcontent">' .  str_replace("+", " ", urldecode($_POST['newemailcontent'])) . '</div><style>' . explode("<style>", explode("</style>", file_get_contents(dirname(__FILE__) . "/flowchart.html"))[0])[1] . '</style></body></html>';
                $content = chunk_split(base64_encode($hcont));
                $fns[0]=$filename;
                $fnc[0]=$content;
                }
                //$uid = md5(uniqid(time()));

                // a random hash will be necessary to send mixed content
                $separator = md5(time());
                
                if (isset($_GET['inline']) || isset($_POST['inline'])) { 
                if (sizeof($fns) == 1) {
                  if (trim($phpcont) != "" && $body == "") {
                   $body=$phpcont;
                   //file_put_contents('inv00x', '' . sizeof($phpcont) . ' ... ' . substr($body,0,45));
                  }
                  if (strpos(strtolower($body), '</head>') !== false) {
                   $message=explode("</head>", str_replace("</HEAD>", "</head>", str_replace("</Head>", "</head>",   $body   )))[0];
                  } else if (strpos(strtolower($fnc[0]), '</head>') !== false) {
                   $message=explode("</head>", str_replace("</HEAD>", "</head>", str_replace("</Head>", "</head>",   $fnc[0]   )))[0];
                  } else if (strpos(strtolower($body), '<body') !== false) {
                   $premessage = explode("</body>", explode("<body", str_replace("<BODY", "<body", str_replace("<Body", "<body", str_replace("</BODY>", "</body>", str_replace("</Body>", "</body>",   $body   )))))[1]   )[0] . "</body>";  
                   $pmsg=explode(">", $premessage);
                   $message="<body" . substr($premessage, strlen($pmsg[0]));
                  } else if (strpos(strtolower($fnc[0]), '<body') !== false) {
                   $premessage = explode("</body>", explode("<body", str_replace("<BODY", "<body", str_replace("<Body", "<body", str_replace("</BODY>", "</body>", str_replace("</Body>", "</body>",   $fnc[0]   )))))[1]   )[0] . "</body>";  
                   $pmsg=explode(">", $premessage);
                   $message="<body" . substr($premessage, strlen($pmsg[0]));
                  } else if (strpos(strtolower($body), '<') !== false) {
                   $message = $body;  
                  } else if (strpos(strtolower($fnc[0]), '<') !== false) {
                   $message = $fnc[0];  
                  } else if (trim($phpcont) != "") {
                   $message = '<pre>' . $fnc[0] . '</pre>';  
                  } else {
                   $message = '<pre>' . $body . '</pre>';  
                  }
                 }
               }

                if ($message == "") {
                $headers .= "MIME-Version: 1.0" . $eol;
                $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol . $eol;
                $headers .= "Content-Transfer-Encoding: 7bit" . $eol;
                $headers .= "This is a MIME encoded message." . $eol . $eol;
                

                // message
                $headers .= "--" . $separator . $eol;
                } else {
                $headers = "MIME-Version: 1.0" . $eol;
                }
 
                if ($message == "") {
                $headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
                $headers .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
                } else {
                $headers .= "Content-Type: text/html; charset=\"iso-8859-1\"" . $eol;
                $headers .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
                $headers .= $message . $eol . $eol; //"Please see attachments created via HTTP://www.rjmprogramming.com.au/PHP/no_body.php below:" . $eol . $eol;
                $headers .= 'To: ' . $emailto . $eol;
                $headers .= 'From: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;
                $headers .= 'Reply-To: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;
                }


                if ($phpcont != "" || sizeof($fns) > 1) {
                if ($body != "") { $body.="  "; }
                if ($message == "") {
                if (sizeof($fns) > 1) {
                if (strpos($_SERVER['HTTP_REFERER'], "rjmprogramming.com.au") !== false) { 
                $headers .= str_replace("\n"," ",str_replace("<br>","\n",$body)) . "Please see attachments created via . " . $_SERVER['HTTP_REFERER'] . " below:" . $eol . $eol;
                } else {
                $headers .= str_replace("\n"," ",str_replace("<br>","\n",$body)) . "Please see attachments below:" . $eol . $eol;
                }
                } else {
                if (strpos($_SERVER['HTTP_REFERER'], "rjmprogramming.com.au") !== false) { 
                $headers .= str_replace("\n"," ",str_replace("<br>","\n",$body)) . "Please see attachment created via . " . $_SERVER['HTTP_REFERER'] . " below:" . $eol . $eol;
                } else {
                $headers .= str_replace("\n"," ",str_replace("<br>","\n",$body)) . "Please see attachment below:" . $eol . $eol;
                }
                }
                }

                if ($message == "") {
                for ($ic=0; $ic<sizeof($fns); $ic++) {
                // attachment
                $headers .= "--" . $separator . $eol;
                $headers .= "Content-Type: application/octet-stream; name=\"" . $fns[$ic] . "\"" . $eol;
                $headers .= "Content-Transfer-Encoding: base64" . $eol;
                $headers .= "Content-Disposition: attachment;filename=\"" . $fns[$ic] . "\"" . $eol;
                $headers .= $fnc[$ic] . $eol . $eol;
                }
                } else {
                $subject=str_replace("HTML Email Attachment", "Email", $subject);
    //file_put_contents("inv.inv999908", "" . str_replace("\n"," ",str_replace("<br>","\n",$body)));
                $headers .= str_replace("\n"," ",str_replace("<br>","\n",$body)) . $eol . $eol;
                }

                $headers .= "--" . $separator . "--";
                }

                if ($message != "") {  // inline HTML

 // To send HTML mail, the Content-type header must be set
 $hdrs[] = 'MIME-Version: 1.0';
 $hdrs[] = 'Content-type: text/html; charset=iso-8859-1';

 // Additional headers
 $hdrs[] = 'To: ' . $to;
 $hdrs[] = 'From: rmetcalfe@rjmprogramming.com.au';

 if ($cc != "") { $hdrs[] = 'CC: ' . $cc; }
 if ($bcc != "") { $hdrs[] = 'BCC: ' . $bcc; }
 $hdrs[] = 'Reply-To: ' . 'rmetcalfe@rjmprogramming.com.au';

 // Mail it
 mail($to, $subject, $message, implode("\r\n", $hdrs));               
                } else {
                mail($to, $subject, "", $headers);
                }
                exit;
} 
?>
