|
||||
| Register--Login--Top 20 Posters--Search Topics |
Forum Main>>Tutorials>>Basic HTML newsletter with PHP/MYSQL | ||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 PM [Chipmunk] View Member Photo | Last replied to on Thu Oct 08, 2009 10:57:02 Edit Post|Quote 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! ----------------------------- Chipmunk, Supreme Administrator | |||||||
| davidk20 Rank:acorn Group: members Posts: 1 IP Logged PM ID and RPS ID: 14087 [PM davidk20] RPS score: 0 RPS challenge | Posted at Mon Nov 12, 2007 08:38:25 Edit post|Quote Need help, When i try making the database i keep getting error messages, i dont supose any 1 could give me the SQL for it so i can just run the script? cheers, Dave | |||||||
| frodo6695 Rank:acorn Group: members Posts: 3 IP Logged PM ID and RPS ID: 13147 [PM frodo6695] | Posted at Mon Jan 14, 2008 19:30:16 Edit post|Quote delete, sory, posted in wrong topic | |||||||
| Eric777 Rank:acorn Group: members Posts: 1 IP Logged PM ID and RPS ID: 15254 [PM Eric777] RPS score: 0 RPS challenge | Posted at Wed Feb 20, 2008 07:42:55 Edit post|Quote Hi, Chipmunk:I'm new to php and I've been studying for a long time.Your tutorial in this newsletter was so far the easiest to understand for beginners like me.I appreciate it a lot.But can you pls. make a code that subscribers to this email can Un-Subscribe.They can click on a link in their email to unsubscribe or what have you?Thanks a lot!Enjoying learning from you:Eric | |||||||
| gavinmertes Rank:acorn Group: members Posts: 1 IP Logged PM ID and RPS ID: 15584 [PM gavinmertes] RPS score: 0 RPS challenge | Posted at Thu Mar 27, 2008 07:37:17 Edit post|Quote I keep getting the error "Could not check duplicates" don't know what to do with line " $checkdups2=mysql_query($checkdups) or die("Could not check duplicates");" | |||||||
| Yamthief Rank:acorn Group: members Posts: 1 IP Logged PM ID and RPS ID: 16590 [PM Yamthief] RPS score: 0 RPS challenge | Posted at Thu Jul 03, 2008 08:28:30 Edit post|Quote nice script, thanks for your useful code! | |||||||
| choppa Rank:acorn Group: members Posts: 1 IP Logged PM ID and RPS ID: 21003 [PM choppa] RPS score: 0 RPS challenge | Posted at Mon Mar 16, 2009 01:31:16 Edit post|Quote Hi Chipmunk Thanks for this script. It's been really useful, and easy to understand. One thing I did find though - and I don't know if anyone has noticed this - is that when you send a bulk mailing list newsletter, the letter also goes to un-validated recipients in the database. I managed to fix this by adding a where validated='1' statement in this line on the sendletter.php page: $getlist="SELECT * from email_table order by email ASC"; So it now looks like this: $getlist="SELECT * from email_table where validated='1' order by email ASC"; As I'm new to PHP, I'm not sure if that was the correct way to put it, but it does work... ![]() | |||||||
| kmc Rank:acorn Group: members Posts: 1 IP Logged PM ID and RPS ID: 21873 [PM kmc] | Posted at Wed Apr 15, 2009 18:45:38 Edit post|Quote Is there a way to send the userID along with the newletter message. I would like something like this:Newsletter blah blah blah -> then at the bottom is "If you wish to unsubscribe to our newsletter, please click here: www.domainname.com/deluser.php?userID=$userID"Then the user can delete themself. is there a way to get $userID from the database? Update. I can get www.domainname.com/deluser.php?userID=5 , but I can't get it to do an array. | |||||||
| VisualRealm Rank:acorn Group: members Posts: 1 IP Logged PM ID and RPS ID: 23236 [PM VisualRealm] RPS score: 0 RPS challenge | Posted at Sun Jun 28, 2009 11:41:44 Edit post|Quote Did anyone figure out the could not check duplicates? I am very new to PHP and have been trying to teach myself how to do it. I thought I had everything set up correct but cant get past the "could not check duplicates". Dont know exactly where that could be wrong, mysql, path, etc. Any response would be appreciated. Or if someone has all the files for download. | |||||||
| waynsonray Rank:acorn Group: members Posts: 1 IP Logged PM ID and RPS ID: 24688 [PM waynsonray] RPS score: 0 RPS challenge | Posted at Thu Oct 08, 2009 10:57:02 Edit post|Quote PHP primarily acts as a filter,[32] taking input from a file or stream containing text and/or PHP instructions and outputs another stream of data; most commonly the output will be HTML. Since PHP 4, the PHP parser compiles input to produce bytecode for processing by the Zend Engine, giving improved performance over its interpreter predecessor.Originally designed to create dynamic web pages, PHP now focuses mainly on server-side scripting, and it is similar to other server-side scripting languages that provide dynamic content from a web server to a client, such as Microsoft's Active Server Pages, Sun Microsystems' JavaServer Pages, and mod_perl. PHP has also attracted the development of many frameworks that provide building blocks and a design structure to promote rapid application development (RAD). Some of these include CakePHP, Symfony, CodeIgniter, and Zend Framework, offering features similar to other web application frameworks.The LAMP and WAMP architectures have become popular in the web industry as a way of deploying web applications. PHP is commonly used as the P in this bundle alongside Linux, Apache and MySQL, although the P may also refer to Python or Perl.As of April 2007, over 20 million Internet domains were hosted on servers with PHP installed, and mod_php was recorded as the most popular Apache module. Significant websites are written in PHP including the user-facing portion of Facebook, Wikipedia (MediaWiki), Yahoo!, MyYearbook, Digg, Joomla, WordPress, YouTube, Drupal and Tagged. ----------------------------- new york flights - national car rental - travel | |||||||
Page: 1 |