- Developing the Subscription Mechanism
- Developing the Mailing Mechanism
- Summary
- Q&A
- Workshop
Developing the Mailing Mechanism
With the subscription mechanism in place, you can create a basic form interface for a script that takes the content of your form and sends 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.3.
Listing 19.3 Send Mail to Your List of Subscribers
1: <?php 2: include 'ch19_include.php'; 3: if (!$_POST) { 4: //haven't seen the form, so display it 5: $display_block = <<<END_OF_BLOCK 6: <form method="POST" action="$_SERVER[PHP_SELF]"> 7: 8: <p><label for="subject">Subject:</label><br/> 9: <input type="text" id="subject" name="subject" size="40" /></p> 10: 11: <p><label for="message">Mail Body:</label><br/> 12: <textarea id="message" name="message" cols="50" rows="10"></textarea></p> 13: <button type="submit" name="submit" value="submit">Submit</button> 14: </form> 15: END_OF_BLOCK; 16: } else if ($_POST) { 17: //want to send form, so check for required fields 18: if (($_POST['subject'] == "") || ($_POST['message'] == "")) { 19: header("Location: sendmymail.php"); 20: exit; 21: } 22: 23: //connect to database 24: doDB(); 25: 26: if (mysqli_connect_errno()) { 27: //if connection fails, stop script execution 28: printf("Connect failed: %s\n", mysqli_connect_error()); 29: exit(); 30: } else { 31: //otherwise, get emails from subscribers list 32: $sql = "SELECT email FROM subscribers"; 33: $result = mysqli_query($mysqli, $sql) 34: or die(mysqli_error($mysqli)); 35: 36: //create a From: mailheader 37: $mailheaders = "From: Your Mailing List 38: <you@yourdomain.com>"; 39: //loop through results and send mail 40: while ($row = mysqli_fetch_array($result)) { 41: set_time_limit(0); 42: $email = $row['email']; 43: mail("$email", stripslashes($_POST['subject']), 44: stripslashes($_POST['message']), $mailheaders); 45: $display_block .= "newsletter sent to: ".$email."<br/>"; 46: } 47: mysqli_free_result($result); 48: mysqli_close($mysqli); 49: } 50: } 51: ?> 52: <!DOCTYPE html> 53: <html> 54: <head> 55: <title>Sending a Newsletter</title> 56: </head> 57: <body> 58: <h1>Send a Newsletter</h1> 59: <?php echo $display_block; ?> 60: </body> 61: </html>
As in Listing 19.2, the file of user-defined functions is included on line 2. Although only the database connection function is used in this file, there’s no harm in having the other function in the file, as well.
The main logic of the script starts at line 3, where you determine whether the user has seen the form yet. If the presence of the $_POST variable is false, you know the user has not submitted the form; therefore, you must show the form.
Lines 5–15 create the form for sending the newsletter to your subscriber list, which uses $_SERVER[PHP_SELF] as the action (line 6), 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 displays as shown 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 sending the message, you must check for the two required items from the form in lines 18–20: $_POST['subject'] and $_POST['message']. If either of these items is not present, redirect the user to the form again.
If the required items are present, the script moves on to line 24, which calls the database connection function. A query is issued in line 33, 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 alphabetic order for whatever reason.
Lines 37–38 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 40, extracts the email addresses from the resultset one at a time. On line 41, you 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.
In lines 43–44, the mail is sent using the mail() function, inserting the values from the form where appropriate. Line 45 adds to a string that is later printed to the screen, which shows to whom the mail was sent. Figures 19.7 and 19.8 show the outcome of the script.
Figure 19.7 Mail has been sent!
Figure 19.8 The mail arrived safely.