This tutorial covers how to create a mailing list using PHP. It's actually rather long, as it covers subscribing, admin, and unsubscribing. We use a dynamic URL to contain it to one file.(This is pretty nice)
I'm going to show it in bits, so it should be easy enough to understand
I: Setup
Get a database, and table set up, name the table list.
(For a new table cPanel > MySQL DB's > Bottom of the page, link to phpMyAdmin > Click your DB from the drop down > click the SQL tab at the top)
When you get there, paste this:
PHP Code:
CREATE TABLE `list` (
`email` VARCHAR( 255 ) NOT NULL ,
`ip` VARCHAR( 255 ) NOT NULL
);
If your wondering why IP is there, its so that you can see if anyone has entered twice, not that it matters, just cool. :cool:
II: Basic variables
This basically has the setup vars, very short and simple:
PHP Code:
//connect to the database, enter your settings
mysql_connect("localhost", "username", "password") or die("Error: ". mysql_error() ."");
mysql_select_db("username_db") or die("Error: ". mysql_error() ."");
//dynamic URL part is do...!
$do = $_GET['do'];
//password to send the emails
$thepassword = "google";
//don't edit this var below!
$encrypted = md5($thepassword);
//whatever you want the subject to be, probaly include newsletter
$subject = "Newsletter from: YourSite";
//Who its from
$youremail = "newsletter@yoursite.hostmatrix.org";
First we connect to the DB, edit in your settings. Then they dynamic URL, now we can use ?do=, it will be used
a lot :p. The password part is the password to access the admin area, it will be compared with the one entered by the user. Next is the subject and from email, I suggest putting newsletter in the subject so people will know what it is, and to put newsletter as to who its from. Thats it, for thats section at least
III: Subscribing
For subscribing we will use ?do=, or the user just doesn't type anything, and ?do=subscribe. The first being the form to subscribe, and the second to actually do the subscribing so to speak.
PHP Code:
if($do == "")
{
echo('<center><strong>Join the mailing list!</strong>
<form action="?do=subscribe" method="post">
E-mail address: <input type="text" name="email"><br /><br />
<input type="submit" name="submit" value="subscribe">
</form>
<a href="?do=unsub">Unsubscribe</a>
</center>');
}
//do all actions when the subscribe form is submitted
else if($do == "subscribe")
{
//get there email and ip
$email = $_POST['email'];
$ip = $_SERVER['REMOTE_ADDR'];
//check to see if the email field is filled in
if(empty($email))
{
die("Sorry, you must fill in your email address.");
}
//make sure there is an @ in the email address
else if(strpos($email, '@') != 1)
{
die("Sorry, not a valid email address.");
}
//everything checks out, add the email to the DB
else
{
//insert into the table list with the values of email and ip
mysql_query("INSERT INTO list (email, ip) VALUES ('$email', '$ip')") or die("Sorry, there was an error adding your email.");
//everything
echo("Thanks for signing up for the mailing list!");
}
}
The first part is basic, it just check to see if do is anything, and if it isn't it will show the form to subscribe. (It also shows a link to unsubscribe). Now the second part is a wee bit more complex. It gets the email from the form, and then makes sure its filled in, and has a '@', a basic validator. If everything checked out, the script will query the DB, inserting the values of email and ip. Not
complicated.
IV: Admin
This is the funnest (:p) part! It has two sections, again, the form, and the actual sending.
PHP Code:
else if($do == "admin")
{
echo('<form action="?do=isadmin" method="post">
The password: <input type="password" name="password"><br /><br />
The content<br />
<textarea rows="5" cols="50" name="content"></textarea><br /><br />
<input type="submit" name="submit" value="enter">
</form>');
}
else if($do == "isadmin")
{
//get the password and content from the form
$password = $_POST['password'];
$password = md5($password);
$content = $_POST['content'];
//make sure the right password was entered
if($password == $encrypted)
{
//set the query which gets the email addresses
$res = mysql_query("SELECT * FROM `list`") or die(mysql_error());
$message = "$content \n\n\n------\nUnsubscribe from the mailing list at: http://www.yoursite.com?do=unsub";
//fetch an array of the data
while($get = mysql_fetch_array($res))
{
//get the email from the DB
$email = $get['email'];
//send the email
mail($email, $subject, $message, "From: $youremail");
//echo who it was sent to (You'll see this line a lot)
echo("Mail sent to $email<br />");
}
//count how many people it is being sent to
$total = mysql_num_rows($res);
//just a little break, then it says how many people it was sent to
echo("<strong>-------------<br />");
echo("Mail sent to $total people sucessfullly.</strong>");
}
//the password was wrong, so send an error
else
{
die("Sorry, wrong password. Bye now!");
}
}
The first bit is again, just a simple form, it asks for the password and the email you wish to send. (It's accessed by ?do=admin)
When processing the form it goes to ?do=isadmin, and then checks the password. If it's ok, it will then query the DB to get all the emails in the table. Next we set the message variable, thats whats actually sent, at the bottom we add a link to unsubscribe, just in case. (Change the URL to yours!)
For a new line, type \n.
Next, we use a while() to cycle through every e-mail in the database, and send a e-mail to them. The script is set to echo who it is sent to, so if there is 3 e-mails in the db, you will see 3 lines, each one containing an E-mail. At the bottom we use mysql_num_rows(), which counts how many rows are in the table to return the total number of people. Somewhat easy?
V: Unsubscribing
The final part, if the user hates you, he/she will want to unsubscribe, we have to consider them. (I know, how sad!)
PHP Code:
//unsubscribe form
else if($do == "unsub")
{
echo('<center><strong>Unsubscribe</strong><form method="post" action="?do=dounsub">
Your e-mail address: <input type="text" name="unsubemail"><br /><br />
<input type="submit" value="Unsubscribe">
</form></center>');
}
//get the email, then delete
else if($do == "dounsub")
{
$unsubemail = $_POST['unsubemail'];
mysql_query("DELETE FROM list WHERE email = '$unsubemail'");
echo("You've been sucessfully removed from our database, goodbye! :)");
}
?>
Wow, this is getting old, the first part shows the form, simple enough. :p
The next part gets the user's e-mail. After that the script runs a query deleting the the email addres equal to the one entered in the form. Woo, were done! Throw confetti! Enjoy it.