Manage small mail lists

Source: Internet
Author: User
Tags unsub
The first step is to subscribe to the unsubscription 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 data first by using the subscription/unsubscription script, which is used to add or delete records from database tables. 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,
So I will use MySQL as the database for this example. You can use any frequently used database, just replace the correct one according to the PHP Manual
Database-related functions.

In my subscription table, I use two fields: email address (email_addr) and date (date_added ). You can
Add a field or delete the date_added field. In this example, I just show you what I did, and you can do it as appropriate.
Modify. In my subscription table, the email_addr field is a non-repeated field, meaning you cannot add another exactly the same e-mail
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 the order
Reading and unsubscribing, including the actions of the table itself, may be a bit complicated. I will explain it from start to end, and then combine all the clips,
Put it in a form.

At the beginning of the script, it is to open the database and prepare the timestamp. Dealing with these inconspicuous things at the beginning can always be mitigated for me.
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 )". So,
The first thing about the script is to check whether the value of $ op is equal to "ds ". This value is sent only after the form is submitted. So if
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 ="



";

}--------------------------------------------------------------------------------
You will notice that I put the text in the $ text_block variable. By placing the text in a variable, all I need to do next is
The value of $ text_block is output 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 means that when the submit button is pressed, it will be reloaded.
. 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", according to it, the user can
To 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 ". So let's continue to look at the above
If... statement of, once submitted, it will be skipped (because $ op = "ds "). If the value of $ op is "ds" and the value of $ action is "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 the response.

--------------------------------------------------------------------------------
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 ="

Thank you for your registration!


";

} Else {

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

$ Text_block ="

You have subscribed to it!


";

}

}--------------------------------------------------------------------------------
Next: What should I do when the value of $ action is "unsub" (unsubscribe) instead of "sub. Okay, just like above.
It is as simple as that, so extend the else if... statement and add a piece of code to check whether e-mail exists in the subscription table before it is deleted.
If yes, delete it and print the response; otherwise, ignore it.

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

// Check that the subscription has been made and unsubscribe them; otherwise, information is returned.

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

$ Check_result = mysql_query ($ check)
Or die ("you cannot check the email 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 unsubscribe

$ Text_block ="

You cannot find your email address in the list!


You have not been unsubscribed because the email you entered is not in the database. ";

} Else {

// If $ check_num is not 0, the user is in the list and can be unsubscribed.

$ SQL = "delete from subscribers
Where email_addr = \ "$ email \"";

@ Mysql_query ($ SQL) or die ("email cannot be deleted. ");

$ Text_block ="

Unsubscribe successful!


";
}

}

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

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



Subscription/unsubscription




Subscription/unsubscription





--------------------------------------------------------------------------------
The following is a complete program list:

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

// 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 ");

If ($ op! = "Ds "){

// Subscribe/unsubscribe

$ Text_block ="



";
} 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 ="

Thank you for your registration!


";

} Else {

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

$ Text_block ="

You have subscribed to it!


";

}

} Else if ($ op = "ds") & ($ action = "unsub ")){

// Check that the subscription has been made and unsubscribe them; otherwise, information is returned.

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

$ Check_result = mysql_query ($ check)
Or die ("you cannot check the email 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 unsubscribe

$ Text_block ="

You cannot find your email address in the list!


You have not been unsubscribed because the email you entered is not in the database. ";

} Else {

// If $ check_num is not 0, the user is in the list and can be unsubscribed.

$ SQL = "delete from subscribers
Where email_addr = \ "$ email \"";

@ Mysql_query ($ SQL) or die ("email cannot be deleted. ");

$ Text_block ="

Unsubscribe successful!


";
}

}
?>




Subscription/unsubscription




Subscription/unsubscription





--------------------------------------------------------------------------------
Now that you have an appropriate subscription/unsubscription mechanism, I will show you how to send a news letter with only one simple form
And an email script. ("While" loop is your good friend !). First, it is a form named "send_mail.html. Form action
Something like "do_send_mail.phtml", and I only use one text field (subject) to write the topic and one
Newsletter ). You can use form fields as needed, as long as the form and script are modified as appropriate.

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



Send email




Send a Newsletter





--------------------------------------------------------------------------------
The last note is about the form action. this script is called "do_send_mail.phtml ". The script first looks for $ subject and
$ Newletter value, and redirect to the form if one of their values is null:

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

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

}--------------------------------------------------------------------------------
Next, connect to the database and retrieve the email address from the subscription table:

--------------------------------------------------------------------------------
// 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. ");

$ SQL = "select email_addr from subscribers ";

$ Res = mysql_query ($ SQL) or die ("the email address cannot be obtained. ");
--------------------------------------------------------------------------------
Before entering the cycle of sending mail information, create an additional mail header. Here, I only use the "From:" line:

$ Headers = "From: \" Your Mailing List \" \ N ";


The email sending cycle is now in progress. First, use the mysql_fetch_array function (or a function similar to your database)
Records are placed in an array. If you retrieve more than one field, it may make more sense. I use it because it is faster. The following statement
Through the mail () function to send e-mail to each mailbox in the list:

--------------------------------------------------------------------------------
While ($ row = mysql_fetch_array ($ res )){

$ Email_addr = $ row [0];

Mail ("$ email_addr", "$ subject", $ newsletter, $ headers );

}
--------------------------------------------------------------------------------
The values of $ subject and $ newletter are input in the previous form. Add a line of output statement at the end of the script so that you know
The line is complete. This is all done! The complete "do_send_mail.phtml" script looks like:

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

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

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

} Else {

// 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. ");

$ SQL = "select email_addr from subscribers ";

$ Res = mysql_query ($ SQL) or die ("the email address cannot be obtained. ");

$ Headers = "From: \" Your Mailing List \" \ N ";


While ($ row = mysql_fetch_array ($ res )){

$ Email_addr = $ row [0];

Mail ("$ email_addr", "$ subject", $ newsletter, $ headers );

}

Echo "email 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.