Developing the Mailing Mechanism

Developing the Mailing Mechanism with PHP
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.

ارایه درگاه اختصاصی پرداخت ePayBank.ir برای وب مستران و صاحبان سایت ایرانی

آیا اطلاع دارید که برای افزایش فروش باید تبلیغات کنید؟ بهرتین سایت تبلیغاتی MyCityAd.ir هست

آیا میخواهید بسته آموزشی طراحی سایت اختصاصی را از mortezasaheb.ir خریداری نمایید ؟

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
1: <?php
2: if (!$_POST) {
3: //haven’t seen the form, so display it
4: echo “<html>
5: <head>
6: <title>Send a Newsletter</title>
7: </head>
8: <body>
9: <h1>Send a Newsletter</h1>
10 <form method=\”post\” action=\”".$_SERVER["PHP_SELF"].”\”>
11 <p><strong>Subject:</strong><br/>
12 <input type=\”text\” name=\”subject\” size=\”30\”></p>
13 <p><strong>Mail Body:</strong><br/>
14 <textarea name=\”message\” cols=\”50\” rows=\”10\”
15 wrap=\”virtual\”></textarea>
16: <p><input type=\”submit\” name=\”submit\” value=\”Send It\”></p>
17: </form>
18: </body>
19: </html>”;
20: } else if ($_POST) {
21: //want to send form, so check for required fields
22: if (($_POST["subject"] == “”) || ($_POST["message"] == “”)) {
23: header(“Location: sendmymail.php”);
24: exit;
25: }
26:
27: //connect to database
28: $mysqli = mysqli_connect(“localhost”, “joeuser”,
29: “somepass”, “testDB”);
30:
31: if (mysqli_connect_errno()) {
32: //if connection fails, stop script execution
33: printf(“Connect failed: %s\n”, mysqli_connect_error());
34: exit();
35: } else {
36: //otherwise, get emails from subscribers list
37: $sql = “SELECT email FROM subscribers”;
38: $result = mysqli_query($mysqli, $sql)
39: or die(mysqli_error($mysqli));
40:
41: //create a From: mailheader
42: $mailheaders = “From: Your Mailing List
43: <you@yourdomain.com>”;

44: //loop through results and send mail
45: while ($row = mysqli_fetch_array($result)) {
46: set_time_limit(0);
47: $email = $row["email"];
48: mail(“$email”, stripslashes($_POST["subject"]),
49: stripslashes($_POST["message"]), $mailheaders);
50: echo “newsletter sent to: “.$email.”<br/>”;
51:
}
52: mysqli_free_result($result);
53: mysqli_close($mysqli);
54: }
55: }
56: ?>

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!