Teléfono y Whatsapp: 675 18 68 80

Script para exportar suscriptores de PHPList

Con este sencillo script, configurado con la conexión a nuestra base de datos, podremos exportar los suscriptores de las listas de correo de PHPList en formato CSV.

Basta con ponerlo en una carpeta web del servidor, y dar permisos de escritura.

<?php
//    EXPORTS CSV from any list
 
session_start();
$servername = "mydomain.com";    //ENTER YOUR DOMAIN NAME
$username = "mydbusername";        //ENTER YOUR DATABASE USERNAME
$password = "mydbpassword";        //ENTER YOUR DATABASE PASSWORD
$dbname = "mydbname";            //ENTER YOUR DATABASE NAME
$dbprefix = "phplist";            //REPLACE WITH YOUR DATABASE PREFIX IF DIFFERENT THAN DEFAULT
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (is_array($_SESSION['filesarray'])) {
    $listarray = $_SESSION['listarray'];
    $filesarray = $_SESSION['filesarray'];
} else {
    $listarray = array();
    $filesarray = array();
};
 
function getlists(){
    global $dbprefix, $conn, $filesarray, $listarray;
    if (!is_array($_SESSION['listarray'])) {
        if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
        } else {
            $sql = "SELECT id, name FROM ".$dbprefix."_list ORDER BY ".$dbprefix."_list.name ASC;";
            if ($result = mysqli_query($conn, $sql)) {
                while ($row = mysqli_fetch_assoc($result)) {
                    array_push($listarray, $row['id']);
                    echo $row['id']."<br />";
                    array_push($listarray, $row['name']);
                    $fname = (strtolower(preg_replace("#\s#", "_", $row['name'])).".csv");
                    array_push($filesarray, "$fname");
                };
            };
            mysqli_close($conn);
        };
    } else {
        $listarray = $_SESSION['listarray'];
        $filesarray = $_SESSION['filesarray'];
    };
    $lsize = (count($listarray)-1);
    for ($i = 0; $i <= $lsize; $i++) {
        echo "\t\t\t\t\t\t\t\t\t".'<option value = "'.$listarray[$i].'">';
        $i++;
        echo $listarray[$i].'</option>'."\n";
    };
    $_SESSION['listarray'] = $listarray;
    $_SESSION['filesarray'] = $filesarray;
};
 
function getfiles(){
    global $filesarray;
    foreach ($filesarray as $k => $v){
        if ((file_exists("$v")) && (preg_match("#\.csv$#", $v))){
            echo "\t\t\t\t\t\t\t\t".'<br /><a href="'.$v.'" download="'.$v.'">DOWNLOAD</a> '.$v."\n";
        };
    };
};
 
function delfile($expunge){
    if (file_exists("$expunge")) {unlink("$expunge");};
};
 
