1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

email subscription script

Discussion in 'PHP' started by linda-duxbury, Feb 14, 2008.

  1. #1
    Hi,

    I want to collect NAME and EMAIL ADDRESS of my website visitors. I'm looking for a simple php script for this job.

    Could you please give me guidance?

    Thanks.
     
    linda-duxbury, Feb 14, 2008 IP
  2. greenline

    greenline Banned

    Messages:
    910
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What is your budget?
     
    greenline, Feb 14, 2008 IP
  3. kreoton

    kreoton Peon

    Messages:
    229
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Du you tried to search in hotscripts.com or google for it?
     
    kreoton, Feb 14, 2008 IP
  4. linda-duxbury

    linda-duxbury Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi,

    yes, there are many newsletter scripts with this feature.
    but I'm just looking for a simple script which only receive NAME and EMAIL ADDRESS then save them in a file (not database)

    Thanks.

    Hi,

    I'm looking for a free script!

    Thanks.

     
    linda-duxbury, Feb 14, 2008 IP
  5. stoli

    stoli Peon

    Messages:
    69
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I just rolled this:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
    <title>Simple e-mail subscription</title>
    <style type="text/css" media="screen">
    body {
    	font-family: verdana, sans-serif;
    	color: #000;
    	background-color: #fff;
    }
    </style>
    </head>
    <body>
    
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    Name:</span> <input name="name" type="text" size="20">
    e-mail address:</span> <input name="email" type="text" size="20"> 
    <input type="submit" value="Subscribe">
    </form>
    
    <?php
    $subscriberfile = "emails.csv";
    if (array_key_exists('email', $_POST) && $_POST['email']) {
      $subscribers = file($subscriberfile);
      $emails = array();
      foreach ($subscribers as $subscriber) {
        list($email, $name) = split(",",trim($subscriber));
        $emails[$email] = $name;
      }
      if (!check_email_address($_POST['email'])) {
        echo '<p>Your email address <b>' .$_POST['email']. '</b> does not look right. Please check your spelling and try again.</p>';
      }
      elseif (array_key_exists($_POST['email'], $emails)) {
        echo '<p>The email address <b>' .$_POST['email']. '</b> is already subscribed. Thanks again for your interest.</p>';
      }
      else {
        $emails[$_POST['email']] = $_POST['name'];
        $f = fopen($subscriberfile, 'w');
        foreach ($emails as $key => $value) {
          $value = ereg_replace("[^A-Za-z0-9' ]", "", $value);
          fwrite($f, "$key,$value\n");
        }
        fclose($f);
        echo '<p>Thank you. <b>' .$_POST['email']. '</b> has been subscribed.</p>';
      }
    }
    ?>
    
    </body>
    </html>
    
    <?php
    function check_email_address($email) {
      // First, we check that there's one @ symbol, and that the lengths are right
      if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
        // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
        return false;
      }
      // Split it into sections to make life easier
      $email_array = explode("@", $email);
      $local_array = explode(".", $email_array[0]);
      for ($i = 0; $i < sizeof($local_array); $i++) {
        if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
          return false;
        }
      }
      if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
        $domain_array = explode(".", $email_array[1]);
        if (sizeof($domain_array) < 2) {
          return false; // Not enough parts to domain
        }
        for ($i = 0; $i < sizeof($domain_array); $i++) {
          if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
            return false;
          }
        }
      }
      return true;
    }
    ?>
    PHP:
    It writes the submitted email and name to a file in comma separated format (email,name). Email is required, name is not. Email is checked to make sure it looks like an email address.

    As it is this script expects to write to a file "emails.csv" in the same directory as itself. You should put this file outside your document root and alter the $subscriberfile value to point to its location.

    Having said all that if you are doing anything other than the most trivial task you would almost certainly be better off with phplist (http://www.phplist.com/) - its a good quality free (open source) newsletter manager.
     
    stoli, Feb 14, 2008 IP
  6. linda-duxbury

    linda-duxbury Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hi,

    Many thanks. :)

     
    linda-duxbury, Feb 14, 2008 IP
  7. stoli

    stoli Peon

    Messages:
    69
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You're welcome. I hope it does the job for you.
     
    stoli, Feb 14, 2008 IP
  8. mrc00l88

    mrc00l88 Greenhorn

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #8
    how can i edit this code, so that the php code is in a separate file than the html form? i want to have the php code that shows the 'emails.csv' file location in another file rather than having to put the code on each page of my website. thank you!
     
    mrc00l88, Dec 18, 2013 IP