Current location: Hot Scripts Forums » General Community » Script Requests » Simple PHP Email Script


Simple PHP Email Script

Reply
  #1 (permalink)  
Old 01-27-06, 03:16 PM
phillyhotshots phillyhotshots is offline
Newbie Coder
 
Join Date: Jun 2003
Location: Philadelphia
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Simple PHP Email Script

I need a script that does the following, but can't seem to find one anywhere to modify easily:

Written in PHP:
- Name (required)
- Subject Line (required)

Movie Name: Input Movie Title (required)
Person's Name: Input Text 1 (required) Input: Input Text 2 (required)
Person's Name: Input Text 3 (optional) Input: Input Text 4(optional)
Person's Name: Input Text 5 (optional) Input: Input Text 6(optional)
Person's Name: Input Text 7 (optional) Input: Input Text 8(optional)

..... and so on



Returns email like so:

To: submission@mydomain.com
From: Joseph Smith
Subject: Witty Subject

Movie Name: Billy Madison
Frank: Who would you rather bone, Meg Ryan or Jack Nicholson?
Billy: Jack Nicholson now, or 1974?
Frank: 1974.
Billy: Meg Ryan
Reply With Quote
  #2 (permalink)  
Old 01-28-06, 06:17 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
I wrote a simple contact form script where you can add as much fields as you want and make which ever you want "required".

Save this as "config.php"
PHP Code:

<?php


// Your email address to which the mail will be sent to 
$email_to "your@email.com";

// Custom subject for the email
$email_subject "Contact form submission";

// Site title. 
// Default: Nico's Contact form
$title "Nico's Contact form";

// Size for input fileds
// Default: 30
$maxchars "30";

// Enable Javascript pre-check
// Default: true
$pre_check "true";

// Where do you want the user to be redirected to after submitting the form?
$redirect_to "http://www.your-site.com/contact/thankyou.php";

/* Fields for contact form.
Feel free to add as much fields as you want. Just make sure to 
use a higher number than the last. */
$field[1] = "Name";
$require[1] = "true";

$field[2] = "Email";
$require[2] = "true";

$field[3] = "Company";
$require[3] = "true";

$field[4] = "Phone";
$require[4] = "false";

$field[5] = "Direction";
$require[5] = "false";


?>
And save this as whatever you want. This is the form:
PHP Code:

<?php


include("config.php");

if (isset(
$_POST["Submit"])) {

$id 1;
$nr_errors 0;
$email_string "Email from contact form:\n-------------------------------------------------\n";

    
    while (isset(
$field[$id]) && !empty($field[$id])) {    
    
        if ((
$require[$id] !== "true") or (!empty($_POST["input_"$id]))) {
        
            
$email_string .= $field[$id] ." : "$_POST["input_"$id] ."\n";
            
$id++;
            
        } else {
        
            
$error 1;
            
$nr_errors++;
            
$id++;
            
        }

    }
    
    if (!isset(
$error)) {
    
        
// Send email if all required fileds are filled out.    
        
mail($email_to$email_subject$email_string"From: \"" .$email_to"\" <"$email_to .">\n");
        
        
header("Location: "$redirect_to);
            
    }

}
// Start HTML output
?>
<html>
    <head>
    <title><?php echo $title ?></title>
    
    <?php
    
// Error message of precheck disabled and user tries to submit empty fields
    
