php邮件表单不起作用

时间:2014-01-23 00:32:18

标签: php mail-form

我想我已经修好了。这是原来的:

<?php
    if(isset($_POST['email'])) {

    $email = $_POST['email'];
    $name = $_POST['name'];
    $message = $_POST['message'];;
    $subject = $email = $_POST['subject'];;

    $email_to = $email;
    $header = 'From: 1totheN <website@1tothen.com>' . "\r\n";
    function clean_text($string) {
    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad,"",$string);
    }
    $email_message .= "Name: ".clean_text($email)."\n";
    $email_message .= "Email: ".clean_text($email)."\n";
    $email_message .= "Message: ".clean_text($message)."\n";
    @mail($email_to, $subject, $email_message, $header);
    echo "Your message has been sent.";

}
?>

以下是HTML页面上的标记:

<form class="contact-form" id="subscribeForm" action="mail.php" method="post">
       <input type="text" name="name" class="required" placeholder="YOUR NAME">
       <input type="text" name="email" class="required email" placeholder="EMAIL ADDRESS">
       <input type="text" name="subject" class="required last" placeholder="SUBJECT">
       <textarea name="message" placeholder="YOUR MESSAGE"></textarea>
       <input type="submit" name="submit" value="SUBMIT YOUR MESSAGE">
       <h6 id="form_result">Your email will not be published.</h6>
</form>

这是修复:

$email_to = "webmail@1tothen.com";
$header = 'From: 1totheN <webmail@1tothen.com>' . "\r\n";
$email_message .= "Name: ".clean_text($name)."\n";

我认为这样可以解决问题。我在$ email_to字段中添加了一封电子邮件,并添加了$ name。似乎工作。

1 个答案:

答案 0 :(得分:0)

以下现在有效。这只是一些小的语法错误,你有一些额外的;,这一行是错误的,可能是一个错误:$subject = $email = $_POST['subject'];

我还把这个函数移到if语句之外......把它放在那里并不是错误的,它看起来有点奇怪。

function clean_text($string) {
    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad,"",$string);
}

if(isset($_POST['email'])) {
    $email = $_POST['email'];
    $name = $_POST['name'];
    $message = $_POST['message'];
    $subject = $_POST['subject'];

    $email_to = $email;
    $header = 'From: 1totheN <website@1tothen.com>' . "\r\n";
    $email_message .= "Name: ".clean_text($email)."\n";
    $email_message .= "Email: ".clean_text($email)."\n";
    $email_message .= "Message: ".clean_text($message)."\n";
    @mail($email_to, $subject, $email_message, $header);
    echo "Your message has been sent.";
}
?>