Click here to Skip to main content
Click here to Skip to main content

An Easy to Use PHP "Email Blast" Utility

By , 11 Dec 2013
Rate this:
Please Sign up or sign in to vote.

Introduction

Recently I was working on a website for a customer who asked me to import a mailing list to a MySQL database table on the site. This was going to be run through an email blast utility, which also had to facilitate HTML so it could send through images and rich text. Although there are PHP software utilities that are already made for this, I decided to make my own for the purpose of learning more about how the process really works. I have included screen snapshots as well as programming code that illustrates some of the more specialized custom software that comprises this email blast utility.

Select recipients for the email blast from a list

The operator is first required to log in with a user ID and password that will be authenticated with its stored counterparts in a MySQL data table on the site (see image below).

Here is the PHP code that will authenticate the user’s log in credentials after the Login button has been clicked:

<?php
 
.
.
.
 
   // connect to the mysql database with predefined
   // constants from the included 'generic_config.php' file.
   $conn = mysql_connect(DB_HOSTX, DB_USERX, DB_PASSWORDX) or die('Could not connect: ' . mysql_error());
   $db_selected = mysql_select_db(DB_NAMEX, $conn) or die('Could not select database');  
   $query_rsLogin = "SELECT * FROM generic_admin_datatable";
   $rsLogin = mysql_query($query_rsLogin, $conn) or die(mysql_error());
   $totalRows_rsLogin = mysql_num_rows($rsLogin);
      if ( $totalRows_rsLogin >= 1 ) {
      $flagvar3 = 1;
      $rowx = mysql_fetch_row($rsLogin);
         if ( $_POST['uservar'] == $rowx[0] && $_POST['passvar'] == $rowx[1] ) {
 
         // set the php session variable to 'YES' so the
         // email address listing page won't bounce us
         // back to this one.
         $_SESSION['getin'] = "YES";
         // initialize the php session variable to the
         // first row of the listed email addresses.
         $_SESSION[start_row] = 0;
         $flagvar3 = 0;
 
         // set the 'blastready' field for all the email
         // addresses to 'NO'.
         mysql_query("UPDATE generic_mailing_list SET blastready = 'NO'") 
           or die('Query failed: ' . mysql_error());
 
         }
      }
   mysql_free_result($rsLogin);
   mysql_close($conn);
 
      // redirect to the email address listing page.
      header("Location: http://www.genericsite.com/blast/emailblastlist.php");
 
.
.
.
 
?>

Upon successful log in, the operator will be redirected to a web page that shows a listing of email recipients (see image below). This is populated with email addresses and the first and last names from the MySQL data table on the site. Notice that there are two sets of navigation buttons at the top. The operator may move backwards and forwards through the list by 12 or 100 rows with each click.

If the operator wants to do a selective email blast to the list of recipients, then the Add To Blast link to the right of each chosen row needs to be clicked. This will change the default status of NO under the Email Blast? column to YES (see image below).

Here is the PHP code that switches the Email Blast? column to YES for a selected row from the listing of recipients:

<?php

session_start();                                                                    

// this configuration file contains predefined
// constants used to connect to the server and database.
include 'generic_config.php';
 
   // assign the passed email address variable into
   // a php session variable.
   $_SESSION[at] = $_GET['var'];
 
      // connect to the mysql database with predefined
      // constants from the included 'generic_config.php' file.
      $conn = mysql_connect(DB_HOSTX, DB_USERX, DB_PASSWORDX) or 
                   die('Could not connect: ' . mysql_error());
      $db_selected = mysql_select_db(DB_NAMEX, $conn) or die('Could not select database');  
 
     // set the 'ready for email blast' column in the listing to 'YES' for
     // the passed email address.
     mysql_query("UPDATE generic_mailing_list SET blastready = 'YES' WHERE emailaddress = '".
       $_SESSION[at]."'") or die('Query failed: ' . mysql_error());
     mysql_close($conn);

    // redirect to the email address listing page.
    header("Location: http://www.genericsite.com/blast/emailblastlist.php"); 
 
?>

If sending an email to all recipients in the listing is desired, then nothing needs to be clicked on this web page. That will be taken care of in the next one where the email is being composed for the blast operation. To go to that web page, just click the Email Blast button at the top of the page.

Next, fill out the email web page and send out the blast!

This next web page is where the operator will compose the content for the email blast (see image below).

Notice the Who to email: combo box control. By default it is set to Selected for a selective email blast operation. As mentioned previously, this designator is meant for choosing specific email recipients from the prior web page. The All designator will blast the email to all recipients from the list.

Also, the text area for the message body has been embellished with a rich text editor, CKEDITOR. I declared the text area for the email message, which I named MESSAGEBOARD. I then used the script tag enclosed directive CKEDITOR.replace(‘MESSAGEBOARD’) to integrate the rich text editor into the text area as shown below:

.
.
.
 
<br><br>
<H1>EMAIL BLAST</H1>
<br><br>
 
