How to Batch Send Email in PHP

How to Batch Send Email in PHP thumbnail
PHP makes sending bulk emails easier.

Whatever you can think of doing on the Internet, chances are the scripting language PHP has a function for it. From its humble beginnings as a tool for maintaining websites, PHP has grown into a full-featured tool for Web application developers, server and database administrators, and Web masters. One example of the breadth of PHP's built-in toolkit is the mail() function, which uses the Web host's "sendmail" utility to send email. You can use the mail() function in a PHP script to read a list of email addresses and send out an email for each one.

Things You'll Need

  • Plain text editor
  • Standard Web browser
  • Access to a Web server with PHP installed
  • File Transfer Protocol (FTP) application
Show More

Instructions

    • 1

      Launch the standard, plain text editor available on your computer.

    • 2

      Enter the following code into the text editor. Use real addresses in the email list. Change the "$email" variable to your email address. Change the subject and the message strings as well.

      <?php

      // email list

      $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";

      // 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';

      // this section loops through the email address list

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

      {

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

      if ($result=True) {

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

      }

      }

      ?>

    • 3

      Click "File" and "Save." Save under the file name "test.php."

    • 4

      Click "File" and "Quit" or "Exit" to close the text editor.

    • 5

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

    • 6

      Upload the file "test.php" to the root directory of the Web server.

    • 7

      Launch a Web browser, and enter the appropriate URL to access the PHP file.

      http://yourdomainname.com/test.php

      Replace "yourdomainname.com" with the domain name or IP address of the Web server.

    • 8

      Press "Enter" to load the URL and run the PHP file. The browser should display each email address successfully sent to the Web host's "sendmail" utility.

Tips & Warnings

  • Even if your script is flawless and PHP's mail() function is given the correct parameters, emails sometimes do not get delivered. For some reason, the receiver's email service is not able to deliver your email. Your mail server may be sent a notification called a bounce. You may or may not be notified of bounced mail. You can add a parameter to your email header section that will tell the receiver's mail server to send any bounced mail directly to you for review. Set up another email account and add the address to the header as in the example below.

  • // replace this address with your new bounce address

  • $returnpath='Return-Path: mybounceaddress@myemailservice.com\r\n';

  • $returnreceipt="Return-Receipt-To: mybounceaddress@myemailservice.com\r\n';

  • $header=$header.$returnpath.$returnreceipt; // add this to the header using concatenation

  • Some Web servers may limit the number of emails you may send out per day. Some may also limit the rate at which mail may be sent out: If the mail() function is called too quickly, the server may intentionally throttle the rate or it may even skip over some emails while trying to keep up the pace. PHP's usleep() function can be used to slow down the email submission rate. The amount of delay is in millionths of a second. A two-second delay would be usleep(2000000).

Related Searches:

References

Resources

  • Photo Credit Medioimages/Photodisc/Photodisc/Getty Images

Comments

You May Also Like

Related Ads

Featured