Permalink
Browse files

New features (see description)

Now, if there are not valid email addresses, the mailer will stop trying
to send after a predefined limit, that has been set in config.php
(max_allowed_fails).

It's now possible to add new users to a preexisting mailing list by just
updating it.
  • Loading branch information...
1 parent 3a1fbf8 commit e81f056ae6f4f7b2ad7d3ece2a191bde3df14b1f @simonewebdesign committed Feb 9, 2013
Showing with 64 additions and 44 deletions.
  1. +1 −1 Lista.php
  2. +19 −2 User.php
  3. +1 −1 _list_form.php
  4. +1 −1 _user_form.php
  5. +3 −2 config.php
  6. +4 −32 list_create.php
  7. +9 −3 list_update.php
  8. +24 −0 save_users_in_list.php
  9. +1 −1 send.php
  10. +1 −1 user_create.php
View
@@ -1 +1 @@
-<?php// si chiama Lista e non List perché list è riservato alla funzione list().class Lista { static $db; # database PDO instance static function create($name='') { $q = "INSERT INTO lists (name, created_at) VALUES (?,?)"; $sql_data = array( $name, date("Y-m-d H:i:s"), // 2001-03-10 17:16:18 (the MySQL DATETIME format) ); $s = self::$db->prepare($q); return $s->execute($sql_data); } static function read($id) { if ($id > 0) { $q = "SELECT * FROM lists WHERE id=?"; $s = self::$db->prepare($q); if ( $s->execute(array($id)) ) { return $s->fetchObject(); } } return false; } static function update($id, $name='') { if ($id > 0) { $q = "UPDATE lists SET name=?, updated_at=? WHERE id=?"; $sql_data = array( $name, date("Y-m-d H:i:s"), // 2001-03-10 17:16:18 (the MySQL DATETIME format) $id ); $s = self::$db->prepare($q); return $s->execute($sql_data); } return false; } static function delete($id) { if ($id > 0) { $q = "UPDATE lists SET is_deleted=1 WHERE id=?"; $s = self::$db->prepare($q); return $s->execute(array($id)); } return false; } static function all() { $q = "SELECT * FROM lists WHERE is_deleted <= 0"; $s = self::$db->prepare($q); if ($s->execute()) { return $s->fetchAll(PDO::FETCH_OBJ); } return false; } static function getSelectById($id) { if ($id > 0) { $q = "SELECT * FROM lists"; } } }/***** INITIALIZATION *****/Lista::$db = $db;
+<?php// si chiama Lista e non List perché list è riservato alla funzione list().class Lista { static $db; # database PDO instance static function create($name='') { $q = "INSERT INTO lists (name, created_at) VALUES (?,?)"; $sql_data = array( $name, date("Y-m-d H:i:s"), // 2001-03-10 17:16:18 (the MySQL DATETIME format) ); $s = self::$db->prepare($q); return $s->execute($sql_data); } static function read($id) { if ($id > 0) { $q = "SELECT * FROM lists WHERE id=?"; $s = self::$db->prepare($q); if ( $s->execute(array($id)) ) { return $s->fetchObject(); } } return false; } static function update($id, $name='') { if ($id > 0) { $q = "UPDATE `lists` SET `name`=?, `updated_at`=? WHERE `id`=?"; $sql_data = array( $name, date("Y-m-d H:i:s"), // 2001-03-10 17:16:18 (the MySQL DATETIME format) $id ); $s = self::$db->prepare($q); return $s->execute($sql_data); } return false; } static function delete($id) { if ($id > 0) { $q = "UPDATE lists SET is_deleted=1 WHERE id=?"; $s = self::$db->prepare($q); return $s->execute(array($id)); } return false; } static function all() { $q = "SELECT * FROM lists WHERE is_deleted <= 0"; $s = self::$db->prepare($q); if ($s->execute()) { return $s->fetchAll(PDO::FETCH_OBJ); } return false; } static function getSelectById($id) { if ($id > 0) { $q = "SELECT * FROM lists"; } } /* Returns an array without duplicates. * @param $list a list of email addresses. */ public static function remove_duplicates_from($list, $separator="\r\n") { $array_with_dupes = explode($separator, $list); $array_sanitized = array_unique($array_with_dupes); return $array_sanitized; /* $input_rows = count($duplicated_array); $output_rows = count($sanitized_array); $dupes = $input_rows - $output_rows; echo "Operazione conclusa con successo!<br>"; echo "Email iniziali: $input_rows<br>"; echo "Email finali: $output_rows<br>"; echo "Duplicati rimossi: $dupes<br>"; */ }}/***** INITIALIZATION *****/Lista::$db = $db;
View
@@ -64,8 +64,7 @@ static function setMailAsReceived($id) {
}
static function resetFetch() {
- $s = self::$db->prepare("UPDATE users SET has_received_mail=0");
- return $s->execute();
+ return self::$db->exec("UPDATE users SET has_received_mail=0, fails=0");
}
static function all($limit=0) {
@@ -122,6 +121,24 @@ static function toBeSent($limit, $list_id) {
return $s->fetchAll(PDO::FETCH_OBJ);
}
}
+
+ static function increment_fails($id) {
+ if ($id > 0) {
+ $q = "UPDATE `users` SET `fails`=`fails`+1 WHERE `id`=?";
+ $s = self::$db->prepare($q);
+ return $s->execute(array($id));
+ }
+ return false;
+ }
+
+ static function get_invalid_ones_by_list_id($list_id, $max_allowed_fails=5){
+ if ($list_id > 0) {
+ $q = "SELECT * from `users` WHERE `list_id`=? AND `fails`>=?";
+ $s = self::$db->prepare($q);
+ return $s->execute(array($list_id, $max_allowed_fails));
+ }
+ return false;
+ }
}
View
@@ -1 +1 @@
-<form method=POST> <fieldset> <legend><?=LISTS?></legend> <p> <label for="id"><?=ID?></label> <input id="id" name="id" type="text" value="<?=isset($l->id) ? $l->id : NOT_SET?>" disabled> </p> <p> <label for="name"><?=NAME?></label> <input id="name" name="name" type="text" value="<?=isset($l->name) ? $l->name : ''?>"> </p> <p> <label for="email_addresses"><?=EMAIL_ADDRESSES?></label> <textarea id="email_addresses" name="email_addresses" rows=15></textarea> </p> <p> <input id="submit" name="submit" type="submit" value="<?=CREATE_LIST?>"> </p> </fieldset></form>
+<form method=POST> <fieldset> <!-- <legend><?=LISTS?></legend> --> <p> <label for="id"><?=ID?></label> <input id="id" name="id" type="text" value="<?=isset($l->id) ? $l->id : NOT_SET?>" disabled> </p> <p> <label for="name"><?=NAME?></label> <input id="name" name="name" type="text" value="<?=isset($l->name) ? $l->name : ''?>"> </p> <p> <label for="email_addresses"><?=EMAIL_ADDRESSES?></label> <?php $email_addresses_plain_text = ''; if (isset($l->id)) { $email_addresses = User::all_where("list_id = {$l->id}"); foreach ($email_addresses as $em) { $email_addresses_plain_text .= "$em->email\r\n"; } } ?> <textarea id="email_addresses" name="email_addresses" rows=15><?=$email_addresses_plain_text?></textarea> </p> <p> <input id="submit" name="submit" type="submit" value="<?=(isset($_GET['id'])) ? UPDATE_LIST : CREATE_LIST?>"> </p> </fieldset></form>
View
@@ -1 +1 @@
-<form method=POST> <fieldset> <legend><?=USERS?></legend> <p> <label for="id"><?=ID?></label> <input id="id" name="id" type="text" value="<?=isset($u->id) ? $u->id : NOT_SET?>" disabled> </p> <p> <label for="email"><?=EMAIL?></label> <input id="email" name="email" type="email" value="<?=isset($u->email) ? $u->email : ''?>" required> </p> <p> <label for="name"><?=NAME?></label> <input id="name" name="name" type="text" value="<?=isset($u->name) ? $u->name : ''?>"> </p> <?php include_once '_list_select.php'; ?> <p> <input id="submit" name="submit" type="submit" value="<?=CREATE_USER?>"> </p> </fieldset></form>
+<form method=POST> <fieldset> <legend><?=USERS?></legend> <p> <label for="id"><?=ID?></label> <input id="id" name="id" type="text" value="<?=isset($u->id) ? $u->id : NOT_SET?>" disabled> </p> <p> <label for="email"><?=EMAIL?></label> <input id="email" name="email" type="email" value="<?=isset($u->email) ? $u->email : ''?>" required> </p> <p> <label for="name"><?=NAME?></label> <input id="name" name="name" type="text" value="<?=isset($u->name) ? $u->name : ''?>"> </p> <?php include_once '_list_select.php'; ?> <p> <input id="submit" name="submit" type="submit" value="<?=(isset($_GET['id'])) ? UPDATE_USER : CREATE_USER?>"> </p> </fieldset></form>
View
@@ -41,10 +41,11 @@
# miscellaneous
'language' => 'it',
'regex' => '^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$^',
- 'limit' => 100, # limit of emails to send before refreshing the send.php page
+ 'limit' => 300, # limit of emails to send before refreshing the send.php page
'word_wrap' => 50,
'timeout' => 10000, # refresh timeout, in milliseconds.
'debug_mode' => 0, # enables SMTP debug information (2 for testing)
'smtp_auth' => true,
- 'charset' => 'UTF-8'
+ 'charset' => 'UTF-8',
+ 'max_allowed_fails' => 5
);
View
@@ -13,39 +13,11 @@
if ( isset($_POST['submit']) ) {
if ( Lista::create($_POST['name']) ) {
-
- $list_id = $db->lastInsertId();
-
- // fetch email addresses and remove duplicates
- $separator = "\r\n";
-
- $duplicated_array = explode($separator, $_POST['email_addresses']); var_dump($duplicated_array);
- $sanitized_array = array_unique($duplicated_array); var_dump($sanitized_array);
-
- foreach ($sanitized_array as $email_address) {
- if ( !empty($email_address) ) {
- if ( User::create($email_address) ) {
- echo "<p><b>$email_address</b> aggiunto con successo alla lista <b>{$_POST['name']}</b>.";
- } else {
- echo "<p>ERRORE durante l'inserimento di <b>$email_address</b>.";
- }
- }
- }
-
- $input_rows = count($duplicated_array);
- $output_rows = count($sanitized_array);
- $dupes = $input_rows - $output_rows;
-
- echo "Operazione conclusa con successo!<br>";
- echo "Email iniziali: $input_rows<br>";
- echo "Email finali: $output_rows<br>";
- echo "Duplicati rimossi: $dupes<br>";
-
- $update_list_id = $db->prepare("UPDATE users SET list_id = $list_id WHERE list_id <= 0");
- $update_list_id->execute();
-
-
+
echo SUCCESS;
+ $list_id = $db->lastInsertId();
+ include 'save_users_in_list.php';
+
} else {
echo FAIL;
}
View
@@ -2,6 +2,7 @@
include_once 'bootstrap.php';
include_once 'Lista.php';
+include_once 'User.php';
################################
@@ -10,10 +11,15 @@
$l = Lista::read($_GET['id']);
if (isset($_POST['submit'])) {
- if ( Lista::update($_POST['name']) ) {
- echo SUCCESS;
+ if ( Lista::update($_GET['id'], $_POST['name']) ) {
+ echo SUCCESS; // relativo solo alla lista, non agli utenti
+
+ $list_id = $_GET['id'];
+
+ include 'save_users_in_list.php';
+
} else {
- echo FAIL;
+ echo FAIL . " La lista non e' stata aggiornata.";
}
}
View
@@ -0,0 +1,24 @@
+<?php
+
+// fetch email addresses and remove duplicates
+$emails = Lista::remove_duplicates_from($_POST['email_addresses']);
+
+/* Saves users in the list.
+* @param $list_id the ID of the mailing list you want to save the users to.
+* @param $emails an array of unique emails.
+*
+*/
+foreach ($emails as $email) {
+ if ( !empty($email) && User::findByEmail($email) == false ) { // user doesn't exist yet
+ if ( User::create($email) ) {
+ echo "<p><b>$email</b> aggiunto con successo alla lista <b>{$_POST['name']}</b>.";
+ }
+ else {
+ // Probabilmente fallisce perche` e` gia` nel db.
+ echo "<p>ERRORE durante l'inserimento di <b>$email</b>.";
+ }
+ }
+}
+// Setto la list_id all'id della lista corrente, cosi' l'aggiunta e' completa
+$update_list_id = $db->prepare("UPDATE users SET list_id = $list_id WHERE list_id <= 0");
+$update_list_id->execute();
Oops, something went wrong.

0 comments on commit e81f056

Please sign in to comment.