[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 458
  • Last Modified:

Need script to collect email addresses

I send out a newsletter to subscribed customers each month.  I need a webpage that allow individuals to enter their email addresses they can start receiving the latest newsletter.  I would like the email addresses to be stored in a text file. Thanks.
0
mcdonald_g
Asked:
mcdonald_g
1 Solution
 
AndyCommented:
Here's an example using PHP that checks the file to see if the email address is already listed and if not writes the email to a new line. If you want the file to be stored somewhere else then just change mailinglist.txt to path/to/mailinglist.txt. (The extension of this webpage must be .php)

<?php
if($_GET['signup'] == "true"){
    $errors = array();
    if($_POST['email'] <> ""){;      
        //If the email field is not empty we validate the email
        if (ereg("^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$", $_POST['email'])){
            //If it's ok we open your txt file and check to see if its already listed
            $exists = false;
            if(file_exists("mailinglist.txt")){
                $handle = fopen("mailinglist.txt","r") or die("Failed to open file!");
                while (!feof($handle)) {
                    $line = preg_replace("'\n'", "", fgets($handle));
                    if($line == $email){
                        $exists = true;
                        array_push($errors, "That email address is already in the mailing list!");
                    }
                }  
               fclose($handle);
            }
            //If it doesn't exist we reopen (or create) the file for writing
            if($exists == false){
                $handle = fopen("mailinglist.txt","a+");
                fwrite($handle, "$email\n");
                fclose($handle);
            }
        } else {
            array_push($errors, "That does not appear to be a valid email address!");
        }
    } else {
        array_push($errors, "You did not enter an email address!");
    }
}
?>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php if($_GET['signup'] == "true" && count($errors) == 0){ ?>
You have successfully joined the mailing list!
<?php } elseif(count($errors) > 0){ ?>
The following errors occured...<br><br>
<?php foreach($errors as $e){
      echo "$e<br>";
} ?>
<br>Please click the back button on your browser to correct the mistakes...
<?php } else { ?>
<form name="form1" method="post" action="?signup=true">
<p>To receive regular updates and information enter your email address in the box below...</p>
<p><input name="email" type="text" id="email" size="30"></p>
<p><input type="submit" name="Submit" value="Join The Mailing List"></p>
</form>
<?php } ?>
</body>
</html>
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now