SQL PHP HTML ASP JavaScript articles and free scripts to download
 

PHP Newsletter Script

By using PHP,newsletter script can be developed. This is an easy newsletter script and this article will explain step by step on creating a newsletter script with all the functionality of subscribing and unsubscribing by the members. The php script will take care of all subscribe and unsubscribe requirements.
All newsletters will have one unique link in side the body of the email to allow members to unsubscribe from the list. One famous site on Funny jokes with Daily Jokes uses this script to manage its subscribers. We need one simple input email box in the site by which visitors can submit their email address.

<?

echo " <table border='0' width='100%' cellspacing='0' cellpadding='0' bgcolor='$mail_bgcolor'>
<tr><td><font face='Verdana' size='2'>Submit your email address and receive daily one joke in your mail box</font></td></tr>
<tr><td align=center>
<form method=post action=newsletter.php><input type=text name=em size=15><br><input type=submit value='Signup'></form>
</td></tr></table>";

?> 

This will display one input box for the visitors to add their email address to subscribe to newsletter.

Next step is to add the email address to our database. We will receive the email address in the page newsletter.php and before inserting to table we will like to validate the email address.  Here is the article explaining php form validation.  We will check the email address and if we found the email address format is correct then we will add to our database.

$status = "OK";  // setting the flag to check the status
$msg="";          // setting the variable for message
if (!stristr($em,"@") OR !stristr($em,".")) {
$msg="Your email address is not correct<BR>";
$status= "NOTOK";}
 echo "<br><br>";
if($status=="OK"){// Now the email is valid and we can add to our database
$query=("INSERT INTO nl_subscribe (em,status) VALUES ('$em','subscribe')");
$result=mysql_query($query);
echo "<center>THANK YOU <br>Thanks for subscribing to our newsletter and any time you can unsubscribe by clicking a link in your newsletter</center>";
          }
else {echo "<center>$msg </center>";}  // this will display the error message if email address is not valid one.
?>

This will complete the form validation process of the email address and  insert data to the table. The table structure is here to use. 
CREATE TABLE nl_subscribe (  em varchar(75) NOT NULL default '',  status varchar(10) NOT NULL defult '') TYPE=MyISAM;
Now let us create one interface for the site admin to use for sending the newsletters to the subscribers. We will store the code in a file inside the admin director with all login managements. Here we will discuss only the posting message part with unsubscribe link. Here is the table to display the text box to enter the message

<? echo "<form method='post' action=postmailall.php>"; echo "<table border='0' width='100%' bgcolor='#C0C0C0' cellspacing='0' cellpadding='0'> <tr>"; echo "<td width='54%' bgcolor='#99CCFF'>&nbsp;<font face='Verdana'><b>Post Message </b></font></td> <td width='46%' bgcolor='#99CCFF'>&nbsp;</td> </tr><tr> <td colspan=2 align=center>&nbsp;<font face='Verdana'>Subject of message</font></td></tr> <tr><td colspan=2 align=center>&nbsp;<input type=text name=sub></td> </tr><td COLSPAN='2' align=center>&nbsp;<input type='submit' value='Send'> <input type=reset value=Reset></td></tr> </table> <input type=hidden name=sendmail value=yes></form>"; ?>

 This is to display the text area for the admin to enter the subject and body details of the mail. Now let us see how to send the mails. We will format the mail first based on the data we receive from the form.All the pages should have one unique link at the bottom of the pages to unsubscribe from the list.   So let us prepare the link to be attached at the bottom of each newsletter.
$pt2="http://$SERVER_NAME$SCRIPT_NAME";
$pt2= substr($pt2,0,strlen($pt2)-21);  This will prepare url up to the root of the page to add the unsubscribe page name here .  Please note that the number 21 is the file name length including the extensions.
Now let us collect the email address from the database
$query="select em from nl_subscribe where status='subscribe'";
$result=mysql_query($query);
while($rowdata=mysql_fetch_array($result))
{
$msg2=$msg."<BR>To unsubscribe <a     href=$pt2"."unsub.php?email=$rowdata[em]>Click HERE</a>";
mail($rowdata[em],$sub,$msg2,"$mail_type");
}//end of while loop

This code will collect all email address from the table and will post to the subscriber with unique link at the bottom for unsubscribing. This is upto the sending the mails part. Now let us work on the unsubscribing part of the script. As we have one unique link at each of the newsletter and on clicking that link the script will take us to the page where the email address can be removed or unsubscribed. We have to do two things here , first is to receive the email address from the query string and check the database. If the email address exists then unsubscribe or if it does not exists then display a error message.

$result=mysql_query("select * nl_subscribe where em='$email' ");
$rows=mysql_num_rows($result);
if ($rows > 0){ // email exists so let us change the status to vacation or unsubscribe
$result=mysql_query("update nl_subscribe set status='vacation' where em='$email' ");
       $msg="Your email address $email removed from our list <BR><BR>You can signup any time by visiting signup page<br>";
}
else {  // email address is not there in our database so display the error message.
     $msg="As per our list Your email address is not there with us. <BR>You can contact our site admin <br>";
}
echo "<center>$msg</center>";

This is a simple email newsletter management script and many more functionality can be added to this to get the best result.


Join Our Email List
Email:  
For Email Newsletters you can trust



HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer