<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="robots" CONTENT="NOINDEX,NOFOLLOW">
<title>E-Mail Subscriber List</title>
<link rel="stylesheet" type="text/css" href="email.css">
</head>
<body>
<h3><u>E-Mail Subscriber List</u></h3><br>
<?php


########
# INFO #
########

/*
Title:
E-Mail Subscriber List (I Guess?)
Call it whatever you'd like...

Author:
Eric O'Callaghan (eric@ericoc.com)

Date Created:
August 10, 2006 (Friday)

Requirements:
PHP
SMTP

Installation:
1. You'll have to put this PHP file on your website
2. You will have to make a file to store the email addresses in (and chmod it to 0666)
The file is "emails.txt" by default, or you can change it in the variables section if you'd like
3. You can create a CSS file to style the page if you'd like (email.css by default, or you can change the filename at the top of this script)
A good example is at http://ericoc.com/mail/email.css
4. It's suggested to add an e-mail address and try sending something to it
5. Do whatever


Notes:

Feel free to edit this script, but if you've found a great
way to improve upon it, go ahead and email me (eric1207@gmail.com)
with your changes and I'll update it on the website (http://ericoc.com/mail/)
and give you plenty of credit.

A single e-mail address cannot be added to the list twice.

The administrator password (which you change in the variables section)
is entered as if you were going to add an e-mail address, but when submitted it will
take you to a form so that you can send an e-mail to all of the addresses in the list.

To remove an e-mail address from the list, you will have to manually edit the file
(emails.txt or whatever you change it to in the variables section)

Bugs:

Thanks to Simon Evans for pointing out that there was an error 
with the success message after sending e-mail from the admin area!

Thanks to Thomas McCartney for multiple suggestions!


Enjoy..
*/

##########################
# VARIABLES - EDIT THESE #
##########################

// What do you want the administrator password to be (to send emails)?
// Enter the password into the form to add an e-mail address and it will give you a form to send email to the users
$adminpass "password";

// What do you want the From address for emails sent from the script to be?
$fromemail "webmaster@domain.com";

// What's the name of the file emails will be stored in?
$file "emails.txt";

// How many e-mail addresses should the file be limited to?
// Set to 0 for unlimited
$limit "100";

############################
# BE CAREFUL EDITING BELOW #
############################

// Create function to limit number of addresses
function checklimit ($limit) {

    
// Get filename and limit
    
global $file;

    
// Find number of lines in file
    
$lines file($file);
    
$num_lines count($lines);

    
// Error if limit reached
    
if ($limit == "0") {
        
$limitreached "NO";
    } elseif (
$num_lines >= $limit) {
        
$limitreached "YES";
    } else {
        
$limitreached "NO";
    }

    
// Return error
    
return $limitreached;
}

// Process the form to send email if submitted
if (($_GET['do'] == "send") && ($_POST['password'] == $adminpass) && (isset($_POST['subject'])) && (isset($_POST['body']))) {

    
// Open the list
    
$fh fopen($file"a");
    if (
$fh) {

        
// Get the contents of the list
        
$data file($file);

        
// Pick out each email in the list
        
for ($n 0$n count($data); $n++) {
            
$line explode("\n"$data[$n]);

            
// Send an email to each individual address in the list
            
mail($line[0], $_POST['subject'], $_POST['body'], "From: $fromemail");
        }

        
// Success message
        
$msg "The e-mail was sent to all addresses on the list.";
 
    
// Error if list can't be opened
    
} else {
        
$error "Sorry, the mailing list cannot be accessed - please try again later.";
    }

    
// Close the list
    
fclose($fh);

// Process the form to add an email if submitted
} elseif (($_GET['do'] == "add") && (isset($_POST['email']))) {

    
// Replace the < and > in e-mail address for showing on HTML page
    
$lefttag "&lt;";
    
$righttag "&gt;";
    
$EmailFix str_replace("<"$lefttag$_POST[email]);
    
$EmailFix str_replace(">"$righttag$EmailFix);

    
// Check if admin
    
if ($_POST['email'] == $adminpass) {

        
// Show form to send email
        
echo "<form method=\"POST\" action=\"" $_SERVER['SCRIPT_NAME'] . "?do=send\">\n";
        echo 
"<input type=\"hidden\" name=\"password\" value=\"$_POST[email]\">\n";
        echo 
"Subject:<br>\n<input type=\"text\" name=\"subject\"><br>\n";
        echo 
"Body:<br>\n<textarea name=\"body\" cols=\"50\" rows=\"20\"></textarea><br>\n";
        echo 
"<input type=\"submit\" value=\"Send\">\n";

        
// End the script
        
echo "</body>\n";
        echo 
"</html>\n";
        exit;

    
// Check address limit
    
} elseif (checklimit($limit) == "YES") {
        
$error "Sorry, the limit of e-mail addresses allowed has been reached and new addresses cannot be added at this time.";

    
// Check the email
    
} elseif (!eregi("^([a-z ])*(<)?[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}(>)?$"$_POST['email'])) {
        
$error "Please enter a valid e-mail address.";

    
// Add the email if it doesn't already exist
    
} else {

        
// Open the list
        
$fh fopen($file"a");
        if (
$fh) {

            
// Get the contents of the list
            
$data file($file);

            
// Compare each email in list with the one being entered
            
for ($n 0$n count($data); $n++) {
                
$line explode("\n"$data[$n]);

                
// Error if email being entered matches one in the list
                
if (eregi("^(\\$_POST[email])$"$line[0])) {
                    
$emailexists "yes";
                    
$error "That e-mail address $EmailFix is already in the list.";
                }
            }

        
// Error if list can't be opened
        
} else {
            
$emailexists "yes";
            
$error "Sorry, the mailing list cannot be accessed - please try again later.";
        }

        
// Write to file if email doesn't already exist
        
if ($emailexists != "yes") {

            
// Write to the list and give success message
            
fwrite($fh"$_POST[email]\n");
            
$msg "Your e-mail address $EmailFix was successfully added to the list!";
        }

        
// Close the list
        
fclose($fh);
    }
}

// Show any errors encountered
if (isset($error)) {
    echo 
"<font color=\"red\"><b>" $error "</b></font><br><br>\n";

// Show success message if there was one and end script, and no errors were encountered
} elseif (isset($msg)) {
    echo 
"<b>" $msg "</b>\n";
    echo 
"</body>\n";
    echo 
"</html>\n";
    exit;
}

?>
<form method="POST" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>?do=add">
Email Address: <input type="text" name="email"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>