Newsletter

quadrant

New Member
Messages
12
Reaction score
0
Points
0
Hello to all!
I'm new here and I'd like to know how can I have in my site a form for newsletter.
I've tried in some tests but it didn't work!!!!
Could you help me????

Quadrant
 

tittat

Active Member
Messages
2,478
Reaction score
1
Points
38
There are two ways to send newsleeters
1)server based scripts
2)Mass mailing software which is installed on your local computer.
Which one do you prefer?
 

Farenvin

New Member
Messages
10
Reaction score
0
Points
0
Here is a very simple mailing and subscribing mechanism in two pieces.
Code:
<?php
//set up a couple of functions
function doDB() {
   global $conn;
   //connect to server and select database; you may need it
   $conn = mysql_connect("localhost", "joeuser", "somepass") or die(mysql_error());
   mysql_select_db("testDB",$conn)  or die(mysql_error());
}

function emailChecker($email) {
   global $conn, $check_result;
   //check that email is not already in list
   $check = "select id from subscribers where email = '$email'";
   $check_result = mysql_query($check,$conn) or die(mysql_error());
}

//determine if they need to see the form or not
if ($_POST[op] != "ds") {
   //they do, so create form block
   $display_block = "
   <form method=POST action=\"$_SERVER[PHP_SELF]\">

   <p><strong>Your E-Mail Address:</strong><br>
   <input type=text name=\"email\" size=40 maxlength=150>

   <p><strong>Action:</strong><br>
   <input type=radio name=\"action\" value=\"sub\" checked> subscribe
   <input type=radio name=\"action\" value=\"unsub\"> unsubscribe

   <input type=\"hidden\" name=\"op\" value=\"ds\">

   <p><input type=submit name=\"submit\" value=\"Submit Form\"></p>
   </form>";

} else if (($_POST[op] == "ds") && ($_POST[action] == "sub")) {
    //trying to subscribe; validate email address
   if ($_POST[email] == "") {
       header("Location: manage.php");
       exit;
   }

   //connect to database
   doDB();

   //check that email is in list
   emailChecker($_POST[email]);

   //get number of results and do action
   if (mysql_num_rows($check_result) < 1) {
        //add record
        $sql = "insert into subscribers values('', '$_POST[email]')";
        $result = mysql_query($sql,$conn) or die(mysql_error());
        $display_block = "<P>Thanks for signing up!</P>";
   } else {
       //print failure message
       $display_block = "<P>You're already subscribed!</P>";
   }
} else if (($_POST[op] == "ds") && ($_POST[action] == "unsub")) {
   //trying to unsubscribe; validate email address
   if ($_POST[email] == "") {
       header("Location: manage.php");
       exit;
   }

   //connect to database
   doDB();

   //check that email is in list
   emailChecker($_POST[email]);

   //get number of results and do action
   if (mysql_num_rows($check_result) < 1) {
       //print failure message
       $display_block = "<P>Couldn't find your address!</P>
       <P>No action was taken.</P>";
   } else {
       //unsubscribe the address
       $id = mysql_result($check_result, 0, "id");
       $sql = "delete from subscribers where id = '$id'";
       $result = mysql_query($sql,$conn) or die(mysql_error());
       $display_block = "<P>You're unsubscribed!</p>";
   }
}
?>
<HTML>
<HEAD>
<TITLE>Subscribe/Unsubscribe</TITLE>
</HEAD>
<BODY>
<h1>Subscribe/Unsubscribe</h1>
<?php echo "$display_block"; ?>
</BODY>
</HTML>
That is manage.php
This is sendmymail.php
Code:
<?php
if ($_POST[op] != "send") {
   //haven't seen the form, so show it
   print "
   <HTML>
   <HEAD>
   <TITLE>Send a Newsletter</TITLE>
   </HEAD>
   <BODY>
   <h1>Send a Newsletter</h1>
   <form method=\"post\" action=\"$_SERVER[PHP_SELF]\">
   <P><strong>Subject:</strong><br>
   <input type=\"text\" name=\"subject\" size=30></p>
   <P><strong>Mail Body:</strong><br>
   <textarea name=\"message\" cols=50 rows=10 wrap=virtual></textarea>
   <input type=\"hidden\" name=\"op\" value=\"send\">
   <p><input type=\"submit\" name=\"submit\" value=\"Send It\"></p>
   </FORM>
   </BODY>
   </HTML>";

} else if ($_POST[op] == "send") {
    //want to send form, so check for required fields
    if (($_POST[subject] =="") || ($_POST[message] == "")) {
       header("Location: sendmymail.php");
       exit;
   }

   //connect to database
   $conn = mysql_connect("localhost", "joeuser", "somepass") or die(mysql_error());
   mysql_select_db("testDB",$conn)  or die(mysql_error());

   //get emails from subscribers list
   $sql = "select email from subscribers";
   $result = mysql_query($sql,$conn) or die(mysql_error());

   //create a From: mailheader
   $headers = "From: Your Mailing List <you@yourdomain.com>\n";

   //loop through results and send mail
   while ($row = mysql_fetch_array($result)) {
       set_time_limit(0);
       $email = $row['email'];
       mail("$email", stripslashes($_POST[subject]), stripslashes($_POST[message]), $headers);
       print "newsletter sent to: $email<br>";
   }
}
?>

The first one contains the code you would give to those you are going to be giving the newsletter to; the second lets you send the newsletter. I would suggest you password protect the second one to prevent spam.

The table you need is "subscribers". The 2 fields are "id" and "email". "id" is a Primary NULL INT that auto-increments. "email" is a VARCHAR.
Change "localhost", "joeuser", and "somepass" to whatever your server settings are.

Have fun!

(Source code from Julie C. Melonie's excellent book, "Teach Yourself PHP, MySQL, and Apache.")
 
Top