<?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();

<?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) {

  $_user_is_empty = empty($email);
  $_user_already_exists = User::findByEmail($email);

  if ($_user_is_empty) {

    echo "<p>$email cannot be added because it's empty.</p>";
  }

  else if ($_user_already_exists) {

    // then don't insert it.
    echo "<p>$email cannot be added to the list because it already exists in another list.</p>";

  } else {

    // let's create the user.
    if (User::create($email, '', $list_id)) {
      echo "<p><b>$email</b> successfully added to the list.";
    } else {
      echo "<p><b>$email</b> hasn't been created because of an unknown error. Sorry.</p>";
    }

  }
}

Fix bug when saving users in mailing list
Fix bug when saving users in mailing list
