|Register--Login--Top 20 Posters--Search Topics|
Rank:Settler of Bobland
Group: Head Administrator
PM ID and RPS ID: 1
View Member Photo
|Last replied to on Wed May 16, 2012 01:53:30|
This tutorial will teach you how to make a simple newsletter in PHP/MYSQL with account activation. This will require PHP that is not running in SAFE_MODE or PHP that at least allows the sendmail function(duh, you can't send mail without this enabled).
First you need a table for your subscribers. So for this tutorial, we will call this table email_table. This table needs the following fields:
1. userID - Autogenerated, prmary auto-increment
2. email - varchar 255 length
3. validated - Int with a default value of Zero
4. validkey - varchar 255
So basically the signup process will Consist of three files:
connect.php - The connector file
signup.php - Actual signup file
validate.php - a file for users to validate themselves
The connector file is just a basic connect file:
Your username, password, and database name should go where indicated.
The $adminmail is your administrator mail, this is where you are sending the mail from.
$path is the domain and path this script is installed in. array_map and mysql_real_escape_string parses out possible SQL injections.
The signup file should look something like this:
When you first go to this file, it prints a simple form asking for the e-mail to subscribe, when you hit enter if goes to the
code. First it checks to see if your email is already in the list of emails with $checkdups. Basically it queries the mysql table for that email and
then counts the number of rows, if the number of rows is greater than zero, then it will give you the message that the email is already in the database.
It also counts the number of characters in the email with strlen() and uses substr_count to check for spaces. If the email has spaces or is less than 4 characters, it returns an invalid e-mail error.
If everything goes through error checking then it first sets a random generator seed and then generates a random key validation value. It then
inserts this value into the database along with the email that subscribed. It also uses the mail() function to send a validation email to the email that
just subscribed, to make sure that person actually subscribed.
Now we have to write the validation file, validate.php:
So this file is short and simple it first gets the values from the strings passed in by the URL and uses trim() to trim out any whitespace and strip_tags to
kill any possible HTML injections. These can be done on one line, but for the purposes of this being a tutorial, they are shown line-by-line.
Then after it sanitizes the strings, it updates the sql table and sets the validated value to one where email and validation key are the ones passed in.
Okay the below files go into a password protected admin folder. For this I am assuming you have the ability in your control panel to password protect folders.
If not please visit This link to see how to set up a password protected directory in .htaccess.
So in the admin folder we will have 3 files:
sendletter.php -- the file that sends the newsletter
displaylist - file that displays the list of emails
deluser - delete a email
First lets go over displaylist.php:
This is pretty basic, the query selects all emails on the list and displays them in alphabetical order in tabular format. You can click on the email link
to email that person directly or click on delete to delete that person from your list.
Now lets look at deluser.php , the file that actually deletes the user:
First this file prints a form asking you for a confirmation that you want to delete the user. It takes the ID passed in by the URL and sanitizes it
by trimming the whitespace and stripping any HTML tags from it. Then it hides the ID in a hidden field. When you hit submit, it sends the ID
and the query deletes the user with that ID.
Now lets look at the file that actually sends the newsletter, sendletter.php:
Ok so that sends the newsletter. First it prints out the form with a subject and a message field.
After you hit submit it first checks if those fields are greater than 1 character in length, if they are not, it returns an error message.
If they are, then it takes the subject and the message and does some formatting using stripslashes() which, well strips the slashes from the message(some servers will put slashes in to prevent injections). Then it replaces the "rn" character with <br> or an HTML line break. Then it makes the headers, which specify where this message is from, the text/html content type makes sure that it is sending HTML email Mime version 1.0 .
Last of all it queries the list and loops through all the email address and sends the message to them all.
Thats your basic email list!