Let PHP manage a small mailing list

Source: Internet
Author: User
Tags date exit final header mail modify connect unsub

Recently a reader asked me about the mechanism for processing subscriptions/unsubscribe in the Thickbook News list, and how to match the actual sending information to all the addressee in the mailing list. The problem is good, there is a fairly easy way, but the first thing I have to say is that I do not have any opportunistic places, because I am very cool----is, I use this method is only because I am lazy. Note that there are a lot of mailing list software available on the market, you can put it on the system, and there are other, more combinations of script sets that you can use to do the same thing. But, as I said, I am a lazy person and do not want to download and install anything, so I have generated several fairly simple pages to perform the work I have requested. Hopefully, someone can learn something from it.

The first is the Subscribe/unsubscribe script, which is the job of adding or deleting records from a database table. Call it "manage.phtml" or something of the sort. In this case, you need the background to be some kind of database, where you can create a subscription table. Based on half the principles of politics, I will use MySQL as a database for this example. You can use any database you have used, just to replace the correct database-related functions with the PHP manual.

In my subscription table, I used two fields: Mail address (EMAIL_ADDR) and add date (date_added). You can add fields as needed or delete the date_added fields. In this case, I'm just showing you what I did and you can modify it appropriately. In my subscription table, the Email_addr field is a repeating field, meaning that you cannot add another e-mail address that is exactly the same as the one. This avoids duplicate subscriptions and makes it simpler and more reliable to delete records when users want 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 subscriptions and unsubscribe, and also to include the actions of the table itself, so it may be a bit complicated. I'll cover all the pieces and put them all together 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 relieve me of a little 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 a time stamp
$add _date = Date ("y-m-d"); --------------------------------------------------------------------------------
We want the value of $op to be "DS". It is not a complex acronym----I created, which means "do something (doing something)". So the first thing about the script is to see if the value of the $op is equal to "DS". This value is sent only when the form is submitted. So if the value of $op is not "DS", then the user has not seen the form, so the form should be displayed:

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

Subscribe/Unsubscribe Required

$text _block = "

<form name=\ "form\" Method=post action=\ "$PHP _self\" >
<input Type=hidden Name=op value=ds>

<p><strong>your e-Mail address:</strong><br>
<input type=text name=\ "email\" size=25></p>

<p><strong>action:</strong><br>
<input type=radio name=\ "action\" value=\ "sub\" checked> Sub
<input type=radio name=\ "action\" value=\ "unsub\" > Unsub</p>

<p><input type=submit name=\ "submit\" value=\ "Do it\" ></p>
</form>

";

}--------------------------------------------------------------------------------
You'll notice that I put the text in the $text_block variable. By putting the text in a variable, all I have to do next is to output the $text_block value in the main HTML template. This is a matter of personal habit, you can output text according to the time and manner you like.

The action of this form is $php_self, and you can imagine that it means that when the submit button is pressed, it will be reload. Then, you can see that the form has three fields: a hidden field used to assign 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 decide which action to perform (subscribe or unsubscribe).

After the form is submitted, the $op equals "DS" and the $action value will contain "sub" or "Unsub". So, let's continue to look at the above 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 ... Sentences are executed. This code checks whether the e-mail already exists in the subscription table, and if it does not exist, inserts it into the table and prints out the response, otherwise ignored.

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

Submit them if the message is not yet submitted or return information

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

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

$check _num = mysql_num_rows ($check _result);


if ($check _num = = 0) {

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

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

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

$text _block = "
<P> Thank you for your registration! </p>
";

} else {

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

$text _block = "
<P> you've already subscribed! </p>
";

}

}--------------------------------------------------------------------------------

Next: What you should do when the value of $action is "unsub" (unsubscribe, unsubscribe) instead of "sub". OK, just as simple as the above, then for else if ... Statement extension, add a piece of code to check whether the e-mail exists in the subscription table before being deleted, if it exists, delete it and print the response, otherwise ignore it.

--------------------------------------------------------------------------------
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 $check_num is 0, no matching record is found and the user cannot be unsubscribe

