<?php
// mailto.php
// RJM Programming
// October, 2018

if (!file_exists($_FILES['image']['tmp_name']) || !is_uploaded_file($_FILES['image']['tmp_name'])) {
      if (1 == 2) { file_put_contents('qpm.qpm','No upload'); }
      //echo '<html><body onload=" alert(' . "'" . 'No upload' . "'" . '); if (top.document.getElementById(' . "'" . 'demail' . "'" . ')) {   top.document.getElementById(' . "'" . 'demail' . "'" . ').innerHTML=' . "'" . 'No upload' . "';  } " . '"></body></html>';
} else if (isset($_POST['to']) && isset($_POST['subject']) && isset($_POST['filename'])) {
      if (strpos(urldecode($_POST['to']), "@") !== false) {
        if (1 == 2) { file_put_contents('qpm.qpm',urldecode($_POST['to'])); }

        $to = urldecode($_POST['to']);
        $subject = str_replace("+", " ", urldecode($_POST['subject']));
        $body = file_get_contents($_FILES['image']['tmp_name']);
        unlink($_FILES['image']['tmp_name']);
        $eol = PHP_EOL;
        $headers = 'From: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;
        $headers .= 'Reply-To: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;

        $filename=str_replace("+", " ", urldecode($_POST['filename'])); //str_replace("quicktime", "mp4", str_replace("/", ".", str_replace("data:", "", $fname[0])));
        $content = chunk_split(base64_encode($body));
        //$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);

        if (1 == 2) { file_put_contents('qpm.qpm',urldecode($_POST['to']) . ' ' . urldecode($_POST['subject'])); }

        //echo '<html><body onload=" alert(' . "'Emailed to " . $to . " an email with subject " . $subject . "'" . ');  if (top.document.getElementById(' . "'" . 'demail' . "'" . ')) {   top.document.getElementById(' . "'" . 'demail' . "'" . ').innerHTML=' . "'Emailed to " . $to . " an email with subject " . $subject . "';  } " . '"></body></html>';
      } else {
        if (1 == 2) { file_put_contents('qpm.qpm','yes!'); }
      }
} else {  // Your file has been uploaded ... but we do nothing
      if (1 == 2) { file_put_contents('qpm.qpm','yes'); }
      unlink($_FILES['image']['tmp_name']);
      //echo '<html><body onload=" alert(' . "'" . 'Upload can work but we did not save' . "'" . ');    if (top.document.getElementById(' . "'" . 'demail' . "'" . ')) {   top.document.getElementById(' . "'" . 'demail' . "'" . ').innerHTML=' . "'" . 'Upload can work but we did not save' . "';  } " . '"></body></html>';
}

?>

