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

$phpcont="";
$to="";
$cc="";
$bcc="";
$subj="";
$getpostfound=false;
foreach( $_POST as  $name=>$val ) {
  $getpostfound=true;
  if (strpos(str_replace("+"," ",str_replace("&lt;","<",str_replace("&gt;",">",strtolower(urldecode($val))))), "<") !== false && strpos(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);
  } 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 {
    $subj=str_replace("+"," ",str_replace("&lt;","<",str_replace("&gt;",">",urldecode($val))));
  }
}
foreach( $_GET as $name=>$val ) {
  $getpostfound=true;
  if (strpos(str_replace("+"," ",str_replace("&lt;","<",str_replace("&gt;",">",strtolower(urldecode($val))))), "<") !== false && strpos(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);
  } 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 {
    $subj=str_replace("+"," ",str_replace("&lt;","<",str_replace("&gt;",">",urldecode($val))));
  }
}


if ($phpcont != "" && $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;

                $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));
                //$uid = md5(uniqid(time()));

                // a random hash will be necessary to send mixed content
                $separator = md5(time());

                $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;
                $headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
                $headers .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;

                $headers .= "Please see attachment below:" . $eol . $eol;

                // attachment
                $headers .= "--" . $separator . $eol;
                $headers .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
                $headers .= "Content-Transfer-Encoding: base64" . $eol;
                $headers .= "Content-Disposition: attachment;filename=\"" . $filename . "\"" . $eol;
                $headers .= $content . $eol . $eol;

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

                mail($to, $subject, "", $headers);
                exit;
} 
?>
