سؤال

I am attempting to make a simple php form mail.
all im after is an email that has looks

Name: joe blogs
Email: joe@blogs.co.uk
Message: anything.
Answer 1: Red
Answer 2: green
Answer 3: Blue

in the example below I have removed my email address... the problem is I fill out the fields and hit the submit button and I get my message to say the message is sent but no email ever comes. it was all working fine till I added the $questX fields then it stopped working. but I see no difference in the $name filed. so i'm quite confused.

<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
 //send email
    $email = $_REQUEST['email'] ;
    $name = $_REQUEST['name'] ;
    $quest = $_REQUEST['quest'] ;
    $questb = $_REQUEST['questb'] ;
    $questc = $_REQUEST['questc'] ;
    $subject = "Competition Entry" ;
    $message = $_REQUEST['message'] ;
    mail("MYEMAIL@HOME.com", $subject, "Name:" . $name, "From:" . $email, "Answer 1:" . 
    $quest, "Answer 2:" . $questb, "Answer 3:" . $questc, $message );
    echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{
    echo "<form method='post' action='win-a-deck2.php'>
            <label for='name'>Name:</label><input name='name' type='text'  placeholder='Enter your fullname'><br>
            <label for='email'>Email:</label><input name='email' type='text'><br>
            <label for='message'>Message:</label><br>
            <input name='message' type='text'><br>
            <label for='quest'>Question 1:</label><input name='quest' type='text'  placeholder='Answer to Question 1'><br>
            <label for='questb'>Question 2:</label><input name='questb' type='text'  placeholder='Answer to Question 2'><br>
            <label for='questc'>Question 3:</label><input name='questc' type='text'  placeholder='Answer to Question 3'><br>
            <input type='submit'>
        </form>";  
}  
?>
هل كانت مفيدة؟

المحلول

Simple php email form:

<form action="process.php" method="post">
<ul style="list-style-type: none;">
<li>Name: <input type="text" name="name" size="30" maxlength="40"></li>
<li>&nbsp;</li>
<li>Email: <input type="text" name="email" size="30" maxlength="40"></li>
<li>&nbsp;</li>
<li><input type="submit" name="submit" value="Send"></li>
</ul>
</form>

The code for the process.php page is as follows:-

Thanks, <?php
@extract($_POST);
$sub="Form feedback";
$name = stripslashes($name);
$email = stripslashes($email);
mail('admin@your-domain.com',$sub,"$name $email","From: $name <admin@your-domain.com>");
echo stripslashes($name);
?> , we will drop you a line shortly.

Tutorial:

http://wickham43.net/formemail.php

It goes over auto reply and non response.

Also just for general purposes:

http://php.net/manual/en/function.mail.php

نصائح أخرى

The mail function:

bool mail ( 
1. string $to ,
2. string $subject , 
3. string $message 
4. [, string $additional_headers 
5. [, string $additional_parameters ]] )

http://www.php.net/manual/en/function.mail.php

You are using it like this:

mail(
1. "MYEMAIL@HOME.com", 
2. $subject,
3. "Name:" . $name, 
4. "From:" . $email, 
5. "Answer 1:" . 
$quest, "Answer 2:" . $questb, "Answer 3:" . $questc
6. , $message );

Definitely, you are passing incorrect parameters to the function, that's why it doesn't work. Try this:

mail(
"MYEMAIL@HOME.com", 
$subject, 
"Name:" . $name . "Answer 1:" . $quest, "Answer 2:" . $questb
. "Answer 3:" . $quest . $message, 
"From:" . $email );

First, you should edit your form to have the questions name be an array. That way you can use $_POST['questions'] as an array, and use a foreach loop on it, rather than type out each question into it's own variable.

Secondly, turn on error reporting and see what errors you get.

Third, you have too many parameters in mail() http://us1.php.net/manual/en/function.mail.php You have $to, $subject, then a bunch more strings which should all be in ONE $message string.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top