PHP联系表单脚本不发送电子邮件

时间:2014-10-14 21:59:41

标签: php validation contact-form

我对php不是很好,我的朋友用这个php联系表单脚本帮助了我。但这似乎并没有将电子邮件发送到所需的地址。你能否说一下这个剧本可能出现的问题?

我真的很感激。感谢

    <?php
$error = array();

if(!empty($_POST['contact_submit']) && ($_POST['contact_submit'] == 'submit') ) {
    if(!empty($_POST['name'])) {
        $name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
    } else {
        $error[] = 'Please enter your name.';
    }

    if(!empty($_POST['email'])) {
        if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
        } else {
            $error[] = 'Please enter a correct email address.';
        }
    } else {
        $error[] = 'Please enter your email address.';
    }

    if(!empty($_POST['phone'])) {
        if(filter_var($_POST['phone'], FILTER_VALIDATE_INT)) {
            $phone = filter_var($_POST['phone'], FILTER_SANITIZE_NUMBER_INT);
        } else {
            $error[] = '<i>Phone number</i> only expects number';
        }
    } else {
        $error[] = 'Please enter your email address.';
    }

    if(!empty($_POST['time'])) {
        $time = filter_var($_POST['time'], FILTER_SANITIZE_STRING);
    } else {
        $error[] = 'Please enter your best time to contact.';
    }

    if(!empty($_POST['msg'])) {
        $msg = filter_var($_POST['msg'], FILTER_SANITIZE_STRING);
    } else {
        $error[] = 'Please enter your message.';
    }

    if(empty($error)) {
        $to      = 'your@email.com';
        $subject = 'from contact form';

        $message = $phone . "\r\n";
        $message .= $time . "\r\n";
        $message .= $msg;

        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $headers .= 'From: '.$name.' <'. $email.'>' . "\r\n" .
            'Reply-To: '.$name.' <'. $email . '>' ."\r\n";

        //echo '<pre>'; var_dump($to, $subject, $message, $headers); echo '</pre>'; die();

        mail($to, $subject, $message, $headers);
    }
}

?>


<?php
if(!empty($error)) {
    echo '<ul class="error">';
    echo '<li>' . implode('</li><li>', $error) . '</li>';
    echo '</ul>';
}
?>

<form method="post" action="">
    <input type="text" name="name" value="" placeholder="Enter your name" class="email_form"/>
    <input type="text" name="email" value="" placeholder="Enter your email address" class="email_form"/>
    <input type="text" name="phone" value="" placeholder="Phone number" class="email_form"/>
    <input type="text" name="time" value="" placeholder="Best time to contact. e.g. 3 am" class="email_form"/>
    <textarea name="msg" placeholder="Your message" class="email_form"></textarea>

    <input type="image" value="submit" name="contact_submit" src="images/submit.png" width="96" height="43" class="email_button">
</form>

1 个答案:

答案 0 :(得分:2)

更改此行:

if(!empty($_POST['contact_submit']) && ($_POST['contact_submit'] == 'submit') ) {

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

<input type="image" value="submit" name="contact_submit" src="images/submit.png" width="96" height="43" class="email_button">

<input type = "submit" value="submit" name="contact_submit">

PHP正在寻找提交类型,并且您正在使用图片类型。

  • 如果您仍想将图像用作提交按钮:

您需要更改:

if(!empty($_POST['contact_submit']) && ($_POST['contact_submit'] == 'submit') )

另一个条件陈述。

例如:

if(!empty($_POST['email'])){

您可以随时使用要检查的其他字段添加该条件语句,如果它们已设置/为空。

例如:

if(!empty($_POST['email']) || !empty($_POST['name'])){
  • 在测试代码后,代码在这些更改完成之前不起作用。

NB :如果仍未发送/接收邮件,则需要确保mail()确实可供您使用,和/或检查您的日志和垃圾邮件箱。

error reporting添加到文件的顶部,这将有助于在生产测试期间。

error_reporting(E_ALL);
ini_set('display_errors', 1);
  • 会触发发现的任何错误。

<强> 脚注:

手机字段必须是所有数字,否则会失败。

即:555-234-5678无效,但5552345678无效,因此您需要告知您的用户应如何输入。


修改(完整代码) - 在将email@example.com更改为您自己的电子邮件时,完全按照所示进行复制。

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

$error = array();

if(isset($_POST['contact_submit']) ) {
    if(!empty($_POST['name'])) {
        $name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
    } else {
        $error[] = 'Please enter your name.';
    }

    if(!empty($_POST['email'])) {
        if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
        } else {
            $error[] = 'Please enter a correct email address.';
        }
    } else {
        $error[] = 'Please enter your email address.';
    }

    if(!empty($_POST['phone'])) {
        if(filter_var($_POST['phone'], FILTER_VALIDATE_INT)) {
            $phone = filter_var($_POST['phone'], FILTER_SANITIZE_NUMBER_INT);
        } else {
            $error[] = '<i>Phone number</i> only expects number';
        }
    } else {
        $error[] = 'Please enter your email address.';
    }

    if(!empty($_POST['time'])) {
        $time = filter_var($_POST['time'], FILTER_SANITIZE_STRING);
    } else {
        $error[] = 'Please enter your best time to contact.';
    }

    if(!empty($_POST['msg'])) {
        $msg = filter_var($_POST['msg'], FILTER_SANITIZE_STRING);
    } else {
        $error[] = 'Please enter your message.';
    }

    if(empty($error)) {
        $to      = 'email@example.com';
        $subject = 'from contact form';

        $message = $phone . "\r\n";
        $message .= $time . "\r\n";
        $message .= $msg;

        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $headers .= 'From: '.$name.' <'. $email.'>' . "\r\n" .
            'Reply-To: '.$name.' <'. $email . '>' ."\r\n";

        //echo '<pre>'; var_dump($to, $subject, $message, $headers); echo '</pre>'; die();

        mail($to, $subject, $message, $headers);
    }
}

?>


<?php
if(!empty($error)) {
    echo '<ul class="error">';
    echo '<li>' . implode('</li><li>', $error) . '</li>';
    echo '</ul>';
}
?>

<form method="post" action="">
    <input type="text" name="name" value="" placeholder="Enter your name" class="email_form"/>
    <input type="text" name="email" value="" placeholder="Enter your email address" class="email_form"/>
    <input type="text" name="phone" value="" placeholder="Phone number" class="email_form"/>
    <input type="text" name="time" value="" placeholder="Best time to contact. e.g. 3 am" class="email_form"/>
    <textarea name="msg" placeholder="Your message" class="email_form"></textarea>

    <input type = "submit" value="submit" name="contact_submit">

</form>

如果通过替换:

成功发送,您也可以显示消息
mail($to, $subject, $message, $headers);

使用:

if(mail($to, $subject, $message, $headers)){

  echo "Mail sent, thank you.";

 }

 else{
   echo "There was an error.";
}

您还可以记录错误:

0   message is sent to PHP's system logger, using the Operating System's system logging mechanism or a file, depending on what the error_log configuration directive is set to. This is the default option.
1   message is sent by email to the address in the destination parameter. This is the only message type where the fourth parameter, extra_headers is used.
2   No longer an option.
3   message is appended to the file destination. A newline is not automatically added to the end of the message string.
4   message is sent directly to the SAPI logging handler.

<强>即:

if(mail($to, $subject, $message, $headers)){

  echo "Mail sent, thank you.";

 }

 else{
   error_log("Error!", 3, "/var/tmp/mail-errors.log");
}