I.T. Discussion Community!
-Collapse +Expand
PHP
Search PHP Group:

Advanced
-Collapse +Expand PHP Store
PRESTWOODSTORE

Prestwood eMagazine

December Edition
Subscribe now! It's Free!
Enter your email:

   ► KBWebsite Scri...PHP   Print This     
 
An Easy To Use PHP Email Blast Utility
 
Posted 4 months ago on 8/3/2017
Take Away:

This article describes a simple to use PHP program I made for performing a mass email “broadcast” or “blast” to a mysql database full of email addresses.

KB102801



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. I am using [ in place of < and ] in place of > for article formatting purposes.

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

// 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: 

[H2]    My Message:  [textarea name="MESSAGEBOARD" cols="50" rows="10"][?php echo $var = isset($_SESSION['post_html']) ? $_SESSION['post_html'] : ''; ?][/textarea][/H2]

                [!--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 type="submit" name="submit_email" value="Send Email Blast"][input type="submit" name="submit_return_to" value="Return To Mail List"][/H2]

[!--upload an image file from user's local computer and subsequently paste in the message field.--]

[H2]    [label for="file"]Filename:    [/label][input type="file" name="file" id="file"][input type="submit" name="gogetem" value="Upload"][/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

.

.

.

// 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.


Comments

1 Comments.
Share a thought or comment...
First Comment
Comment 1 of 5

Your blog inspires me every time when I read it. I love reading blogs. I don't excite always by reading blog posts. But in your blog I find something that implies in blogging. Anyway, you are unquestionably a great blogger and you have a skill to make your reader to keep reading your blog again and again. Feel free to visit top essay writing service for any kind of essay writing.Thanks a lot for sharing.
Keep up the magnificent work.

Posted 116 days ago

Comment 2 of 5

Thank you....I'm glad you liked it.

Posted 114 days ago

Comment 3 of 5

Thank you....glad you liked it!

Posted 105 days ago

Comment 4 of 5

Thank you for sharing that information of PHP email blast utility. English Assignment Help in Australia make easy your assignment work to submit. 

Posted 80 days ago

Latest Comment
Comment 5 of 5

This would have been go through an email impact utility which additionally needed to encourage html so it could send through pictures and rich content. Despite the fact that there are PHP programming utilities that are now made for this I chose to make my own particular to Do my Essay for me learn more about how the procedure truly functions. I have included screen depictions and in addition programming code that shows a portion of the more particular custom programming that involves this email impact utility.

Posted 37 days ago
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1156A1
Enter key:
Article Contributed By Douglas.M:

Please visit my software developer website for more information about my services. I offer application development as well as Android app coding services. My developer skills are best suited to dealing with custom software projects. I can perform programming for Corel Paradox as well as C# Sharp and PHP.

In my local area of northeast Ohio, I can cater to computer repair and "fix my computer" issues.

Use my contact web page today to reach me about any software design ideas you have.

Visit Profile

 KB Article #102801 Counter
581
Since 8/3/2017
-
   Contact Us!
 
PrestwoodBoards.com was developed and is maintainted by me. Do you have a question or suggestion? Do you see a problem? Contact me now. My goal is to build an ad-free and spam-free source of I.T. information with many contributers (ok to promote your website/company in your bio). Yes, my company Prestwood IT Solutions is mentioned in my bio which shows with every post, but you can contribute and promote your pet project too!

1,800 People Online Now!!  
Sign In to see who's online now!  Not a member? Join now. It's free!
Show more stats...


©1995-2017 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech