Mailinglist – php example code – part 4 – unsubscribe by e-mail


Article focus on mechanism for unsubscribing users from mailinglist by their e-mails. Because this part is meant to by available for admin, full list of subscribers are shown after all removing action for further look.

Form part

Form part is simplest ever, because sonsist only from one inputfiled gaining e-mail address to usubscribe from mailinglist.

<form method=“post“ action=“<?php echo $_SERVER[‚PHP_SELF‘]; ?>“>
          <div class=“form-group“>
              <label>e-mail to unsubscribe:</label>
              <input type=“text“ onfocus=“this.value=’@'“  name=“email“ class=“form-control“ value=“<?php echo isset($_POST[‚email‘]) ? $email : ‚Write e-mail address to unsubscribe here‘; ?>“>
          <button type=“submit“ name=“submit“ class=“btn btn-warning“> Unsubscribe </button>

Unsubscribe by e-mail php code

For finding and removing unwanted subscribers from a database table is used simple matching e-mail adress.

    // two variables for message and styling of the mesage with bootstrap
    $msg = “;
    $msgClass = “;

    // default values of auxiliary variables
    $email =““;

    $is_removed = false; //before hitting submit button no result is available
    if(filter_has_var(INPUT_POST, ‚submit‘)){
        // Data obtained from $_postmessage are assigned to local variables
        $email = htmlspecialchars($_POST[‚email‘]);
        // Controll if all required fields was written
        if(!empty($email) ) {
            // If check passed – all needed fields are written
            if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
                // E-mail is not walid
                $msg = ‚Please use a valid email‘;
                $msgClass = ‚alert-danger‘;
            } else {
                // E-mail is valid – now delete row with matching e-mail

                        // make database connection
                    $dbc = mysqli_connect(„localhost“, „admin“, „test*555“, „test“);

                    // Check connection
                        if($dbc === false){
                            die(„ERROR: Could not connect to database. “ . mysqli_connect_error());

                    // create DELETE query
                    $sql = „DELETE FROM mailinglist WHERE email = „.“‚$email'“ ;

                        if(mysqli_query($dbc, $sql)){
                            $msg = ‚Subscriber with e-mail: ‚.$email. ‚ has been succesfully removed from mailinglist.‘;
                            $msgClass = ‚alert-success‘;
                            $is_removed = true;

                            // clear entry fields after sucessfull deleting from database
                        } else {
                            $msg = „ERROR: Could not able to execute $sql. “ . mysqli_error($dbc);
                            $msgClass = ‚alert-danger‘;
                            $is_removed = false;

                    // end connection

        } else {
            // Failed – if not all fields are fullfiled
            $msg = ‚Please fill in all fields‘;
            $msgClass = ‚alert-danger‘; // bootstrap format for allert message with red color

    // if reset button clicked
    if(filter_has_var(INPUT_POST, ‚reset‘)){
        $msg = “;
        $msgClass = “; // bootstrap format for allert message with red color
        $subject =“;
        $email =“;

Full code of mailinglist app can be obtained from here.

Share the article via the network
Translate »