$text _block = "
<P> can't find your e-mail address in the list! </p>
<P> you have not been unsubscribe because the email you entered is not in the database. ";

} else {

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

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

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

$text _block = "
<P> Unsubscribe Success! </p>
";
}

}

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

--------------------------------------------------------------------------------
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 3.2 final//en" >
<HTML>
<HEAD>
<TITLE> Subscribe/Unsubscribe </TITLE>
</HEAD>

<BODY>

<?php echo "$text _block";?>

</BODY>
</HTML>
--------------------------------------------------------------------------------
Here is a complete list of 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 a time stamp
$add _date = Date ("y-m-d");

if ($op!= "ds") {

Subscribe/Unsubscribe Required

$text _block = "

<form name=\ "form\" Method=post action=\ "$PHP _self\" >
<input Type=hidden Name=op value=ds>

<p><strong>your e-Mail address:</strong><br>
<input type=text name=\ "email\" size=25></p>

<p><strong>action:</strong><br>
<input type=radio name=\ "action\" value=\ "sub\" checked> Sub
<input type=radio name=\ "action\" value=\ "unsub\" > Unsub</p>

<p><input type=submit name=\ "submit\" value=\ "Do it\" ></p>
</form>

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

Submit them if the message is not yet submitted or return information

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

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

$check _num = mysql_num_rows ($check _result);


if ($check _num = = 0) {

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

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

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

$text _block = "
<P> Thank you for your registration! </p>
";

} else {

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

$text _block = "
<P> you've already subscribed! </p>
";

}

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 $check_num is 0, no matching record is found and the user cannot be unsubscribe

$text _block = "
<P> can't find your e-mail address in the list! </p>
<P> you have not been unsubscribe because the email you entered is not in the database. ";

} else {

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

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

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

$text _block = "
<P> Unsubscribe Success! </p>
";
}

}
?>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 3.2 final//en" >
<HTML>
<HEAD>
<TITLE> Subscribe/Unsubscribe </TITLE>
</HEAD>

<BODY>

<?php echo "$text _block";?>

</BODY>
</HTML>
--------------------------------------------------------------------------------
Now that you have the appropriate subscription/unsubscribe mechanism, I will show you how to send a newsletter, using only a simple form and a mail script. (the "While" loop is a good friend of yours!) )。 First, it's a form called "send_mail.html." The action of the form should be something like "do_send_mail.phtml", and I use only a text field (subject) for writing the subject and a Text field (newsletter) that writes the contents of the letter. You can use form fields as needed, as long as you modify the forms and scripts appropriately.

--------------------------------------------------------------------------------
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 3.2 final//en" >
<HTML>
<HEAD>
<TITLE> Send mail </TITLE>
</HEAD>

<BODY>

<form action= "do_send_mail.phtml" method= "POST" >

<P><strong> give a theme:</strong><br>
<input type= "text" name= "subject" size=30></p>

<P><strong> Email Content:</strong><br>
<textarea name= "Newsletter" cols=40 rows=30 wrap=virtual></textarea>

<p><input type= "Submit" name= "Submit" value= "Send Newsletter" ></p>

</form>

</BODY>
</HTML>
--------------------------------------------------------------------------------
The final note is about the action of the form, which is called "do_send_mail.phtml". The script first looks for values for $subject and $newletter, and redirects to the form if one of their values is empty:

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

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

}--------------------------------------------------------------------------------
Next, connect to the database and remove the 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 mail address.") ");
--------------------------------------------------------------------------------
Before entering the loop to send message information, create additional headers. Here, I only use the "From:" Line:

$headers = "From: \" Your Mailing list\ "<you@yourdomain.com>\n";


Now go into the loop of sending the message. First, use the Mysql_fetch_array function (or a function similar to your database) to put each record 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 iterates through the result set and sends an e-mail message to each mailbox in the list through 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. At the end of the script, add a line of output statements so you know that the execution is complete. This is the whole deal! 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 mail address.") ");

$headers = "From: \" Your Mailing list\ "<you@yourdomain.com>\n";


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

$email _addr = $row [0];

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

}

echo "Send mail complete!";
}
?>
--------------------------------------------------------------------------------



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.