Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 4 1 2 3 ... LastLast
Results 1 to 10 of 38
  1. #1
    Senior Member foodbiz's Avatar
    Join Date
    Apr 2003
    Location
    Los Angeles
    Posts
    484
    Member #
    1070
    I need to create a contest entry form that will generate a mailing list for me!

    Basically people will be filling out the form and then submitting it. Once they submit it I want to have a program that will store it and make a nice usable list for me that I can use to mail to!

    Thanks :smoker:

  2.  

  3. #2
    Senior Member Steax's Avatar
    Join Date
    Dec 2006
    Location
    Bandung, Indonesia
    Posts
    1,207
    Member #
    14572
    I guess I'll just make it...

    Mailing List Script v0.93
    (fixed email validation and the tutorial on how to add new params.)

    Disclaimer: This script was built on the go. There are more variables then perhaps needed. Optimize as you wish.

    You will need PHP and MySQL (although you can use others once you get the concept).

    Preparation: MySQL database

    Here's the database you need to have to allow for this script. I'll only post the specifications, not the SQL, so you can learn how to make your own table with custom fields. If you need help, use phpMyAdmin, which provides an interface to do this.

    1. Make a new database (or use an existing one), for here I'll call it "my_website"
    2. Make a new table, call it "mailing_list"
    3. For this example, you need 2 fields. Add more if you want.
    4. Each field should be a VARCHAR field, with appropriate length.

    Creating the config.php file
    You will need this file as the configuration file, so if you need to change you configuration you can just change it here.

    PHP Code:
    <?php

    // Server database configurations
    define("DB_HOST","localhost");  // This should be your server
    define("DB_USER","root");  // The username
    define("DB_PASS","");  // The password
    define("DB_NAME","my_website");  // The name of the database

    // Other configurations
    define("SENDING_PASSWORD","password"); // The password required to send mail
    define("MAIL_FROM","newsletter@site.com"); // The "from" in the emails
    define("MAIL_REPLY_TO","replyhere@site.com"); // Where people will be pointed to for replying

    // Just do the connection procedure here
    $conn mysql_connect(DB_HOSTDB_USERDB_PASS);

    // Handle failed connection
    if (!$conn) {
        echo 
    "Unable to connect to DB: " mysql_error();
        exit;
    }
      
    // Pick Database and handle error
    if (!mysql_select_db(DB_NAME))
    {
        echo 
    "Unable to select mydbname: " mysql_error();
        exit;
    }

    ?>
    Signup form
    First we need our signup form. It dictates what data the user should give. To keep it simple, I'll just ask for a user's name and email.

    HTML Code:
    <form action="signup.php" method="POST">
        <label for="name">Name:</label>
        <input type="text" name="name" id="name" />
        <label for="email">Your E-mail:</label>
        <input type="text" name="email" id="email" />
        <input type="submit" value="Submit" />
    </form>
    Signup.php
    This file will process the user's signup and add it to the database.

    PHP Code:
    <?
    include "config.php";

    // Obtain data from form and secure it against injections
    $name mysql_real_escape_string($_POST['name']); 
    $email mysql_real_escape_string($_POST['email']);

    // This is to check if it's a valid email address using a regular expression
    $pattern "'^[a-z]+[a-z0-9]*[\.|\-|_]?[a-z0-9]+ 
    @([a-z]+[a-z0-9]*[\.|\-]?[a-z]+[a-z0-9]*[a-z0-9]+){1,4} 
    \.[a-z]{2,4}$'"
    ;  
    if(
    preg_match($pattern,$email) == 0)
    {
      exit(
    "Error - you didn't provide a valid email address!");
    }

    // Now insert into the database...

    $query "INSERT INTO `mailing_list` (`name`,`email`)VALUES('$name','$email')";

    $result mysql_query($query); // Do it!

    header("Location: index.php"); // Set this to where you want them to go next
    ?>

    ... Ok, there's the the basic script to sign up. Either make your script to send the mail, or I'll be back in a moment.

    write-mail.php - or place this HTML in a file
    This is the form you should use to write the mail sent to each person. I've added in some extra functionality - use the string [[name]] to insert the name of the person into the mail. Use this as an example to add more custom tags.

    I've also added a quick password to permit only your access to this interface. Set this password in config.php.

    HTML Code:
    <form action="send-mail.php" method="POST">
        <label for="pass">Password:</label>
        <input type="text" name="spass" id="pass" />
    
        <label for="subject">Subject:</label>
        <input type="text" name="subject" id="subject" />
    
        <label for="mail">Body:</label>
        <textarea name="mail" id="mail"></textarea>
    
        <input type="submit" value="Submit" />
    </form>
    send-mail.php
    Remember, in here you should also add in code to make sure you are the only one able to send this. Perhaps by a login system. Or, I guess, I'll provide a simple one now.

    PHP Code:
    <?php
    include "config.php";

    // Check the password first... this is a crude method
    if($_POST['pass'] != SENDING_PASSWORD){
       echo 
    "Fatal error - wrong password";
       exit();
    }

    // Do query to obtain mailing list
    $sql "SELECT * FROM `mailing_list`";
    $result mysql_query($sql);

    // Handle query error
    if (!$result)
    {
        echo 
    "Could not successfully run query ($sql) from DB: " mysql_error();
        exit;
    }

    // Handle if zero users
    if (mysql_num_rows($result) == 0)
    {
        echo 
    "There are no users!";
        exit;
    }

    // Get the POST stuff from the form
    $subject_template $_POST['subject'];
    $mail_template $_POST['mail'];

    if(
    get_magic_quotes_gpc()){
      
    // This means magic quotes is on and you need to strip slashes.
      
    $subject_template =  stripslashes($subject_template);
      
    $mail_template =  stripslashes($mail_template);
    }

    // Some debug info...
    echo "Starting mail process ----";

    while (
    $row mysql_fetch_assoc($result))
    {
        echo 
    "Name: ".$row["name"];
        echo 
    "Email: ".$row["email"];

        
    $mail $mail_template
        $mail 
    str_replace("[[name]]",$row["name"],$mail);
        
    // You can add more tags like that

        
    $to      $row["email"];
        
    $subject $subject_template;
        
    $message $mail;
        
    $headers 'From: ' MAIL_FROM "\r\n" // Edit these!
            
    'Reply-To: ' MAIL_REPLY_TO "\r\n";  // Edit!

        
    mail($to$subject$message$headers);
        echo 
    "Mail sent to $to with subject $subject";
    }

    mysql_free_result($result);

    ?>
    Hmm... I think thats it. Tell me if something doesn't work. Don't forget that your server needs to be configured for mail first.

    Also, sometimes mail sent in this manner will be marked as "spam". Anyone know a workaround?
    Note on code: If I give code, please note that it is simply sample code to demonstrate an effect. It is not meant to be used as-is; that is the programmer's job. I am not responsible to give you support or be held liable for anything that happens when using my code.

  4. #3
    Senior Member Steax's Avatar
    Join Date
    Dec 2006
    Location
    Bandung, Indonesia
    Posts
    1,207
    Member #
    14572
    Kade - you want each HTML form on their appropriate pages, and provide the PHP on their own pages.

    To add extra parameters
    1. You need to add the field to your table. I'll call it "company".
    2. Add in the appropriate HTML to the signup form.
    HTML Code:
        <label for="company">Company:</label>
        <input type="text" name="company" id="company" />
    3. Add in the signup script.
    PHP Code:
    $company mysql_escape_string($_POST['company']); 
    4. And put in the query.
    PHP Code:
    $query "INSERT INTO `mailing_list` (`name`,`email`,`company`)VALUES('$name','$email','$company')"
    5. Finally, plug in the replacement script.
    PHP Code:
        $mail str_replace("[[name]]",$row["name"],$mail); 
        
    $mail str_replace("[[company]]",$row["company"],$mail); // This is the new one! 
    Note on code: If I give code, please note that it is simply sample code to demonstrate an effect. It is not meant to be used as-is; that is the programmer's job. I am not responsible to give you support or be held liable for anything that happens when using my code.

  5. #4
    Senior Member kade119's Avatar
    Join Date
    Feb 2007
    Location
    USA
    Posts
    532
    Member #
    14888
    Liked
    1 times
    i have to say,,,,,,, STEAX IS A GREAT GUY!

    i appreciate this much and im sure alot of others do too, if i have any further questions regarding this, ill let you know steax

    oh did i say this was the best web board ever? =)

  6. #5
    Senior Member kade119's Avatar
    Join Date
    Feb 2007
    Location
    USA
    Posts
    532
    Member #
    14888
    Liked
    1 times
    this line

    FROM `mailing_list`";


    is my database?

  7. #6
    Senior Member Steax's Avatar
    Join Date
    Dec 2006
    Location
    Bandung, Indonesia
    Posts
    1,207
    Member #
    14572
    Hahahaha, nice to be of service.

    `mailing_list` is your table name. Your database is only selected once in each processing script.

    Got any problems, kade? Does it work? That's all untested code, and will probably undergo some minor modifications for security and bugfixes.

    Once that's done, I'll probably move it to the tutorials section.
    Note on code: If I give code, please note that it is simply sample code to demonstrate an effect. It is not meant to be used as-is; that is the programmer's job. I am not responsible to give you support or be held liable for anything that happens when using my code.

  8. #7
    Senior Member kade119's Avatar
    Join Date
    Feb 2007
    Location
    USA
    Posts
    532
    Member #
    14888
    Liked
    1 times
    so i need to create .html table with the name of mailing_list?

    and i list my dbname where it says "mydbname" correct?

    just wanna make sure i edit the correct lines

  9. #8
    Senior Member Steax's Avatar
    Join Date
    Dec 2006
    Location
    Bandung, Indonesia
    Posts
    1,207
    Member #
    14572
    Wait, no, that's in MySQL! You'll need to access something like phpmyadmin to do this. If you have a local server, you may need to download it first (most hosting packages have it as a feature).

    Essentially you'll need to create a new database and table, then populate it with the fields. This has to do with server databases. You got experience with that yet?
    Note on code: If I give code, please note that it is simply sample code to demonstrate an effect. It is not meant to be used as-is; that is the programmer's job. I am not responsible to give you support or be held liable for anything that happens when using my code.

  10. #9
    Senior Member kade119's Avatar
    Join Date
    Feb 2007
    Location
    USA
    Posts
    532
    Member #
    14888
    Liked
    1 times
    Quote Originally Posted by Steax
    Wait, no, that's in MySQL! You'll need to access something like phpmyadmin to do this. If you have a local server, you may need to download it first (most hosting packages have it as a feature).

    Essentially you'll need to create a new database and table, then populate it with the fields. This has to do with server databases. You got experience with that yet?
    ahh i see, ok yea my hosting has phpmyadmin i ve used it

    i actuallly got a DB already created and i replace my db name where u have "mydbname" correct?

    STEAX IS DA MAN!!!! OF WDF
    WWSD
    what would steax do

  11. #10
    Senior Member foodbiz's Avatar
    Join Date
    Apr 2003
    Location
    Los Angeles
    Posts
    484
    Member #
    1070
    STEAX you are awesome!!!!!!!!!!!!!!!!!!!!


Page 1 of 4 1 2 3 ... LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 05:21 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com