Let PHP manage small mail lists

Source: Internet
Author: User
Tags unsub

Recently, a reader asked me about the subscription/unsubscription processing mechanism in the ThickBook news list and how it works with the actual sending information to all recipients in the mail list. A good question is raised. There is a very easy method, but the first thing I want to talk about is that what I do is not opportunistic, because I am very cool-there is, I use this method only because I am lazy. Note: There are a lot of mailing list software available on the market, which can be installed on the system, and there are also some other, more is a combined script set, you can also do the same thing. However, as I said, I am a lazy and don't want to download or install anything, so I have generated several fairly simple pages to execute what I want. I hope someone can learn something from it.

The first step is to subscribe to/unsubscribe to the script, which is to add or delete records from the database table. Call it "manage. phtml" or something similar. In this case, a database is required in the background to create a subscription table. According to the half principle in politics, I will use MySQL as the database for this example. You can use any frequently used database, but replace the correct database-related functions according to the PHP Manual.

In my subscription table, I use two fields: email address (email_addr) and date (date_added ). You can add fields as needed or delete the date_added field. In this example, I just show you what I have done and you can modify it as appropriate. In my subscription table, the email_addr field is a non-repeated field, which means you cannot add another identical email address. This avoids repeated subscriptions. When users want to unsubscribe, it also makes the method of deleting records simple and reliable.

So, let's create a subscription/unsubscription form (manager. phtml or what name you think ). I use the same file to process subscription and unsubscription, and also include the actions of the table itself, so it may be a bit complicated. I will explain it from start to end, and then combine all the fragments into a form.

At the beginning of the script, it is to open the database and the preparation timestamp. Dealing with these inconspicuous things at the beginning can always relieve me a little pressure.

--------------------------------------------------------------------------------
// Connect to the database
$ Db = mysql_connect ("servername", "username", "password") or die ("cannot be connected. ");

Mysql_select_db ("yourDB", $ db) or die ("You cannot select a database. ");

// Obtain the timestamp
$ Add_date = date ("Y-m-d ");--------------------------------------------------------------------------------
We want the value of $ op to be "ds ". It is not a complex program abbreviation-I created it, indicating "do something (do something )". Therefore, the first thing in the script is to check whether the value of $ op is equal to "ds ". This value is sent only after the form is submitted. Therefore, if the value of $ op is not "ds", it indicates that the user has not read the form, so the form should be displayed:

--------------------------------------------------------------------------------
If ($ op! = "Ds "){

// Subscribe/unsubscribe

$ 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 will pay attention to putting the text in the $ text_block variable. By placing the text in a variable, all I need to do is output the value of $ text_block in the main HTML template. This is a matter of personal habits. You can output the text according to your favorite time and method.

The action of this form is $ PHP_SELF. You can imagine that it will be reloaded when the submit button is pressed. Then, you can see that this form has three fields: a hidden field, which is used to assign a value to $ op as "ds"; a text field called "email ", here, the user will fill in his or her email address. There is also a single-choice button set called "action". Based on this, the user can decide which action to perform (subscription or unsubscription ).

After the form is submitted, $ op will be equal to "ds" and the value of $ action will include "sub" or "unsub ". Then, let's continue to look at the above if... statement. Once submitted, it will be skipped (because $ op = "ds "). If $ op is set to "ds" and $ action is set to "sub", the following else if... sentence is executed. This code checks whether e-mail already exists in the subscription table. If it does not exist, insert it into the table and print the response. Otherwise, ignore it.

--------------------------------------------------------------------------------
Else if ($ op = "ds") & ($ action = "sub ")){

// If you have not submitted the email, submit the email; otherwise, the message is returned.

$ Check = "select email_addr from subscribers
Where email_addr = "$ email "";

$ Check_result = mysql_query ($ check)
Or die ("The local email address cannot be checked. ");

$ Check_num = mysql_num_rows ($ check_result );


If ($ check_num = 0 ){

// If $ check_num is 0, no matching record is 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>

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.