如何将自定义字段添加到php电子邮件脚本

时间:2013-06-05 21:20:08

标签: php formmail

我正在尝试制作一个简单的php格式邮件 我以后都是 一个看起来像

的电子邮件
Name: joe blogs
Email: joe@blogs.co.uk
Message: anything.
Answer 1: Red
Answer 2: green
Answer 3: Blue

在下面的示例中,我删除了我的电子邮件地址... 问题是我填写字段并点击提交按钮,我收到消息说消息已发送但没有电子邮件来。 在我添加$ questX字段然后它停止工作之前一切正常。但是我发现$ name字段没有区别。所以我很困惑。

<?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>";  
}  
?>

3 个答案:

答案 0 :(得分:0)

简单的php电子邮件表单:

<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>

process.php页面的代码如下: -

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.

教程:

http://wickham43.net/formemail.php

它会自动回复并且无响应。

也仅用于一般目的:

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

答案 1 :(得分:0)

邮件功能:

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

您正在使用它:

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 );

当然,您正在向函数传递不正确的参数,这就是它无效的原因。试试这个:

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

答案 2 :(得分:0)

首先,您应该编辑表单以使问题名称为数组。这样你可以使用$ _POST [&#39; questions&#39;]作为数组,并在其上使用foreach循环,而不是将每个问题输入到它自己的变量中。

其次,打开错误报告并查看您获得的错误。

第三,你在mail()中有太多参数 http://us1.php.net/manual/en/function.mail.php 你有$ to,$ subject,然后是一堆更多的字符串,它们都应该在ONE $ message字符串中。