<?php
// Set your exact Brevo-verified sender address here
$verified_sender = "tove@nergtwo.com";
$destination_email = "spam@nerg.com";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Sanitize and validate input
    $user_email = filter_var($_POST['user_email'], FILTER_SANITIZE_EMAIL);

    if (filter_var($user_email, FILTER_VALIDATE_EMAIL)) {

        $subject = "New Email Submission";
        $body = "A new email address was submitted on your site:\n\n";
        $body .= "Submitted Email: " . $user_email . "\n";
        $body .= "Date/Time: " . date("Y-m-d H:i:s") . "\n";
        $body .= "IP Address: " . $_SERVER['REMOTE_ADDR'] . "\n";

        // Construct proper RFC-compliant headers
        $headers = array(
            'From' => $verified_sender,
            'Reply-To' => $user_email,
            'X-Mailer' => 'PHP/' . phpversion(),
                         'Content-Type' => 'text/plain; charset=UTF-8'
        );

        // Convert headers array to string format
        $header_string = "";
        foreach ($headers as $key => $value) {
            $header_string .= "$key: $value\r\n";
        }

        // Pass the -f parameter to Postfix to explicitly set the envelope sender (Return-Path)
        $extra_params = "-f " . escapeshellarg($verified_sender);

        if (mail($destination_email, $subject, $body, $header_string, $extra_params)) {
            echo "<!DOCTYPE html><html><head><title>Success</title></head>";
            echo "<body style='background:#111; color:#00ff66; text-align:center; padding-top:100px; font-family:sans-serif;'>";
            echo "<h1 style='font-size:3rem;'>🚀 SUCCESS!</h1>";
            echo "<p style='font-size:1.5rem;'>Transmitted <strong>" . htmlspecialchars($user_email) . "</strong> to " . htmlspecialchars($destination_email) . "</p>";
            echo "<a href='index.html' style='color:#00f0ff;'>← Back to form</a>";
            echo "</body></html>";
        } else {
            echo "<!DOCTYPE html><html><head><title>Error</title></head>";
            echo "<body style='background:#111; color:#ff3333; text-align:center; padding-top:100px; font-family:sans-serif;'>";
            echo "<h1>Mail Handoff Failed</h1>";
            echo "<p>Postfix rejected the submission. Check <code>sudo journalctl -u postfix -n 20</code> for logs.</p>";
            echo "<a href='index.html' style='color:#fff;'>Try again</a>";
            echo "</body></html>";
        }
    } else {
        echo "<!DOCTYPE html><html><head><title>Invalid Email</title></head>";
        echo "<body style='background:#111; color:#ffff00; text-align:center; padding-top:100px; font-family:sans-serif;'>";
        echo "<h1>Invalid Email Address</h1>";
        echo "<p>Please go back and enter a valid email address.</p>";
        echo "<a href='index.html' style='color:#fff;'>Go Back</a>";
        echo "</body></html>";
    }
} else {
    // Redirect direct GET requests back to the main page
    header("Location: index.html");
    exit();
}
?>
