简体   繁体   English

php联系表单在验证后不发送邮件

[英]php contact form not sending mail after validation

I'm using a contact form for my website, which I validate and then email to myself, the validation is working correctly and it emails me if the user enters all the details correctly first time. 我正在使用我的网站的联系表格,我先对其进行验证,然后通过电子邮件发送给自己,验证工作正常,如果用户第一次正确输入所有详细信息,它将通过电子邮件发送给我。 However if the user enters incorrect data, then corrects it and hits send again, it won't send an email, below is the form and PHP code I have so far. 但是,如果用户输入了不正确的数据,然后对其进行了更正并再次点击发送,它将不会发送电子邮件,以下是我到目前为止使用的表单和PHP代码。

HTML code for contact form 联系人表单的HTML代码

<form action="contact.php" method="post">
<fieldset>

<label for="name">Name:<span class="star">*</span></label> <br />
<input type="text" name="name" id="name" placeholder="Enter your name" maxlength="50" required />
<label for="email">Email:<span class="star">*</span></label> <br />
<input type="email" name="email" id="email" placeholder="Enter your email address" maxlength="100" required />
<label for="number">Telephone: </label><input type="tel" name="number" id="number" placeholder="Enter your phone number" maxlength="12" />
<label for="message">Message:<span class="star">*</span></label>
<textarea name="message" id="message" placeholder="Enter your message" cols="54" rows="5" required></textarea>

<p class="small"><span class="star">*</span>&nbsp; Denotes a required field </p>

<input type="submit" id="send" name="send" value="Send" />

</fieldset>
</form>

PHP code for sending the form 用于发送表单的PHP代码

    function fix_string($var)
{
    if(get_magic_quotes_gpc()) $var = stripslashes($var);
    $var = strip_tags($var);
    return $var;
}

{
    $details = array('name' => fix_string($_POST['name']),
            'email' => fix_string($_POST['email']),
            'number' => fix_string($_POST['number']),
            'message' => fix_string($_POST['message']));
}

$send = $_POST['send'];
$message = "";

 foreach ($details as $field => $detail)
    $message .= $field . ": " . $detail . "<br />";

$to = "smokey.12345@hotmail.co.uk";
$subject = "Website contact form";
$message = wordwrap($message, 70, "/r/n");
$headers = "From ". $details['email'];

function trim_value(&$value)
{
    $value = trim($value);
}

array_walk($details, 'trim_value');

if ($send)
{
    foreach ($details as $field => $detail)
    {
        if (empty($detail) && $field!='number')
            echo "<p class='error'>Please fill in the required field: " . ucfirst($field) . "<br /></p>";

    }
}
else
{
    mail($to, $subject, $message, $headers);
    echo "<p class='success'>Mail was sent successfully</p>";
}

EDIT 编辑

In order for the code to work on the same page, you need to set the action to action="" 为了使代码在同一页面上工作,您需要将操作设置为action=""

Otherwise, you need to use two pages. 否则,您需要使用两个页面。 One for your form and one for contact.php which is your handler. 一种用于您的表单,另一种用于contact.php ,即您的处理程序。 I suggest you use two pages, but here is a version that will work inside one page. 我建议您使用两页,但这是一个可以在一页内使用的版本。

<?php

if(isset($_POST['send'])) {

function fix_string($var)
{
    if(get_magic_quotes_gpc()) $var = stripslashes($var);
    $var = strip_tags($var);
    return $var;
}

    $details = array('name' => fix_string($_POST['name']),
            'email' => fix_string($_POST['email']),
            'number' => fix_string($_POST['number']),
            'message' => fix_string($_POST['message']));

function trim_value(&$value)
{
    $value = trim($value);
}

array_walk($details, 'trim_value');

    foreach ($details as $field => $detail)

    {
        if (empty($detail) && $field!='number')

            die("<p class='error'>Please fill in the required field: " . ucfirst($field) . "<br /></p>");

    }

$message = "";
 foreach ($details as $field => $detail)
    $message .= $field . ": " . $detail . "\n";

$send = $_POST['send'];
$email = $_POST['email'];

$to = "smokey.12345@hotmail.co.uk";
$subject = "Website contact form";

$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();

    mail($to, $subject, $message, $headers);
    echo "<p class='success'>Mail was sent successfully</p>";


exit;

} // closing brace for if(isset($_POST['send'])) {

?>

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>

<body>

<form action="" method="post">
<fieldset>

<label for="name">Name:<span class="star">*</span></label> <br />
<input type="text" name="name" id="name" placeholder="Enter your name" maxlength="50"  />
<label for="email">Email:<span class="star">*</span></label> <br />
<input type="email" name="email" id="email" placeholder="Enter your email address" maxlength="100"  />
<label for="number">Telephone: </label><input type="tel" name="number" id="number" placeholder="Enter your phone number" maxlength="12" />
<label for="message">Message:<span class="star">*</span></label>
<textarea name="message" id="message" placeholder="Enter your message" cols="54" rows="5" ></textarea>

<p class="small"><span class="star">*</span>&nbsp; Denotes a required field </p>

<input type="submit" id="send" name="send" value="Send" />

</fieldset>
</form>

</body>
</html>

Original answer 原始答案

This line is not properly formatted. 该行的格式不正确。

$message = wordwrap($message, 70, "/r/n");

change it to: 更改为:

$message = wordwrap($message, 70, "\r\n");

You need to use \\ instead of / 您需要使用\\而不是/

EDIT 编辑

The only way I could get your form to work, is to add a die function. 我可以使您的表单起作用的唯一方法是添加一个die函数。

Try this now: 立即尝试:

<?php

function fix_string($var)
{
    if(get_magic_quotes_gpc()) $var = stripslashes($var);
    $var = strip_tags($var);
    return $var;
}

    $details = array('name' => fix_string($_POST['name']),
            'email' => fix_string($_POST['email']),
            'number' => fix_string($_POST['number']),
            'message' => fix_string($_POST['message']));

function trim_value(&$value)
{
    $value = trim($value);
}

array_walk($details, 'trim_value');

    foreach ($details as $field => $detail)

    {
        if (empty($detail) && $field!='number')

            die("<p class='error'>Please fill in the required field: " . ucfirst($field) . "<br /></p>");

    }

$message = "";
 foreach ($details as $field => $detail)
    $message .= $field . ": " . $detail . "\n";

$send = $_POST['send'];
$email = $_POST['email'];

$to = "smokey.12345@hotmail.co.uk";
$subject = "Website contact form";

$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();

    mail($to, $subject, $message, $headers);
    echo "<p class='success'>Mail was sent successfully</p>";


exit;

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM