UK Webmaster Talk - Online Marketing - SEO


 

PHP Mailing list

This is a discussion on PHP Mailing list within the Programming Articles forums, part of the Webmaster Articles/Tutorials category; This tutorial covers how to create a mailing list using PHP. It's actually rather long, as it covers subscribing, admin, ...


Go Back   UK Webmaster Talk - Online Marketing - SEO > Webmaster Articles > Webmaster Articles/Tutorials > Programming Articles

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Notices

Reply

 

LinkBack Thread Tools Display Modes
Old 29-04-2005, 17:19   #1 (permalink)
*poke* *poke*
 
Join Date: Apr 2005
Posts: 134
iTrader: 0 / 0%
mck9235 is on a distinguished road
Default PHP Mailing list

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` (
`
emailVARCHAR255 NOT NULL ,
`
ipVARCHAR255 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.
__________________
| - The Fast Track - |
Thanks to WMT for the new domain!
mck9235 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
list, mailing, php

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
UK Directory list coldclimber Search Engine Optimisation (SEO) 5 19-05-2008 19:04
Mailing List sign-up help... Fuzquia Website Design Forum 1 21-05-2006 08:44
List ALL your sites PLEASE! robertall Ads, Offers & Services 3 28-11-2005 18:24
Using cpanel part 1: using webmail and e-mailing fetures. robertall Programming Articles 0 19-06-2005 17:32
Free mailing script. robertall Website Development Forum 2 05-04-2005 09:15


All times are GMT +1. The time now is 19:11.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0
UK Webmaster Forum © WebmasterTalk.co.uk | Design by Forbairt

Ad Management by RedTyger

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41