Technology > SMF Forum Code Modifications

Last Online Email Batch Job

(1/1)

Chip:
Here is a handy batch approach to notify idle members

<?php
$servername = "localhost";
$username = "dbuser";
$password = "dbpasswordi";
$dbname = "smf";
$tbpref = "smf_";

// SET THESE !
$days_not_logged_in = 365;
$do_email = 0;
// ---------

$connect = new mysqli($servername, $username, $password, $dbname);
if ($connect->connect_error) {
        die("Connection failed: " . $connect->connect_error);
}

$sql = "SELECT * FROM" . " " . $tbpref . "members ORDER BY last_login";

$result = $connect->query($sql);

if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
                if (!empty($row["last_login"])) {
                        $dateSin =  $row["last_login"];
                        $current_date = date("U") /* to have it in microseconds */;
                        $your_month = date("m", $dateSin);
                        $your_day =  date("d", $dateSin);
                        $your_year =  date("Y", $dateSin);
                        $selected_date_stamp = mktime(0,0,0,$your_month,$your_day,$your_year);
                        $selected_date = date("U",$selected_date_stamp);
                        $difference = round (($current_date - $selected_date)/(3600*24));
                        if ($difference >= $days_not_logged_in) {
                                echo $row["member_name"] . " " . $row["email_address"] . " " . "posts=" . $row["posts"] . " ";
                                echo "Last logged in" . " " . $difference . " " . "days ago" . PHP_EOL;
                                $to_member =  $row["member_name"];
                                $from_name = "SMF Administrator";
                                $from_email = "smfadmin@forum.drugs-and-users.org";
                                $headers = "From: $from_name <$from_email>";
                                $body = "Dear $to_member, \n
We miss you as you have not logged in to https://forum.drugs-and-users.org for over $difference days. \n
Your membership is safe however you may be deleted from the forum some time in the future if you remain inactive.
Of course you will be given ample warning should that be the case. \n
Come and visit us if you're bored :-) \n
Regards, \n
Chip (Server Admin)";
                                $subject = "Regarding your last login to dopetalk/D&U";
                                $to_email = $row["email_address"];
                                if ($do_email) {
                                        if (mail($to_email, $subject, $body, $headers)) {
                                                echo "(email sent)" . " ";
                                        } else {
                                                echo "email failed" . " ";
                                        }
                                }
                        }
                }
        }
} else {
        echo "0 results";
}

$connect->close();
?>

Navigation

[0] Message Index

It appears that you have not registered with dopetalk. To register, please click here...
Go to full version