<?php
########
# INFO #
########
/*
Title:
E-Mail Subscriber List (I Guess?)
Call it whatever you'd like...
Author:
Eric O'Callaghan ([email protected])
Date Created:
August 10, 2006 (Friday)
Updated by: Alex "apexad" Martin on February 8th, 2007 (unsubscribe added)
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://eric1207.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 ([email protected])
with your changes and I'll update it on the website (http://eric1207.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 = "[email protected]";
// What's the name of the file emails will be stored in?
$file = "/home/your_username/emails.txt"; //this should not be a public directory
// How many e-mail addresses should the file be limited to?
// Set to 0 for unlimited
$limit = "0";
############################
# 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], 'Newsletter'.$_POST['subject'], $_POST['body']."\n\n--------------------\nAccording to our records, you have signed up for this list.\nTo Unsubscribe please go to http://www.yourwebsite.com/", "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'])) && ($_POST['submit'] != "Unsubscribe") ) {
// Replace the < and > in e-mail address for showing on HTML page
$lefttag = "<";
$righttag = ">";
$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['PHP_SELF'] . "?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";
echo "</form>";
// Check address limit
} elseif ((checklimit($limit) == "YES") && ($_POST['email'] != $adminpass)) {
$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'])) && ($_POST['email'] != $adminpass)) {
$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);
}
}
elseif ( ($_GET['do'] == "add") && (isset($_POST['email'])) && ($_POST['submit'] == "Unsubscribe") ) {
// Open the list
$fh = fopen($file, "a");
if ($fh) {
// Get the contents of the list
$data = file($file);
//delete the file and make a new one
fclose($fh);
unlink($file);
copy($file.'_blank',$file);
chmod($file,0666);
$fh = fopen($file, "a");
// 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";
$msg = "The e-mail address: $line[0] has been deleted from the list";
} else {
fwrite($fh, "$line[0]\n");
}
}
// 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") {
// populate error message
$error = "Your e-mail address $EmailFix was not found in 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><br><br>\n";
}
if ($_POST['email'] != $adminpass) {
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>?do=add">
Email Address: <input type="text" name="email"><br>
<input type="submit" name="submit" value="Subscribe"><input name="submit" type="submit" value="Unsubscribe">
</form>
<?php } ?>