PHP邮件表单不起作用

时间:2015-11-13 18:01:38

标签: php

我的HTML:                          全名                    

<div class="row">
<div class="label"> Age </div>
<div class="input">
<input type="text" id="age" class="detail" name="age" value="" />
</div>
</div>

<div class="row">
<div class="label"> Gender </div>
<div class="input">
Male:<input type="radio" id="radio1"   name="gender" value="" />
Female:<input type="radio" id="radio2" name="gender" value="" />
</div>
</div>

<div class="row">
<div class="label"> Department </div>
<div class="select">
<select id="department" class="detail" name="department" value="" />
<option>Please select below</option>
<option>xxxx</option>
<option>xxxx</option>
</select>
</div>
</div>

<div class="row">
<div class="label"> Who do you want to consult </div>
<div class="select">
<select id="consult" class="detail" name="consult" value="" />
<option>xxxx</option>
<option>xxxx</option>
<option>xxxxy</option>
<option>xxxxx</option>
<option>xxx</option>
<option>xxxxx</option>
</select>
</div>
</div>


<div class="row">
<div class="label"> When do you want to visit us</div>
<div class="date">
<input type="date" id="date" class="detail" name="date" value="" />
</div>
</div>

<div class="row">
<div class="label"> Do you have any specific requests or needs</div>
<div class="request">
<input type="text" id="request" class="detail" name="request" value="" />
</div>
</div>

<div class="row">
<div class="label"> When is a good time to call you</div>
<div class="time">
<input type="time" id="time" class="detail" name="time" value="">
</div>
</div>


<div class="row">
<div class="label"> E-mail </div>
<div class="input">
<input type="email" id="email" class="detail" name="email" value="" />
</div>
</div>

<div class="row">
<div class="label"> Phone number </div>
<div class="input">
<input type="tel" id="tel" class="detail" name="tel" value="">
</div>
</div>


<div id="submit-reset">
<input type="submit" id="submit" name="submit" value="Submit Form" />
<input type="reset" id="reset" name="reset" value="Reset" />
</div>
</form>

我很难为此配置PHP部分 我的PHP代码:     

$fullname = $_POST['fullname'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$department = $_POST['department'];
$consult = $_POST['consult'];
$date = $_POST['date'];
$request = $_POST['request'];
$time = $_POST['time'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$to = "example@email.com";
$subject = "message goes here";

mail ($to, $subject, "From: " .$fullname);
echo "message sent"
?>

使用我的邮件ID进行测试时,它会显示已发送邮件,但我根本没有收到任何邮件。请帮我解决这个问题。我是菜鸟的新手,所以我不知道它是如何工作的:( 此外,任何有关验证的人都会非常感激。

3 个答案:

答案 0 :(得分:0)

您的HTML表单缺少开始表单标记。 确保你的html表单以表单标记开头,方法是post with action是php邮件脚本文件名。

例: <form method="post" action="mail.php"> .. .. .. .. </form>

并在 mail.php 中输入邮件脚本,如下所示

$fullname = $_POST['fullname'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$department = $_POST['department'];
$consult = $_POST['consult'];
$date = $_POST['date'];
$request = $_POST['request'];
$time = $_POST['time'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$to = "example@email.com";
$subject = "message goes here";
$message="Name: ".$fullname."<br/>Age: ".$age,"<br/>Gender",$gender."<br/>Phone".$phone."<br/>Dept:".$department;
if(mail ($to, $subject,$message, "From: " .$fullname)){
    echo "Success";
} else {
    echo "not sent";
}

还要确保正确配置了php邮件服务器。

如果您在localhost计算机上的windows/Linux上进行测试,则应首先配置SMTP设置并启用从localhost发送邮件。

如果您在实时服务器上进行测试,请与服务提供商联系以启用发送邮件

答案 1 :(得分:0)

您尚未提及您的环境,但您可以尝试从命令行发送它,看看邮件是否有效。我怀疑你的服务器无法在php中使用邮件。 您可以尝试使用smtp邮件作为选项 这是一个很棒的邮件课程 https://github.com/PHPMailer/PHPMailer 希望这有帮助

答案 2 :(得分:0)

我认为您需要在服务器上添加From作为地址并添加Reply-to标头,这样当您收到收件箱中的电子邮件时,您可以正常方式回复它。

据我所知,您可以在服务器上看到来自表单的全名。

引用@ nana-partykar获取消息部分 - 从$message开始替换为:

    $message="Name: ".$fullname."<br/>Age: ".$age,"<br/>Gender",$gender."<br/>Phone".$phone."<br/>Dept:".$department;

    $your_email = 'your_email@your_server.com';
    $headers = "From: $your_email" . "\r\n";
    // Additional headers
    $headers .= "Reply-To: $email" . "\r\n";
    if(mail($to,$subject,$message,$headers)){
    echo "Success";
    } else {
    echo "not sent";
    }

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

您还可以尝试在脚本顶部设置sendmail用户:

    ini_set('sendmail_from', 'your_email@your_server.com'); 

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

就验证而言,您可以清理提交的内容,仅允许使用字母,数字和其他一些字符:

    $email = preg_replace("[^a-zA-Z0-9@._-]",'',$_POST['email']);

并且至少通过使用以下内容包装邮件部分来确保基本电子邮件地址不为空

    if($email != ""){

    if(mail....to the end

    }

如果要将任何收到的信息放入数据库,则需要确保它也没有注入攻击代码

How can I prevent SQL injection in PHP?

我建议你在做之前先让它工作......但要做到这一点!