]> git.leonardobizzoni.com Git - highschool-graduation-project/commitdiff
Fixed mail spam
authorLeonardoBizzoni <leo2002714@gmail.com>
Sun, 3 Jul 2022 08:36:16 +0000 (10:36 +0200)
committerLeonardoBizzoni <leo2002714@gmail.com>
Sun, 3 Jul 2022 08:36:16 +0000 (10:36 +0200)
www/Migrations/m_1656833406_addedNotificationSentToFavorite.php [new file with mode: 0644]
www/mailer.php
www/vtuberIsLive.php

diff --git a/www/Migrations/m_1656833406_addedNotificationSentToFavorite.php b/www/Migrations/m_1656833406_addedNotificationSentToFavorite.php
new file mode 100644 (file)
index 0000000..6f90516
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+
+use app\core\Application;
+
+class m_1656833406_addedNotificationSentToFavorite
+{
+    public function up()
+    {
+        $db = Application::$app->db;
+        $sql = "ALTER TABLE vtubers ADD COLUMN sent BOOL default 0;";
+        $db->pdo->exec($sql);
+    }
+
+    public function down()
+    {
+        $db = Application::$app->db;
+        $sql = "ALTER TABLE vtubers DROP COLUMN sent;";
+        $db->pdo->exec($sql);
+    }
+}
+?>
index 8a20ff2ec854f17ca0c8f4ced12990b0c07a49ff..184c9c697be87c51b8cf535b8bba10dc9d400cf2 100644 (file)
@@ -22,39 +22,44 @@ $config = [
 
 $app = new Application(__DIR__, $config);
 
-$stmt = $app->db->pdo->prepare("
-select email, vtubers.username, vtubers.id, notify
+while (true) {
+    $stmt = $app->db->pdo->prepare("
+select email, vtubers.username, vtubers.id, notify, sent
 from users, vtubers, favoriteVtuber
 where _vtuberID=vtubers.id
 and _userID=users.id
+and sent=0
 and notify=1");
 
-$stmt->execute();
-$data = $stmt->fetchAll();
+    $stmt->execute();
+    $data = $stmt->fetchAll();
 
+    foreach ($data as $row) {
+        // var_dump($row);
+        $mail = new PHPMailer;
+        $mail->IsSMTP(); // telling the class to use SMTP
+        $mail->SMTPAuth = true;
+        $mail->SMTPSecure = "ssl";
+        $mail->Host = "smtp.gmail.com";
+        $mail->Port = "465";
 
-foreach ($data as $row) {
-    // var_dump($row);
-    $mail = new PHPMailer;
-    $mail->IsSMTP(); // telling the class to use SMTP
-    $mail->SMTPAuth = true;
-    $mail->SMTPSecure = "ssl";
-    $mail->Host = "smtp.gmail.com";
-    $mail->Port = "465";
+        $mail->Username = Application::$app->config["mail"]["username"];
+        $mail->Password = Application::$app->config["mail"]["pass"];
 
-    $mail->Username = Application::$app->config["mail"]["username"];
-    $mail->Password = Application::$app->config["mail"]["pass"];
+        $mail->SetFrom($mail->Username);
 
-    $mail->SetFrom($mail->Username);
+        $mail->Subject    = $row["username"] . " is live!";
+        $mail->Body       = " ";
 
-    $mail->Subject    = $row["username"] . " is live!";
-    $mail->Body       = " ";
+        $mail->AddAddress($row["email"]);
 
-    $mail->AddAddress($row["email"]);
+        if (!$mail->Send()) {
+            echo "Mailer Error: " . $mail->ErrorInfo . "\n";
+        } else {
+            echo "Message sent!\n";
+            $stmt = $app->db->pdo->prepare("update vtubers set sent=1 where id=".$row["id"]);
 
-    if (!$mail->Send()) {
-        echo "Mailer Error: " . $mail->ErrorInfo . "\n";
-    } else {
-        echo "Message sent!\n";
+            $stmt->execute();
+        }
     }
 }
index 54e20f28f604ca216489271179832fb28d133549..e9c123e4b3237e435db36c6d9da855ff718f10e5 100644 (file)
@@ -76,7 +76,7 @@ while (true) {
                 $tag = $result->item($i)->getAttribute("href");
                 if (str_contains($tag, "https://www.youtube.com/watch?v=")) {
                     echo "{$vtuber["username"]} - $tag\n";
-                    $stmt = $app->db->pdo->prepare("update vtubers set live='$tag' where id=" . $vtuber["id"]);
+                    $stmt = $app->db->pdo->prepare("update vtubers set live='$tag', sent=0 where live=NULL and id=" . $vtuber["id"]);
                     $stmt->execute();
                     $isLive = true;
                 }