Create Newsletter subscription - Printable Version +- Support Forums (https://www.supportforums.net) +-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87) +--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18) +---- Forum: PHP The Hypertext Preprocessor (https://www.supportforums.net/forumdisplay.php?fid=21) +---- Thread: Create Newsletter subscription (/showthread.php?tid=486) |
Create Newsletter subscription - Gaijin - 10-07-2009 Welcome and Enjoy! About: Newsletter is a great way to inform your visitors about Updates and Realeses on your Homepage. I will show you how to create a Newsletter subscription for your Users, with two types of storing their E-Mails, 1 = Flat file database, 2 = MySql Database. Requirements:
Files:
Functions:
HTML files: First let us create needed html files. subscribe.htm Code: <html> send.htm Code: <html> Saving E-Mails: Flat file: This script isn't very long and it'S easy to understand. subscribe.php PHP Code: if(!isset($_POST['subscribe']) || $_POST['email'] == "") { In the second if block (elseif) we try to open newsletter.txt with the function "fopen" and if the file can not be opened or created we will output an error and let the script end with the "die" function. PHP Code: $mail = $_POST['email']." With "fwrite" we write the "$mail" into newly opened/created file "$handle" MySql: First you will need a database table, I've named mine "mails". Code: CREATE TABLE `mails` ( subscribe.php PHP Code: $db = array( I will skip this since you can read more here . http://www.supportforums.net/showthread.php?tid=116 Thanks to MyNameIs PHP Code: if(!isset($_POST['subscribe']) || $_POST['email'] == "") { We set out mysql command to "$sql" variable. Then with mysql_query we try to save the E-Mail to the databse, and if it fails we let the script end and output error message. And lastly we close the connection with mysql_close() Sending E-Mails: Flat file: And again we doe some chacking PHP Code: if(!isset($_POST['send'])) { It is set to "r" wich opens the file for reading only. Now we run a loop and send our mail to every one in the list with the mail() function PHP Code: while(!feof($handle)) { So on english "while not end of file do...". If the mail() function fails to send the mail we output the error and let the script end. And finally we "fclose()" the file. MySql: This part is explained above PHP Code: $db = array( Next we run a query with mysql_query("SELECT * FROM `mails`"), and if the query fails we let the script die. PHP Code: if(!$query = mysql_query("SELECT * FROM `mails`")) { Now the loop for mysql PHP Code: while($row = mysql_fetch_array($query)) { Source Flat file (Click to View) Source MySql (Click to View) This code isn't a secure one, you should read how to secure your code. http://www.supportforums.net/showthread.php?tid=700 Thank you for reading! RE: Create Newsletter subscription - Omniscient - 10-07-2009 Not bad but you should do some email validation checking. You also aren't checking for sql injection code. RE: Create Newsletter subscription - Gaijin - 10-07-2009 Thank you. And yes that was on purpose, cause I'm planning a new tutorial about that. RE: Create Newsletter subscription - El Zorro - 10-08-2009 shouldn't you make a seperate file to store sql database info and block it with htaccess RE: Create Newsletter subscription - Gaijin - 10-08-2009 (10-08-2009, 03:29 PM)El Zorro Wrote: shouldn't you make a seperate file to store sql database info and block it with htaccess Yes that would be better for you, this is just a simple code for learning not for real use. RE: Create Newsletter subscription - brett7 - 10-08-2009 this is pretty cool thx RE: Create Newsletter subscription - MAcar - 10-08-2009 Nice tut Ninjageek! Ima use the send email code to send registration validation for my website. RE: Create Newsletter subscription - Ajsec5000 - 10-08-2009 Awsome RE: Create Newsletter subscription - Gaijin - 10-08-2009 Thank you two. (10-08-2009, 05:26 PM)MAcar Wrote: Nice tut Ninjageek! Ima use the send email code to send registration validation for my website.That's nice. RE: Create Newsletter subscription - Dr.Viper - 10-09-2009 Nice share. I was trying to get this by scraping sources from websites which had this. But now you made it easy for me. |