This hour provides the first of several hands-on, small
projects designed to pull together your PHP and MySQL knowledge. In this hour,
you'll learn the methods for creating a managed distribution list, which can be
used to send out newsletters or anything else that you want to send, to a list
of email addresses in a database.
The mailing mechanism you'll use in this hour is not meant to
be a replacement for mailing list software, which is specifically designed for
bulk messages. The type of system you'll build in this lesson should be used for
only small lists of fewer than a few hundred email addresses.
In this hour, you will learn how to
-
Create a subscribe/unsubscribe form and script
-
Create a front end for sending your message
-
Create the script that sends your message
Developing the Subscription Mechanism
-
A table to hold email addresses
-
A way for users to add or remove their email addresses
-
A form and script for sending the message
The following sections will describe each item
individually.
Creating the subscribers Table
You really need only one field in the
subscribers table: to hold the email address of the user. However, you
should have an ID field just for consistency among your tables, and also because
referencing an ID is a lot simpler than referencing a long email address in
where clauses. So, in
this case, your MySQL query would look something like
mysql> create table subscribers ( -> id int not null primary key auto_increment, -> email varchar (150) unique not null -> ); Query OK, 0 rows affected (0.00 sec) |
Note the use of unique in the field definition for email. This means that although id is the primary key, duplicates should not be allowed in the email field either. The email field is a unique key, and id is the primary key.
This relationship is represented in the table information as
MUL (or "multiple") in the Key field:
mysql> describe subscribers;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | | PRI | NULL | auto_increment |
| email | varchar(150) | YES | MUL | NULL | |
+-------+--------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
Now that you have a table, you can create the form and script that place values in there.
Creating the Subscription Form
The subscription form will actually be an all-in-one form and script called manage.php, which
will handle both subscribe and unsubscribe requests. Listing 18.1 shows the code for manage.php, which uses a
few user-defined functions to eliminate repetitious code.
Comments
Post a Comment