Mailinglist – php example code – part 3 – mailer page

Article focus on part responsible for creating a post and resending them to a subscribers. List of subscribers is also shown.

Form part

Sending of separate information messages to subscribers is enabled by mailer.php page. Form part of the page consist from two filed. Simple input text filed for subject. Second much bigger textarea for gaining text of message from page admin.

Look at GUI of mailer.php page

 <form method=“post“ action=“<?php echo $_SERVER[‚PHP_SELF‘]; ?>“>
          <div class=“form-group“>
              <label>Subject of send message:</label>
              <input type=“text“ onfocus=“this.value=““  name=“subject“ class=“form-control“ value=“<?php echo isset($_POST[‚firstname‘]) ? $subject : ‚Subject of message:‘; ?>“>
 
              <label>Message to send:</label>
              <textarea onfocus=“this.value=““ id=“message“ name=“message“ class=“form-control“ rows=“10″ cols=“50″><?php echo isset($_POST[‚message‘]) ? $message : ‚Your text goes here …‘; ?></textarea>
          </div>
          
              
          <button type=“submit“ name=“submit“ class=“btn btn-warning“> Send to subscribers </button>
          <button type=“submit“ name=“reset“ class=“btn btn-info“> Reset form </button>

Interesting part of code is inserted in input tag  onfocus=“this.value=““ that enable clearing information „value“ text inserted into a form field.

Sending e-mail-s

Part for sending a e-mails is inserted into a HTML body because we will produce messages after all succesfully sent e-mails. Full code can be obtainted for further reference and study from github here.

 <?php // if message to send was submitted then emails are sent mail by mail

      // Control if data was submitted
    if(filter_has_var(INPUT_POST, ‚submit‘)){
        // $subject and $message was aded to variables in scrit on upper part of page, because we expect outpu about sending email
        // in body of page thic code is inserted in html body part of code
        

        // Controll if all required fields was written
        if(!empty($subject) && !empty($message)) {
            // If check passed – all needed fields are written
            $is_result = true;
            // send e-mail to all subscribers

                // connect to database
                $dbc = mysqli_connect(„localhost“, „admin“, „test*555“, „test“);
 
                // Check connection
                    if($dbc === false){
                        die(„ERROR: Could not connect to database. “ . mysqli_connect_error());
                    }
            
                // read all e-mails from database – create query and pass it to database server

                $sql = „SELECT DISTINCT email FROM mailinglist“;

                if($output = mysqli_query($dbc, $sql)){
                    if(mysqli_num_rows($output) > 0){  // if any record obtained from SELECT query
                        
                        // create  and send email one by one
                        
                        echo „<h4>Sending e-mails</h4>“;
                        echo „<br>“;
    
                        while($row = mysqli_fetch_array($output)){ //send email by email and output message
                            // create email structure
                            // E-mail is ok
                                $fromEmail = ‚ciljak@localhost.org‘; //!!! e-mail address from message is send – change for your needs!!!
                                $toEmail = $row[‚email‘];
                                $body = $message;

                                // Email Headers
                                $headers = „MIME-Version: 1.0″ .“\r\n“;
                                $headers .=“Content-Type:text/html;charset=UTF-8″ . „\r\n“;

                                // Additional Headers
                                $headers .= „From: CDesigner.eu  <„.$fromEmail.“>“. „\r\n“;

                                if(mail($toEmail, $subject, $body, $headers)){
                                    // Email Sent
                                    echo „<p> Email to: „;
                                    echo “ „ . $row[‚email‘] . “ „;
                                    echo “  has been sent … </p>„;
                                   
                                } else {
                                    // Failed
                                    echo „<p> Email to: „;
                                    echo “ “ . $row[‚email‘] . „ „;
                                    echo “  cannot be send, please examine your email server connection! </p>„;
                                }

                            
                                
                        }
                        echo „<br>“;
                        // Free result set – free the memory associated with the result
                        mysqli_free_result($output);
                    } else{
                        echo „There is no subscriber in mailinglist. Please add them.“; // if no records in table
                    }
                } else{
                    echo „ERROR: Could not able to execute $sql. “ . mysqli_error($dbc); // if database query problem
                }
    
                // Close connection
                mysqli_close($dbc);
               
                
        } else {
            // Failed – if not all fields are fullfiled
            $msg = ‚Please fill in all contactform fields‘;
            $msgClass = ‚alert-danger‘; // bootstrap format for allert message with red color
        }; 

    };  

      ?>

Listener of subscribers part

Our next php code part is responsible for showing list of subscribers in form of a table. For styling of the output, some css was added to style.css file (github link is here).

<?php // code showing all subscribers in form of a table at end of the page

            /* Attempt MySQL server connection. Assuming you are running MySQL
            server with default setting (user ‚root‘ with no password) */
            $dbc = mysqli_connect(„localhost“, „admin“, „test*555“, „test“);
            
            // Check connection
            if($dbc === false){
                die(„ERROR: Could not connect to database – stage of article listing. “ . mysqli_connect_error());
            }
            
            
                
                        
            // read all rows (data) from guestbook table in „test“ database
            $sql = „SELECT FROM mailinglist ORDER BY id DESC„;  // read in reverse order – newest article first
            /*****************************************************************/
            /*   Output in Table – solution 1 – for debuging data from database     */
            /*****************************************************************/
            // if data properly selected from mailinglist database tabele
            
            echo „<h4>Our subscribers mailinglist</h4>„;
            echo „<br>„;
            echo ‚ <button class=“btn btn-secondary btn-lg “ onclick=“location.href=\’unsubscribe.php\'“ type=“button“>  Unsubscribe by e-mail -> </button>‚;
            
            echo „<br>“; echo „<br>„;
            
                if($output = mysqli_query($dbc, $sql)){
                    if(mysqli_num_rows($output) > 0){  // if any record obtained from SELECT query
                        // create table output
                        echo „<table>“; //head of table
                            echo „<tr>„;
                                echo „<th>id</th>„;
                                echo „<th>firstname</th>„;
                                echo „<th>lastname</th>„;
                                echo „<th>date</th>„;
                                echo „<th>email</th>„;
                                
                            echo „</tr>„;
                        while($row = mysqli_fetch_array($output)){ //next rows outputed in while loop
                            echo “ <div class=\“mailinglist\“> “ ;
                            echo „<tr>„;
                                echo „<td>“ . $row[‚id‘] . „</td>„;
                                echo „<td>“ . $row[‚firstname_of_subscriber‘] . „</td>„;
                                echo „<td>“ . $row[‚secondname_of_subscriber‘] . „</td>„;
                                echo „<td>“ . $row[‚write_date‘] . „</td>„;
                                echo „<td>“ . $row[‚email‘] . „</td>„;
                            echo „</tr>„;
                            echo “ </div> “ ;
                        }
                        echo „</table>„;
                        // Free result set
                        mysqli_free_result($output);
                    } else{
                        echo „There is no postmessage in Guestbook. Please wirite one.„; // if no records in table
                    }
                } else{
                    echo „ERROR: Could not able to execute $sql. “ . mysqli_error($dbc)// if database query problem
                }
            
            

            // Close connection
            mysqli_close($dbc);
            ?>

Full mailinglist app code is available from here.