05 May, 2002
How to set up a "subscribe to comments" feature on your blog (MT specific)
Posted by: Jennifer In: How to's
Leaving this post here for archival purposes – but the latest version of the script is now in cgi format – find more info here
This is so easy you're going to laugh… it's very much cut and paste, very little customzing.
Please note that very soon I'll be re-doing things here so that scripts like this will be in a download section, and you'll just be able to download the whole thing with a readme of instructions… but in the meantime….
Also note – this was a team effort between myself and Lynda (who you can see from the last post did quite a job on this!)
Step 1
(this will be the "hardest" most customizing of this script)
part a:
on your individual entry page – go to the comments form. In the form tag for the comments change this line:
action="<$MTCGIPath$>mt-comments.cgi"
to:
action="http://www.YOURSITE.COM/PATH_TO_THIS_FILE_YOU_WILL_BE_CREATING_VERY_SOON/emailtolist.php"
part b:
you'll see a few hidden tags… add this one:
<input type="hidden" name="title" value="<$MTEntryTitle encode_html="1"$>"/>
part c:
This will allow people to automatically subscribe/unsubscribe to comments when they enter a new comment.
add this somewhere BEFORE end form tag (</form>):
<input type="radio" name="subscription" value="subscribe"> Subscribe<br>
<input type="radio" name="subscription" value="unsubscribe"> Unsubscribe<br> (email field must be filled in)
part d:
This will allow people to subscribe/unsubscribe to comments WITHOUT having to enter a new comment.
Add this outside of the comment form:
<form action="http://www.YOURSITE.COM/PATH_TO_THIS_FILE_YOU_WILL_BE_CREATING_VERY_SOON/addsubscriber.php" method="post" name="addsubscriber">
Enter email address to subscribe/unsubscribe to comments on this post without having to post a comment:<br />
<input type="radio" name="subscription" value="subscribe"> Subscribe<br>
<input type="radio" name="subscription" value="unsubscribe"> Unsubscribe<br> (email field must be filled in)
<input type="hidden" name="entry_id" value="<$MTEntryID$>" />
Email: <input name="email" size="40" />
<input type="submit" value="submit" name="submit">
</form>
Step 2
Create a folder called "commentsubscribe". CHMOD this folder to full read/write/execute access (777)
Step 3
Copy and paste the following into a file. Name it: emailtolist.php
(Make sure to change the appropriate variables -marked in red – to match your setup)
<?
$text = stripslashes($text);
$fileString = "/home/PATH_TO_YOUR_FOLDER/commentsubscribe/$entry_id.txt";
if ($subscription == "subscribe")
{
if (file_exists($fileString)) {
$emailList = file("$fileString");
for ($i = 0; $i < count($emailList); $i++) {
if (trim($emailList[$i]) == trim($email)) {
$doRun = false;
}
}
}
if (!isset($doRun)) {
$file = fopen("$fileString", "a+");
if ($file) {
fputs($file, $email);
fputs($file, "\n");
fclose($file);
}
}
}
if (file_exists($fileString))
{
if ($subscription == "unsubscribe")
{
$array = file($fileString);
$file = fopen($fileString, "w");
if ($array)
{
foreach($array as $oldemail)
{
$oldemail = trim($oldemail);
if ($oldemail != $email) fputs($file, "$oldemail\n");
}
fclose ($file);
}
}
}
if (!isset($isSent)) {
if (file_exists($fileString)) {
$emailList = file("$fileString");
$count = count($emailList);
for ($i = 0; $i < $count; $i++) {
$recepient = trim($emailList[$i]);
$title = stripslashes($title);
mail("$recepient ", "New Comment on $title", "Author: $author\ncomment: $text", "From: YOU@YOURDOMAIN.COM");
}
}
$submitForm = "document.comments_form.submit()";
$isSent = true;
} else {
$submitForm = "document.location='http://YOUR_HOME_PAGE.COM'";
/*
I put this here in case someone tried to reload THIS page…
that way the $isSent variable is set to true (at the bottom)…
and the form won't process again post the comment again
and annoy people with bogus emails
*/
}
$text = htmlspecialchars($text);
?>
<html><head><title></title></head><body onLoad="<? echo $submitForm ?>">
<form method="post" action="http://LOCATION_OF_YOUR_MT_INSTALLATION/mt/mt-comments.cgi" name="comments_form">
<input type="hidden" name="static" value="<? echo $static ?>" />
<input type="hidden" name="entry_id" value="<? echo $entry_id ?>" />
<input type="hidden" name="ipadd" value="<? echo $ip ?>" />
<input type="hidden" name="author" value="<? echo $author ?>" />
<input type="hidden" name="email" value="<? echo $email ?>" />
<input type="hidden" name="url" value="<? echo $url ?>" />
<input type="hidden" name="text" value="<? echo $text ?>" />
<input type="hidden" name="post" value="submit" />
</form>
<?
$isSent = "true";
?>
please wait…
your comment is being submitted.
</body>
Step 4
Copy and paste the following into a file. Name it: addsubscriber.php
(Make sure to change the appropriate variables -marked in red – to match your setup)
<?
$fileString = "/home/PATH_TO_YOUR_FOLDER/commentsubscribe/$entry_id.txt";
if ($subscription == "subscribe")
{
if (file_exists($fileString)) {
$emailList = file("$fileString");
for ($i = 0; $i < count($emailList); $i++) {
if (trim($emailList[$i]) == trim($email)) {
$doRun = false;
}
}
}
if (!isset($doRun)) {
$file = fopen("$fileString", "a+");
if ($file) {
fputs($file, $email);
fputs($file, "\n");
fclose($file);
}
}
}
if (file_exists($fileString))
{
if ($subscription == "unsubscribe")
{
$array = file($fileString);
$file = fopen($fileString, "w");
if ($array)
{
foreach($array as $oldemail)
{
$oldemail = trim($oldemail);
if ($oldemail != $email) fputs($file, "$oldemail\n");
}
fclose ($file);
}
}
}
header("Location: http://YOUR_HOME_PAGE.COM/");
exit;
?>
features to be added soon (but not yet on here)… the ability to GLOBAL REMOVE oneself from ALL subscriptions…. Also – this is not setup for "preview comment"…coming soon… 
scriptygoddess