<!--inputs for subject and message.-->
<H2>    My Subject:  <input name="SUBJECT" type="text" 
  size="40" maxlength="40" value="<?php echo $_SESSION['mysubject']; ?>"></H2>
<H2>    My Message:  <textarea name="MESSAGEBOARD" 
  cols="50" rows="10"><?php echo $var = isset($_SESSION['post_html']) ? 
  $_SESSION['post_html'] : ''; ?>

Next, the operator begins to compose the email blast message as shown below:

Notice the file name of an image file has been selected using the Browse button from the file upload feature near the bottom of the web page. You can see the name of the selected image file just to the right of the Browse button. If an upload image file has not been selected, then it will say No file selected. instead, as shown in the snapshot that preceded this one. The Upload button has not yet been clicked, but here is the code it runs in PHP when it is:

<?php
 
.
.
.
 
// upload an image to the website in the folder called
// 'uploads_4_emailblast'. this will be positioned in
// the message area where the text cursor is.
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
} else {
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
 
move_uploaded_file($_FILES["file"]["tmp_name"], 
  "/home1/generic/public_html/uploads_4_emailblast/" . $_FILES["file"]["name"]);
$_SESSION['post_html'] = $_POST['MESSAGEBOARD'] . "<br>";
$_SESSION['post_html'] .= "<p><img src='http://www.genericsite.com/uploads_4_emailblast/" . 
  $_FILES["file"]["name"] . "'></p><br>";
}
 
.
.
.
 
?>

After the selected image file has been uploaded to the text area where the cursor is positioned, the result looks like this:

Next, the Send Email Blast button is clicked and the composed email in the web page is blasted to the chosen recipients from the listing web page. Below is the PHP code for this. Notice how I include the headers $headers .= "MIME-Version: 1.0\r\n"; and "$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";" to facilitate HTML in the body of the message:

<?php

session_start();
 
// this configuration file contains predefined
// constants used to connect to the server and database.
include 'generic_config.php';

// initialize data verification variables.
$flagvar = 0;
$flagvar2 = 0;
$flagvar3 = 0;
 
   // processing for sending out the "email blast".
   if ($_POST['submit_email']) {
 
      // test to see if the subject and message fields are empty.
      if ( empty($_POST['SUBJECT']) ) {
      $flagvar2 = 1;
      $flagvar = 1;
      }
      if ( empty($_POST['MESSAGEBOARD']) ) {
      $flagvar3 = 1;
      $flagvar = 1;
      }
 
     // if subject and message fields were typed into,
     // then proceed with "email blast".
     if ( $flagvar == 0 ) {

     // remove slashes from the message.
     $body = $_POST['MESSAGEBOARD'];
     $body = stripslashes($body)."<br><p>Email Sender: anyone@wherever.com</p>";

     // set headers for 'From' field and also set up
     // to format html.
     $headers = "From: anyone@wherever.com\r\n";
     $headers  .= "MIME-Version: 1.0\r\n";
     $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

     // connect to the mysql database with predefined
     // constants from the included 'generic_config.php' file.
     $conn = mysql_connect(DB_HOSTX, DB_USERX, DB_PASSWORDX) or die('Could not connect: ' . mysql_error());
     $db_selected = mysql_select_db(DB_NAMEX, $conn) or die('Could not select database');  

    // if the dropdown box was clicked for 'Selected', then
    // perform the "email blast" for records in the mail list
    // table where the 'blastready' field is equal to 'YES'.
    // otherwise, "email blast" for everyone.
    if ( $_POST['towhom'] == "Selected" ) {
    $query_rsLogin = "SELECT * FROM generic_mailing_list WHERE blastready = 'YES'";
    } else {
    $query_rsLogin = "SELECT * FROM generic_mailing_list";
    }
 
// create queried resource '$rsLogin' and get
// the total number of rows.
$rsLogin = mysql_query($query_rsLogin, $conn) or die(mysql_error());
$totalRows_rsLogin = mysql_num_rows($rsLogin);
 
   // send out "email blast" if the total rows in the previously
   // queried result is greater than or equal to 1.
   if ( $totalRows_rsLogin >= 1 ) {
      while ($rowx = mysql_fetch_row($rsLogin)) {
      // get the email address from the queried result.
      $emailx = $rowx[2];
      // send the email, then loop around for next row
      // in the queried result.
      mail($emailx, $_POST['SUBJECT'], $body, $headers);
      }
   }
     // free the queried data resource.
     mysql_free_result($rsLogin);
     // close the server connection.
     mysql_close($conn);

     }
 
   }
 
.
.
.
 
?>

Conclusion

This is an easy to use utility that makes short work of sending out an email blast. It’s perfect for email marketing, invitations, greetings, etc. This PHP code can be easily modified to accommodate a wide range of email blast objectives. Aside from my application development services, I also offer computer repair and fix my computer expertise for customers in my local area of northeast Ohio, USA.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

doug433
Software Developer DOUGLAS B. MILLER, COMPUTER PROGRAM DESIGNER
United States United States
No Biography provided
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 11 Dec 2013
Article Copyright 2013 by doug433
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid