Solved

Need script to collect email addresses

Posted on 2004-03-26
1
426 Views
Last Modified: 2010-05-18
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
Comment
Question by:mcdonald_g
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 6

Accepted Solution

by:
Andy earned 125 total points
ID: 10694959
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question