Manage a small mailing list _php

Source: Internet
Author: User
Keywords Small management unsubscribe subscription No die
Tags unsub
The first is the Subscribe/unsubscribe script, which does the job of adding or deleting records from the database tables. Call it "manage.phtml" or
Pretty much something. This way, you need a database in the background, you can create a subscription table. According to half the principles of politics,
So I'm going to use MySQL as the database for this example. You can use any database you used, just replace the correct one with the PHP manual.
Database-related functions.

In my subscription table, I used two fields: Mail address (EMAIL_ADDR) and add date (date_added). Depending on your needs, you can
To add a field, or delete the date_added field. In this case, I'm just showing you what I did, and you can do it properly.
Modify. In my subscription table, the Email_addr field is a non-repeating field, meaning you cannot add another e-mail that is exactly the same as
Address. This avoids recurring subscriptions and makes it easier and more reliable to delete records when the user wants to unsubscribe.

So, let's create a subscription/unsubscribe form (manager.phtml or whatever name you think of). I use the same file to process a subscription
Reading and unsubscribe, also includes the action of the table itself, so it may be a bit complicated. I'll explain it from beginning to end, then combine all the fragments together,
Placed in a form.

At the beginning of the script, you open the database and prepare the timestamp. Dealing with these inconspicuous things at the beginning can always be a little lighter for me.
Pressure.

--------------------------------------------------------------------------------
Connecting to a database
$db = mysql_connect ("servername", "username", "password") or Die ("Cannot connect. ");

mysql_select_db ("Yourdb", $db) or Die ("cannot select a database. ");

Get time stamp
$add _date = Date ("y-m-d"); --------------------------------------------------------------------------------
We want the value of $op to be "DS". It is not a complex program abbreviation----I created, saying "do something (doing something)". So
The first thing about the script is to see if the value of $op is equal to "DS". This value is not sent until the form is submitted. So if
$op value is not "DS", then the user has not seen the form, so the form should be displayed:

--------------------------------------------------------------------------------
if ($op! = "ds") {

Need to subscribe/unsubscribe

$text _block = "



";

}--------------------------------------------------------------------------------
You will notice that I put the text in the $text_block variable. By putting the text in a variable, the next thing I do is
The value of $text_block is output later in the main HTML template. This is a personal habit problem, you can output text according to the time and way you like.

The action of this form is $php_self, you can imagine, it means that when the submit button is pressed, it will be re-loaded
Into. You can then see that the form has three fields: a hidden field that assigns the $op to "DS"; a text field called
"Email", where the user will fill in his or her email address, and a radio button set called "Action", according to which the user can
To decide which action to perform (subscribe or unsubscribe).

After the form is submitted, the $OP will be equal to "DS", and the value of $action will contain "sub" or "Unsub". So, let's keep looking up.
The If ... Statement, once committed, it will be skipped (because $op== "DS"). If the value of $op is "DS" and the value of $action is "sub" (subscription),
The following else if ... Sentence is executed. This code checks whether e-mail already exists in the subscription table and inserts it into the table if it does not exist
and print out the response, otherwise ignored.