if ($_POST){
    $data = $_POST;
    $query = "SELECT name FROM ".$dbprefix."_list WHERE id = '".$data['xlist']."';";
    if ($result = mysqli_query($conn, $query)) {
        $listname = implode(mysqli_fetch_assoc($result));
    };
    $sql = "SELECT ".$dbprefix."_user_user.email";
    $heading = "Email Addy";
    if (array_key_exists('id', $data)){
        $sql .= ", ".$dbprefix."_list.id AS List_ID";
        $heading .= ",List ID";
    };
    if (array_key_exists('confirmed', $data)){
        $sql .= ", ".$dbprefix."_user_user.confirmed";
        $heading .= ",Confirmed";
    };
    if (array_key_exists('blacklisted', $data)){
        $sql .= ", ".$dbprefix."_user_user.blacklisted";
        $heading .= ",Blacklisted";
    };
    if (array_key_exists('entered', $data)){
        $sql .= ", ".$dbprefix."_user_user.entered";
        $heading .= ",Entered";
    };
    if (array_key_exists('modified', $data)){
        $sql .= ", ".$dbprefix."_user_user.modified";
        $heading .= ",Modified";
    };
    $sql .= " FROM ".$dbname.".".$dbprefix."_listuser";
    $sql .= " INNER JOIN ".$dbname.".".$dbprefix."_list ON (".$dbprefix."_listuser.listid = ".$dbprefix."_list.id)";
    $sql .= " INNER JOIN ".$dbname.".".$dbprefix."_user_user ON (".$dbprefix."_user_user.id = ".$dbprefix."_listuser.userid)";
    $sql .= " WHERE ".$dbprefix."_list.id = '".$data['xlist']."'";
    switch ($data['xconf']) {
        case "yes": $sql .= " AND ".$dbprefix."_user_user.confirmed ='1'"; break;
        case "no": $sql .= " AND ".$dbprefix."_user_user.confirmed ='0'"; break;
        default: $sql .= "";
    };
    $sql .= ";";
    $filename = (strtolower(preg_replace("#\s#", "_", $listname)).".csv");
    if (file_exists("$filename")) {unlink("$filename");};
    $xfile = fopen("$filename","a");
    fwrite($xfile, "$heading\n");
    $result = mysqli_query($conn, $sql);
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
        fputcsv($xfile, $row);
    };
mysqli_close($conn);
fclose($xfile);
};
?>
<html>
<head>
    <meta charset="utf-8">
    <meta name="keywords" content="">
    <title>PHPLIST EXPORT TOOL</title>
    <script language="javascript" type="text/javascript">
    <!--
        function CheckForm(form){
            var message = ""
            if (!form.xlist.value){ message += "- SELECT A List to Export;\n"; }
            if (message) {
                alert(message);
                return false;
            } else {
                return true;
            }
        }
    //-->
    </script>
</head>
<body>
<p style="text-align: center;">
    <table width="" cellspacing="0" cellpadding="10" border="1"><tbody>
        <th colspan = "2">EXPORT TOOL</th>
        <tr>
            <td><h3>CHOOSE<br />YOUR<br />EXPORT<br />CRITERIA</h3></td>
            <td>
                <form method="post" OnSubmit="return CheckForm(this)" action="export.php">
                    <table width="" cellspacing="0" cellpadding="10" border="1"><tbody>
                        <tr>
                            <td>Select the list you want to export:
                                <select name="xlist">
                                    <option value = "" SELECTED>* Select *</option><?php echo "\n"; getlists(); ?>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>Confirmed?<br /><input type="radio" value="yes" name="xconf"> Yes &nbsp;&nbsp;<input type="radio" value="no" name="xconf"> No &nbsp;&nbsp;<input type="radio" value="both" name="xconf" checked="checked"> Both</td>
                        </tr>
                        <tr>
                            <td>
                                Select Fields to export<br />
                                <input type="checkbox" value="id" name="id"> ID &nbsp;&nbsp;
                                <input type="checkbox" value="email" name="email" checked="checked"> Email &nbsp;&nbsp;
                                <input type="checkbox" value="confirmed" name="confirmed"> Confirmed<br />
                                <input type="checkbox" value="blacklisted" name="blacklisted"> Blacklisted &nbsp;&nbsp;
                                <input type="checkbox" value="entered" name="entered"> Entered &nbsp;&nbsp;
                                <input type="checkbox" value="modified" name="modified"> Modified
                            </td>
                        </tr>
                        <tr>
                            <td><input type="reset" value="Reset" /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<input type="submit" name="export" value="EXPORT"></td>
                        </tr>
                        <tr>
                            <td><strong>Exported Lists Available for Download:</strong><?php echo "\n"; getfiles(); ?>
                            </td>
                        </tr>
                    </tbody></table>
                </form>
            </td>
        </tr>
    </tbody></table>
</p>
</body>
</html>

  • Mario Mey

    ¿Exactamente qué permisos hay que darle al archivo?

    El archivo se ejecuta, pero la lista aparece vacía. Tengo la version 2.10.12.