How to Send a Mass Email in PHP Batches

How to Send a Mass Email in PHP Batches thumbnail
PHP makes scripting mass email campaigns possible.

Hypertext Preprocessor (PHP) is a full-featured scripting language with built-in functions to accomplish just about any task a web developer needs to perform. PHP's toolkit includes an email function, array-processing abilities, loop-control structures and the means to read and write local files. A web developer can combine these tools and create a PHP script that performs mass emailings. Splitting a mass email job into small batches allows you to work within the limits your web server places on the use of its send-mail or other email applications.

Things You'll Need

  • Plain Text Editor
  • Access to a web server with PHP installed
  • File Transfer Protocol (FTP) application
Show More

Instructions

    • 1

      Launch the standard, plain-text editor application that is available on your computer.

    • 2

      Enter the following code into the text editor:

      <?php

      // email list -- these could also be stored in a data base

      $list[0]="sndrowjaw@cuttee.com";

      $list[1]="pi23maryd@juno.com";

      $list[2]="ba3yyyy1@cuttee.com";

      $list[3]="cibolhophabolh@mboln.com";

      $list[4]="qoydrter@aok1.com";

      $list[5]="ckebolboled245@blaboltmail.com";

      $list[6]="coccy.foreman@gaik.com";

      $list[7]="zoggit@aok.com";

      $list[8]="cpmcdona@drokina.rr.com";

      $list[9]="trobole@coice-wi23ibol.com";

      $liststep=2; // number of emails to send out at a time

      $TotalAddresses=10;

      $myFile = "listcount.txt";

      // checks for existence of count file. If not there, creates it

      // if it exists, it loads the count, increments it and passes it on to the mailer routine

      if (file_exists($myFile))

      {

      $fh = fopen($myFile, 'r');

      $count=intval(fgets($fh));

      $count=$count+$liststep;

      fclose($fh);

      $fh = fopen($myFile, 'w');

      fwrite( $fh,$count);

      fclose($fh);

      }

      else {

      $fh =fopen($myFile, 'x');

      $count=$liststep;

      fwrite($fh, $count);

      fclose($fh);

      }

      // your email address and email subject

      $email='myorganization@myemailservice.com';

      $subject="meeting announcement";

      // headers to tell where mail from and who to reply to

      $headers = 'From: ' . $email . "\r\n" . 'Reply-To: ' . $email . "\r\n" ;

      $themessage='Here is my message. It could be HTML as well: just have to watch the single and double quotes';

      // mailer routine

      // this section loops through the email address list until the count reaches 10

      // uncomment the mail() function line when ready to actually send out email.

      if ($count<$TotalAddresses)

      {

      for ($n=0;$n<$count;$n++)

      {

      // $result=mail($list[$n], "Meeting Invitation", $themessage,$headers);

      if ($result=True)

      {

      echo $list[$n].' sent! '; // if the mail function is successful

      }

      }

      }

      ?>

    • 3

      Click the file menu. Select the "Save" option. Save under the file name "test.php."

    • 4

      Click on the file menu and select "Quit" or "Exit" to close the text editor.

    • 5

      Launch the FTP application and log in to the web server.

    • 6

      Upload the files "test.php" to the root directory of the web server.

    • 7

      Launch a web browser and enter the appropriate Uniform Resource Locator (URL) to access the PHP file. For example, enter: yourdomainname.com/test.php. Replace "yourdomainname.com" with the domain name or Internet Protocol (IP) address of the web server.

    • 8

      Press the enter key to load the URL and run the PHP file. The browser should display each email address successfully sent to the web host's send-mail utility.

Tips & Warnings

  • Linux, OS X, BSD and UNIX servers have a standard utility, cron, that you may be able to use to send a batch of email at regular intervals. Windows servers may or may not have cron or a cron-like utility you can utilize. Cron is run on most systems as a background process or daemon. Cron checks to see if a job must be run daily, hourly, weekly or monthly. Talk with your system administrator about running an hourly or customized cron script to send out your emails in small batches throughout the day.

  • In addition to limiting the number of emails you may send out per day, some web servers may even limit how many emails you can send out per hour. If the mail() function is called too rapidly, the server may ignore some emails or block your ability to email entirely. PHP's usleep() function can be used to delay the email submission to an acceptable rate. The amount of delay is in millionths of a second. Insert usleep() somewhere after the mail() function as "usleep(2000000)" for a two-second delay between mailings.

Related Searches:

References

  • Photo Credit Comstock/Comstock/Getty Images

Comments

Related Ads

Featured