--------------------------------------------------------------------------------
else if (($op = = "ds") && ($action = = "Sub") {

Check that the messages are not committed, submit them, or return information

$check = "Select Email_addr from Subscribers
where email_addr = \ "$email \" ";

$check _result = mysql_query ($check)
Or Die ("Unable to perform an e-mail address check.") ");

$check _num = mysql_num_rows ($check _result);


if ($check _num = = 0) {

If $check_num is 0, no matching records are found and the user should be committed

$sql = "INSERT INTO Subscribers
VALUES (\ "$email \", \ "$add _date\") ";

@mysql_query ($sql) or Die ("couldn ' t insert email.");

$text _block = "

Thank you for registering!


";

} else {

If $check_num is not 0, then the user has submitted it, and you should let them know

$text _block = "

You've already subscribed!


";

}

}--------------------------------------------------------------------------------
Next: What to do when the value of $action is "unsub" (unsubscribe, unsubscribe) instead of "sub". All right, just like the top.
As simple as that, then to else if ... Statement extension, add a piece of code to check whether e-mail exists in the subscription table before being deleted
If it exists, it is deleted and the response is printed, otherwise it is ignored.

--------------------------------------------------------------------------------
else if (($op = = "ds") && ($action = = "Unsub")) {

Check that you have subscribed, and then unsubscribe them, otherwise return information

$check = "Select Email_addr from Subscribers
where email_addr = \ "$email \" ";

$check _result = mysql_query ($check)
Or Die ("cannot perform a check on an e-mail address.") ");

$check _num = mysql_num_rows ($check _result);

if ($check _num = = 0) {

If the $check_num is 0, no matching record is found and the user cannot be unsubscribe

$text _block = "

You cannot find your e-mail address in the list!


You have not been unsubscribe because the e-mail you have entered is not in the database. ";

} else {

If $check_num is not 0, then the user is in the list, so can be unsubscribe

$sql = "Delete from subscribers
where email_addr = \ "$email \" ";

@mysql_query ($sql) or Die ("Cannot delete email. ");

$text _block = "

Unsubscribe Success!


";
}

}

?>--------------------------------------------------------------------------------
Now that all the hard work has been done, only the output of the $text_block variable in a piece of HTML is left:

--------------------------------------------------------------------------------



<title>Subscribe/Unsubscribe</title>




Subscribe/Unsubscribe







--------------------------------------------------------------------------------
The following is a complete list of the programs:

--------------------------------------------------------------------------------

Connecting to a database
$db = mysql_connect ("servername", "username", "password")
Or Die ("Cannot connect. ");

mysql_select_db ("Yourdb", $db) or Die ("cannot select a database. ");

Get time stamp
$add _date = Date ("y-m-d");

if ($op! = "ds") {

Need to subscribe/unsubscribe

$text _block = "



";
} else if (($op = = "ds") && ($action = = "Sub") {

Check that the messages are not committed, submit them, or return information

$check = "Select Email_addr from Subscribers
where email_addr = \ "$email \" ";

$check _result = mysql_query ($check)
Or Die ("Unable to perform an e-mail address check.") ");

$check _num = mysql_num_rows ($check _result);


if ($check _num = = 0) {

If $check_num is 0, no matching records are found and the user should be committed

$sql = "INSERT INTO Subscribers
VALUES (\ "$email \", \ "$add _date\") ";

@mysql_query ($sql) or Die ("couldn ' t insert email.");

$text _block = "

Thank you for registering!


";

} else {

If $check_num is not 0, then the user has submitted it, and you should let them know

$text _block = "

You've already subscribed!


";

}

} else if (($op = = "ds") && ($action = = "Unsub")) {

Check that you have subscribed, and then unsubscribe them, otherwise return information

$check = "Select Email_addr from Subscribers
where email_addr = \ "$email \" ";

$check _result = mysql_query ($check)
Or Die ("cannot perform a check on an e-mail address.") ");

$check _num = mysql_num_rows ($check _result);

if ($check _num = = 0) {

If the $check_num is 0, no matching record is found and the user cannot be unsubscribe

$text _block = "

You cannot find your e-mail address in the list!


You have not been unsubscribe because the e-mail you have entered is not in the database. ";

} else {

If $check_num is not 0, then the user is in the list, so can be unsubscribe

$sql = "Delete from subscribers
where email_addr = \ "$email \" ";

@mysql_query ($sql) or Die ("Cannot delete email. ");

$text _block = "

Unsubscribe Success!


";
}

}
?>




<title>Subscribe/Unsubscribe</title>




Subscribe/Unsubscribe







--------------------------------------------------------------------------------
Now that you have the right subscription/unsubscribe mechanism, I'll show you how to send a news letter, using only a simple form
and a mail script. (the "while" loop is your good friend!) )。 First, it's a form called "send_mail.html". Action of the form
It should be something like "do_send_mail.phtml", and I only used a text field (subject) to write the subject and a
A Text Field field (newsletter) that writes the contents of a letter. You can use form fields as needed, as long as you modify the forms and scripts appropriately.

--------------------------------------------------------------------------------



<title>Send mail</title>




Send a Newsletter







--------------------------------------------------------------------------------
The last point is about the action of the form, which is called "do_send_mail.phtml". The script first looks for $subject and
$newletter values and redirect to the form if they have a null value:

--------------------------------------------------------------------------------
if ($subject = = "") | | ($newsletter = = "")) {

Header ("location:http://www.yourdomain.com/send_mail.phtml");
Exit

}--------------------------------------------------------------------------------
Next, connect to the database and remove the e-mail address from the subscription table:

--------------------------------------------------------------------------------
Connecting to a database
$db = mysql_connect ("servername", "username", "password")
Or Die ("Cannot connect. ");

mysql_select_db ("Yourdb", $db) or Die ("cannot select a database. ");

$sql = "Select email_addr from Subscribers";

$res = mysql_query ($sql) or Die ("Cannot get e-mail address.") ");
--------------------------------------------------------------------------------
Create additional headers before entering the loop to send message information. Here, I only use the "From:" Line:

$headers = "From: \" Your Mailing list\ " \ n ";


Now enter the loop that sends the message. First, use the Mysql_fetch_array function (or a function similar to your database) to set each
The record is placed in an array. If you retrieve more than one field that might make more sense, I use it because it's fast. The following statement pairs the result
Set to traverse and send e-mail to each mailbox in the list via the Mail () function:

--------------------------------------------------------------------------------
while ($row = Mysql_fetch_array ($res)) {

$email _addr = $row [0];

Mail ("$email _addr", "$subject", $newsletter, $headers);

}
--------------------------------------------------------------------------------
The values for $subject and $newletter are entered in the previous form. Add a line of output statements at the end of the script so that you know
It's done. This is all handled! The complete "do_send_mail.phtml" script looks like this:

--------------------------------------------------------------------------------

if ($subject = = "") | | ($newsletter = = "")) {

Header ("location:http://www.yourdomain.com/send_mail.phtml");
Exit

} else {

Connecting to a database
$db = mysql_connect ("servername", "username", "password")
Or Die ("Cannot connect. ");

mysql_select_db ("Yourdb", $db) or Die ("cannot select a database. ");

$sql = "Select email_addr from Subscribers";

$res = mysql_query ($sql) or Die ("Cannot get e-mail address.") ");

$headers = "From: \" Your Mailing list\ " \ n ";


while ($row = Mysql_fetch_array ($res)) {

$email _addr = $row [0];

Mail ("$email _addr", "$subject", $newsletter, $headers);

}

echo "Mail sent!";
}
?>
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.