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 19.2.
By the Way
Before attempting to use the script in this section, make sure that you have read the section in Chapter 11, "Working with Forms," regarding the configuration in your php.ini file, which is required to send mail.
Listing 19.2. Send Mail to Your List of Subscribers
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 presence of the $_POST variable is false, we know the user has not submitted the form; therefore, we must show the form.
Lines 419 create the form for sending the newsletter to your subscriber list, which uses $_SERVER["PHP_SELF"] as the action (line 10), 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 19.6.
Figure 19.6. Form for sending the bulk mail.
If the presence of $_POST is not false, the script should send the form to the email addresses in the subscribers table. Before we send the message, we must check for the two required items from the form in lines 2225: $_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 2829, which connect to the database. A query is issued in line 37, 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 for whatever reason.
Lines 4243 create a From: mail header, which is used inside the upcoming while loop, when the mail is sent. This header ensures that the mail looks like it is from a person and not a machine because you've specifically provided a value in this string. The while loop, which begins on line 45, extracts the email addresses from the resultset one at a time. On line 46, 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.
By the Way
Because all the script in Listing 19.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 lines 4849, the mail is sent using the mail() function, inserting the values from the form where appropriate. Line 50 prints a message to the screen for you to show who should have received the mail. In Figures 19.7 and 19.8, you can see the outcome of the script.
Figure 19.7. Mail has been sent!
Figure 19.8. The mail arrived safely.