Developing the Mailing Mechanism

With the subscription mechanism in place, you can create a basic form interface for a script that will take the contents of your form and send it to every address in your subscribers table. This is another one of those all-in-one scripts, called sendmymail.php, and it is shown in Listing 18.2.

Listing 18.2 Send Mail to Your List of Subscribers
  1: <?php
  2: if ($_POST[op] != "send") {
  3:    //haven't seen the form, so show it
  4:    print "
  5:    <HTML>
  6:    <HEAD>
  7:    <TITLE>Send a Newsletter</TITLE>
  8:    </HEAD>
  9:    <BODY>
 10:    <h1>Send a Newsletter</h1>
 11:    <form method=\"post\" action=\"$_SERVER[PHP_SELF]\">
 12:    <P><strong>Subject:</strong><br>
 13:    <input type=\"text\" name=\"subject\" size=30></p>
 14:    <P><strong>Mail Body:</strong><br>
 15:    <textarea name=\"message\" cols=50 rows=10 wrap=virtual></textarea>
 16:    <input type=\"hidden\" name=\"op\" value=\"send\">
 17:    <p><input type=\"submit\" name=\"submit\" value=\"Send It\"></p>
 18:    </FORM>
 19:    </BODY>
 20:    </HTML>";
 22: } else if ($_POST[op] == "send") {
 23:     //want to send form, so check for required fields
 24:     if (($_POST[subject] =="") || ($_POST[message] == "")) {
 25:        header("Location: sendmymail.php");
 26:        exit;
 27:    }
 29:    //connect to database
 30:    $conn = mysql_connect("localhost", "joeuser", "somepass")
 31:         or die(mysql_error());
 32:    mysql_select_db("testDB",$conn) or die(mysql_error());
 34:    //get emails from subscribers list
 35:    $sql = "select email from subscribers";
 36:    $result = mysql_query($sql,$conn) or die(mysql_error());
 38:    //create a From: mailheader
 39:    $headers = "From: Your Mailing List <>\n";
 41:    //loop through results and send mail
 42:    while ($row = mysql_fetch_array($result)) {
 43:        set_time_limit(0);
 44:        $email = $row['email'];
 45:        mail("$email", stripslashes($_POST[subject]),
 46:             stripslashes($_POST[message]), $headers);
 47:        print "newsletter sent to: $email<br>";
 48:    }
 49: }
 50: ?>

The main logic of the script starts right there at line 2, where we determine whether the user has seen the form yet. If the value of $_POST[op] is not "send", we know the user has not submitted the form; therefore, we must show it to her. Lines 4?20 create the form for sending the newsletter, which uses $_SERVER[PHP_SELF] as the action (line 11), creates a text field called subject for the subject of the mail, and creates a textarea called message for the body of the mail to be sent. At this point, the script breaks out of the if...else construct and the HTML is printed. The form is displayed as in Figure 18.6.

Figure 18.6. Form for sending the bulk mail.


If the value of $_POST[op] is indeed "send", however, we have to send the form to the recipients. Before we send, we must check for the two required items: $_POST[subject] and $_POST[message]. If either of these items is not present, the user is redirected to the form again.

If the required items are present, the script moves on to lines 30?32, which connect to the database. The query is issued in line 36, which grabs all the email addresses from the subscribers table. There is no order to these results, although you could throw an order by clause in there if you want to send them out in alphabetical order.

Line 39 creates a From: mail header, which is used inside the upcoming while loop, when mail is sent. This header ensures that the mail looks like it is from a person and not a machine. The while loop, which begins on line 42, extracts the email addresses from the resultset one at a time. On line 43, we use the set_time_limit() function to set the time limit to 0, or "no limit." Doing so allows the script to run for as long as it needs to.


Because all the script in Listing 18.2 does is execute the mail() function numerous times, it does not take into account the queuing factors in actual mailing list software, which are designed to ease the burden on your outgoing mail server. Using set_time_limit() does not ease its burden; it just allows the script to continue to run when it might have timed out before.

In line 45, the mail is sent using the mail() function, inserting the values from the form where appropriate. Line 46 prints a message to the screen for you, to show who should have received the mail. In Figure 18.7 and 18.8, you can see the outcome of the script.

Figure 18.7. Mail has been sent!


Figure 18.8. The mail arrived safely.


    Part III: Getting Involved with the Code