pdo = new PDO( "mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET, DB_USER, DB_PASSWORD, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ]); } // (C) DESTRUCTOR - CLOSE DATABASE CONNECTION function __destruct () { if ($this->stmt!==null) { $this->stmt = null; } if ($this->pdo!==null) { $this->pdo = null; } } // (D) COUNT TOTAL NUMBER OF SUBSCRIBERS function count () { $this->stmt = $this->pdo->prepare("SELECT COUNT(*) `cnt` FROM `newsletter`"); $this->stmt->execute(); return $this->stmt->fetchColumn(); } // (E) GET SUBSCRIBERS function get ($start=0, $end=10) { $this->stmt = $this->pdo->prepare("SELECT * FROM `newsletter` LIMIT $x, $y"); $this->stmt->execute(); return $this->stmt->fetchAll(); } // (F) SEND NEWSLETTER function send ($subject, $template, $replace=null) { // (F1) EMAIL HEADERS $header = implode("\r\n", [ "From: " . $this->from, "MIME-Version: 1.0", "Content-Type: text/html; charset=utf-8", ]); // (F2) LOAD TEMPLATE if (!file_exists($template)) { $this->error = "$template not found"; return false; } $template = file_get_contents($template); // (F3) SEND BATCH BY BATCH for ($i=0; $i<$this->count(); $i+=$this->each) { $subs = $this->get($i, $this->each); foreach ($subs as $s) { // (F3-1) EMAIL TEMPLATE REPLACE $msg = $template; if ($replace != null) { foreach ($replace as $from=>$to) { $msg = str_replace($from, $s[$to], $msg); }} // (F3-2) SEND - SAVE THE SEND STATUS HERE IF YOU WANT if (@mail($s["email"], $subject, $msg, $header)) { // OK } else { // NOT OK } } // (F3-3) NEXT CYCLE sleep($this->pause); } } } // (G) DATABASE SETTINGS - CHANGE TO YOUR OWN! define("DB_HOST", "localhost"); define("DB_NAME", "globalpunch_lb-newsletter"); define("DB_CHARSET", "utf8mb4"); define("DB_USER", "globalpunch_lb-user"); define("DB_PASSWORD", "Burney20**##"); // (H) NEWSLETTER OBJECT $_NEWS = new Newsletter();