if (isset($error)) {
    
        echo 
'<script type="text/javascript">
        
            alert("'
$nr_errors .' error(s) ocurred. Please fill out all required fields!");
                
            </script>'
;
        
    }
    
    if (
$pre_check == "true") {
    
    
$id 1;
    
    
// Javascript pre check
    
echo '    
    <script type="text/javascript">
    
    function PreCheck() {
    
        '
;
    
        while (isset(
$field[$id]) && !empty($field)) {
            
                if (
$require[$id] == "true") {
                
                    echo 
'var field_'$id .' = document.getElementById("input_'$id .'").value;';
                                    
                    echo 
"\n\nif (field_"$id ." == \"\") { alert(\"Please fill out the \\\""$field[$id] ."\\\" field.\"); return false; }\n\n";
                    
                }
            
            
$id++;
        
        }
    
    echo 
'}
        
    </script>'
;
    
    }
    
    
?>
    </head>
    <body>
    <?php
    
    $id 
1;
    
    
// Form start    
    
echo '<form method="post" action="'$PHP_SELF .'" name="form">';
    
    
    while (isset(
$field[$id]) && !empty($field[$id])) {
    
        if (
$require[$id] == "true") {
    
            
$req[$id] = '<font size="2" color="red">*</font>';
        
        } else {
        
            
$req[$id] = '<font size="1">(Optional)</font>';
        
        }
    
        echo 
'<input type="text" name="input_'$id .'" id="input_'$id .'" value="'$_POST["input_"$id] .'" size="'$maxchars .'" /> '$field[$id] .'&nbsp;'$req[$id] .'<br /><br />';
        
        echo 
"\n";
        
        
$id++;
        
    }
    
    echo 
'<input type="submit" name="Submit" value="Submit" onclick="return PreCheck();" />';
    
    echo 
'</form>';
    
?>
</body>
</html>
Hope this helps!
Reply With Quote
  #3 (permalink)  
Old 01-28-06, 07:11 AM
Barnz1986 Barnz1986 is offline
Aspiring Coder
 
Join Date: Jan 2006
Posts: 506
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by phillyhotshots
I need a script that does the following, but can't seem to find one anywhere to modify easily:

Written in PHP:
- Name (required)
- Subject Line (required)

Movie Name: Input Movie Title (required)
Person's Name: Input Text 1 (required) Input: Input Text 2 (required)
Person's Name: Input Text 3 (optional) Input: Input Text 4(optional)
Person's Name: Input Text 5 (optional) Input: Input Text 6(optional)
Person's Name: Input Text 7 (optional) Input: Input Text 8(optional)

..... and so on



Returns email like so:

To: submission@mydomain.com
From: Joseph Smith
Subject: Witty Subject

Movie Name: Billy Madison
Frank: Who would you rather bone, Meg Ryan or Jack Nicholson?
Billy: Jack Nicholson now, or 1974?
Frank: 1974.
Billy: Meg Ryan
I can make this script for you in exchange for a link in a decent position on your website.
Reply With Quote
  #4 (permalink)  
Old 01-28-06, 08:26 AM
phillyhotshots phillyhotshots is offline
Newbie Coder
 
Join Date: Jun 2003
Location: Philadelphia
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by nico_swd
I wrote a simple contact form script where you can add as much fields as you want and make which ever you want "required".
Thanks for sharing that script nico. I tried it out and it's very similiar to most found on hotscripts. I see how to require fields, but how do I make a customizable subject line? Also, how can I make the email display the way i showed in my first post?

Quote:
Originally Posted by Barnz1986
I can make this script for you in exchange for a link in a decent position on your website.
Umm... I may consider this if I can't figure it out on my own. BTW, what site would I be linking to?
Reply With Quote
  #5 (permalink)  
Old 01-28-06, 08:37 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Ok, I've changed it a bit. You will recieve the mail as described above, but it won't work if you change the 3 first fields.

This is the new "config.php"
PHP Code:

<?php


// Your email address to which the mail will be sent to 
$email_to "your@email.com";

// Site title. 
// Default: Nico's Contact form
$title "Nico's Contact form";

// Size for input fileds
// Default: 30
$maxchars "30";

// Enable Javascript pre-check
// Default: true
$pre_check "true";

// Where do you want the user to be redirected to after submitting the form?
$redirect_to "http://www.your-site.com/contact/thankyou.php";

/* Fields for contact form.
Feel free to add as much fields as you want. Just make sure to 
use a higher number than the last. */
$field[1] = "Name";
$require[1] = "true";

$field[2] = "Email";
$require[2] = "true";

$field[3] = "Subject";
$require[3] = "true";

$field[4] = "Phone";
$require[4] = "false";

$field[5] = "Direction";
$require[5] = "false";


?>
And this is the new form
PHP Code:

<?php


include("config.php");

if (isset(
$_POST["Submit"])) {

$id 1;
$nr_errors 0;
$email_string "Email from contact form:\n-------------------------------------------------\n";

    
    while (isset(
$field[$id]) && !empty($field[$id])) {    
    
        if ((
$require[$id] !== "true") or (!empty($_POST["input_"$id]))) {
        
            
$email_string .= $field[$id] ." : "$_POST["input_"$id] ."\n";
            
$id++;
            
        } else {
        
            
$error 1;
            
$nr_errors++;
            
$id++;
            
        }

    }
    
    if (!isset(
$error)) {
    
        
// Send email if all required fileds are filled out.    
        
mail($email_to$_POST["input_3"], $email_string"From: \"" .$_POST["input_1"]. "\" <"$_POST["input_2"] .">\n");
        
        
header("Location: "$redirect_to);
            
    }

}
// Start HTML output
?>
<html>
    <head>
    <title><?php echo $title ?></title>
    
    <?php
    
// Error message of precheck disabled and user tries to submit empty fields
    
if (isset($error)) {
    
        echo 
'<script type="text/javascript">
        
            alert("'
$nr_errors .' error(s) ocurred. Please fill out all required fields!");
                
            </script>'
;
        
    }
    
    if (
$pre_check == "true") {
    
    
$id 1;
    
    
// Javascript pre check
    
echo '    
    <script type="text/javascript">
    
    function PreCheck() {
    
        '
;
    
        while (isset(
$field[$id]) && !empty($field)) {
            
                if (
$require[$id] == "true") {
                
                    echo 
'var field_'$id .' = document.getElementById("input_'$id .'").value;';
                                    
                    echo 
"\n\nif (field_"$id ." == \"\") { alert(\"Please fill out the \\\""$field[$id] ."\\\" field.\"); return false; }\n\n";
                    
                }
            
            
$id++;
        
        }
    
    echo 
'}
        
    </script>'
;
    
    }
    
    
?>
    </head>
    <body>
    <?php
    
    $id 
1;
    
    
// Form start    
    
echo '<form method="post" action="'$PHP_SELF .'" name="form">';
    
    
    while (isset(
$field[$id]) && !empty($field[$id])) {
    
        if (
$require[$id] == "true") {
    
            
$req[$id] = '<font size="2" color="red">*</font>';
        
        } else {
        
            
$req[$id] = '<font size="1">(Optional)</font>';
        
        }
    
        echo 
'<input type="text" name="input_'$id .'" id="input_'$id .'" value="'$_POST["input_"$id] .'" size="'$maxchars .'" /> '$field[$id] .'&nbsp;'$req[$id] .'<br /><br />';
        
        echo 
"\n";
        
        
$id++;
        
    }
    
    echo 
'<input type="submit" name="Submit" value="Submit" onclick="return PreCheck();" />';
    
    echo 
'</form>';
    
?>
</body>
</html>
This should work as you wanted!

Last edited by nico_swd; 01-28-06 at 08:50 AM.
Reply With Quote
  #6 (permalink)  
Old 01-28-06, 09:33 AM
phillyhotshots phillyhotshots is offline
Newbie Coder
 
Join Date: Jun 2003
Location: Philadelphia
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by nico_swd
Ok, I've changed it a bit. You will recieve the mail as described above, but it won't work if you change the 3 first fields.

This should work as you wanted!
Wow, thanks again nico. I tried it out and got this error after submitting the form:
Warning: Cannot modify header information - headers already sent by (output started at config.php:40) in /form.php on line 34
Reply With Quote
  #7 (permalink)  
Old 01-28-06, 09:41 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
I see, you're right! There must be a whitespace after the "?>" in the "config.php" file. Try removing it. I just did and it works for me:

http://www.nicoswd.com/form/form.php
Reply With Quote
  #8 (permalink)  
Old 01-28-06, 09:54 AM
phillyhotshots phillyhotshots is offline
Newbie Coder
 
Join Date: Jun 2003
Location: Philadelphia
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by nico_swd
I see, you're right! There must be a whitespace after the "?>" in the "config.php" file. Try removing it. I just did and it works for me:

http://www.nicoswd.com/form/form.php
Ok great! It works beautifully. I'm customizing this now for my needs, and will share a link once its complete. Thanks again for all of your help.
Reply With Quote
  #9 (permalink)  
Old 01-28-06, 09:57 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Ok, nice! Glad I could help!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Run Your Own Profitable and VERY unique eBusiness Voltaire General Advertisements 3 03-30-10 06:36 AM
i need a simple login and registration php script plz lunarboi Script Requests 3 01-03-06 12:04 AM
Looking for a simple PHP login/register script Wheeler Script Requests 2 07-27-05 12:30 PM
converting perl script to php help!! macruddace Perl 1 04-14-05 02:38 PM
trying to create my first simple (?) php script, need guidance HotShotPhoto PHP 1 09-06-04 08:12 PM


All times are GMT -5. The time now is 11:54 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.