<?php
function sendMail() {
        $to = 'dk@doktorkraus.de';
        $headers = "From: \"zaaReloaded\" <dk@doktorkraus.de>";
        $reportId = $_POST['report_id'];
        $info = print_r( $_POST, true ); // this will be a string, not an array!
        $body = <<<EOF
zaaReloaded hat einen Fehler ausgelöst.

-----

Benutzerkommentar:

{$_POST['comment']}

-----

Technische Info: $info

--
www.doktorkraus.de/zaareloaded
EOF;

  if ( $_POST['ccUser'] ) {
    $validMail = filter_var( $_POST['usersMail'], FILTER_VALIDATE_EMAIL );
    $validUser = filter_var( $_POST['usersName'], FILTER_SANITIZE_STRING );
    if ( $validUser ) {
      $validUser = "\"$validUser\" "; // space character deliberately placed
    };
    if ( $validMail ) {
      $headers .= "\r\nCc: $validUser<$validMail>";
    };
  };
  mail($to, "Exception report [$reportId]", $body, $headers);
}

// =============================================================
// Begin main script
// =============================================================

if (!isset($_POST['report_id'])) {
	http_response_code(400);
	exit;
}

sendMail();
// Return the report ID and the issue URL to signal all is well.
$reportId = $_POST['report_id'];
$load = "{\"ReportId\": \"$reportId\", \"IssueUrl\": \"https://www.doktorkraus.de/zaareloaded\"}";
error_log( __FILE__ . ": Load = $load" );
header('Content-Type: application/json');
echo $load;