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
-
-
- $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] ) {
-
-
-
-
- $_SESSION['getin'] = "YES";
-
-
- $_SESSION[start_row] = 0;
- $flagvar3 = 0;
-
-
-
- mysql_query("UPDATE generic_mailing_list SET blastready = 'NO'") or die('Query failed: ' . mysql_error());
-
- }
- }
- mysql_free_result($rsLogin);
- mysql_close($conn);
-
-
- 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 there are 2 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 listing 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();
-
-
-
- include 'generic_config.php';
-
-
-
- $_SESSION[at] = $_GET['var'];
-
-
-
- $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');
-
-
-
- mysql_query("UPDATE generic_mailing_list SET blastready = 'YES' WHERE emailaddress = '".$_SESSION[at]."'") or die('Query failed: ' . mysql_error());
- mysql_close($conn);
-
-
- 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'] : ''; ?>
-
- <!--embellish the message field with CKEditor to format for rich text.-->
- <script>
- CKEDITOR.replace( 'MESSAGEBOARD' );
- </script>
-
- <!--dropdown list for 'Selected' or 'All' to direct php email blast routine for what type of blast.-->
- <h2> Who to email:
- <select name="towhom" size="2">
- <option selected="">Selected</option>
- <option>All</option>
- </select>
- </h2>
-
- <!--click to go to email blast scroll list or email blast processing.-->
- <h2> <input name="submit_email" value="Send Email Blast" type="submit"><input name="submit_return_to" value="Return To Mail List" type="submit"></h2>
-
- <!--upload an image file from user's local computer and subsequently paste in the message field.-->
- <h2> <label for="file">Filename: </label><input name="file" id="file" type="file"><input name="gogetem" value="Upload" type="submit"></h2>
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
-
-
-
- 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();
-
-
-
- include 'generic_config.php';
-
-
- $flagvar = 0;
- $flagvar2 = 0;
- $flagvar3 = 0;
-
-
- if ($_POST['submit_email']) {
-
-
- if ( emptyempty($_POST['SUBJECT']) ) {
- $flagvar2 = 1;
- $flagvar = 1;
- }
- if ( emptyempty($_POST['MESSAGEBOARD']) ) {
- $flagvar3 = 1;
- $flagvar = 1;
- }
-
-
-
- if ( $flagvar == 0 ) {
-
-
- $body = $_POST['MESSAGEBOARD'];
- $body = stripslashes($body)."<br><p>Email Sender: [email protected]</p>";
-
-
-
- $headers = "From: [email protected]\r\n";
- $headers .= "MIME-Version: 1.0\r\n";
- $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
-
-
-
- $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 ( $_POST['towhom'] == "Selected" ) {
- $query_rsLogin = "SELECT * FROM generic_mailing_list WHERE blastready = 'YES'";
- } else {
- $query_rsLogin = "SELECT * FROM generic_mailing_list";
- }
-
-
-
- $rsLogin = mysql_query($query_rsLogin, $conn) or die(mysql_error());
- $totalRows_rsLogin = mysql_num_rows($rsLogin);
-
-
-
- if ( $totalRows_rsLogin >= 1 ) {
- while ($rowx = mysql_fetch_row($rsLogin)) {
-
- $emailx = $rowx[2];
-
-
- mail($emailx, $_POST['SUBJECT'], $body, $headers);
- }
- }
-
- mysql_free_result($rsLogin);
-
- 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.