php action script for forms

6 replies
I found this simple script on the web and started to play with it. For some reason it sends me emails but I dont get any of the fields returned. I get the message that "We've recived your information" and I get an email but they are always empty. The names and Ids of the fields in the form are:
subject,detail,customer_mail,name

Code:
<?php

// Contact subject
$subject ="$subject";

// Details
$message="$detail";


// Mail of sender
$mail_from="$customer_mail";

// From
$header="from: $name <$mail_from>";


// Enter your email address
$to ='myemailaddress.info';

$send_contact=mail($to,$subject,$message,$header);


// Check, if message sent to your email
// display message "We've recived your information"
if($send_contact){
echo "We've received your contact information";
}
else {
echo "ERROR";
}
?>
#action #forms #php #script
  • Profile picture of the author webn
    If you just upload this script, you can't see any Subject/Detail or anything. Because, the subject/details are generated dynamically from the form page that is missed here.

    If you don't understand what I mean then, read any php book (form chapter) or use this tutorial "PHP Forms"

    Hope it helps.
    {{ DiscussionBoard.errors[8369589].message }}
  • Profile picture of the author egyptik
    OK, I assume this is how you have your script set up:

    You have a form page (not displayed in your post), which upon form submission posts it to your action script above. I also assume the PHP script posted above is the full action script, without you having left any code out.

    Here is your problem: you assign variables to other variables, which are empty. That's why you recieve empty emails. In fact, the only var you did correctly assign, is the 'to' email adress.

    Example: $subject = "$subject";
    You are assigning an empty var to itself, this equals: $subject = "";

    I assume you are confusing this with the form field names. If that is the case, this code should work for you:

    Code:
    <?php
    
    // Contact subject
    $subject = $_POST['subject'];
    
    // Details
    $message= $_POST['detail'];
    
    
    // Mail of sender
    $mail_from= $_POST['customer_mail'];
    
    // From
    $header="from: ".$_POST['name'] ."<".$_POST['mail_from'].">";
    
    
    // Enter your email address
    $to ='myemailaddress.info';
    
    $send_contact=mail($to,$subject,$message,$header);
    
    
    // Check, if message sent to your email
    // display message "We've recived your information"
    if($send_contact){
    echo "We've received your contact information";
    }
    else {
    echo "ERROR";
    }
    ?>
    Or if you used the GET method, replace all $_POST vars with $_GET.
    {{ DiscussionBoard.errors[8371230].message }}
    • Profile picture of the author rhinocl
      Thanks good to know it was the page I copied not me.
      That gives me the info from the input fields in the body but I get this in the from field:

      Code:
      jimmyg@, UNEXPECTED_DATA_AFTER_ADDRESS@.SYNTAX-ERROR.
      {{ DiscussionBoard.errors[8372669].message }}
  • Profile picture of the author nmcc
    The line:
    $header="from: ".$_POST['name'] ."<".$_POST['mail_from'].">";

    should be:
    $header="from: ".$_POST['name'] ."<".$_POST['customer_mail'].">";
    {{ DiscussionBoard.errors[8380099].message }}
  • Profile picture of the author otfromtot
    I just use this. I've actually used it for almost ten years I think
    Matt's Script Archive: FormMail
    {{ DiscussionBoard.errors[8393570].message }}
  • Profile picture of the author WMSTR
    Are you sure mail(); function is turned on your server?

    Some companys close this function.

    I think you've to use smtp.
    {{ DiscussionBoard.errors[8393971].message }}

Trending Topics