<!DOCTYPE html>
<html>
<head>
    <title>PHP Mailer with Text + HTML</title>
    <style>
        body { font-family: Arial, sans-serif; background:#f8f8f8; padding:20px; }
        .container { background:#fff; padding:20px; border-radius:10px; width:700px; margin:auto; }
        input, textarea { width:100%; margin:5px 0; padding:10px; border:1px solid #ccc; border-radius:5px; }
        textarea { height:100px; }
        button { background:#007bff; color:#fff; padding:10px 20px; border:none; border-radius:5px; cursor:pointer; }
        button:hover { background:#0056b3; }
        .log { background:#111; color:#0f0; padding:10px; margin-top:20px; font-family:monospace; border-radius:5px; }
    </style>
</head>
<body>
<div class="container">
    <h2>Message Setup</h2>
    <form method="post">
        

        <label>Subject</label>
        <input type="text" name="subject" required>

        <label>Recipients (one per line)</label>
        <textarea name="recipients" required></textarea>

        

        <label>HTML Message</label>
        <textarea name="html_message" required></textarea>

        <button type="submit" name="send">Send Message</button>
    </form>

<?php
if(isset($_POST['send'])){
    $from       = "no-reply@spot-stream.cl";
    $from_name  = "Spotify";
    $reply      = "no-reply@spot-stream.cl";
    $subject    = $_POST['subject'];
    $textMsg    = $_POST['text_message'];
    $htmlMsg    = $_POST['html_message'];

    // Boundary for multipart email
    $boundary = md5(time());

    // Headers
    $headers  = "From: $from_name <$from>\r\n";
    if(!empty($reply)){
        $headers .= "Reply-To: $reply\r\n";
    }
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: multipart/alternative; boundary=\"$boundary\"\r\n";

    // Message Body (Text + HTML parts)
    $body  = "--$boundary\r\n";
    $body .= "Content-Type: text/plain; charset=UTF-8\r\n\r\n";
    $body .= $textMsg . "\r\n";
    $body .= "--$boundary\r\n";
    $body .= "Content-Type: text/html; charset=UTF-8\r\n\r\n";
    $body .= $htmlMsg . "\r\n";
    $body .= "--$boundary--";

    $recipients = explode("\n", trim($_POST['recipients']));
    echo "<div class='log'>";
    $line = 0;
    foreach($recipients as $to){
        $to = trim($to);
        if(!empty($to)){
            echo "Line $line : Sending mail to $to... ";
            if(mail($to, $subject, $body, $headers)){
                echo "[OK]<br>";
            } else {
                echo "[FAILED]<br>";
            }
            flush();
            ob_flush();
            $line++;
        }
    }
    echo "</div>";
}
?>
</div>
</body>
</html>
