联系表单仅在我不输入电子邮件时发送

时间:2014-08-24 23:23:06

标签: php email

如果我在电子邮件字段中输入“test”,但是使用实际的电子邮件地址,它将会发送,它将无法正常工作。我真的不明白为什么会这样。

PHP代码:

<?php
$action=$_REQUEST['action'];
if ($action=="")    /* display the contact form */
    {
    ?>
    <form  action="" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="action" value="submit">
    Your name:<br>
    <input name="name" type="text" value="" size="30"/><br>
    Your email:<br>
    <input name="email" type="text" value="" size="30"/><br>
    Your message:<br>
    <textarea name="message" rows="7" cols="30"></textarea><br>
    <input type="submit" value="Send email"/>
    </form>
    <?php
    } 
else                /* send the submitted data */
    {
    $name=$_REQUEST['name'];
    $email=$_REQUEST['email'];
    $message=$_REQUEST['message'];
    if (($name=="")||($email=="")||($message==""))
        {
        echo "All fields are required, please fill <a href=\"\">the form</a> again.";
        }
    else{        
        $from="From: $name<$email>\r\nReturn-path: $email";
        $subject="Message sent using your contact form";
        mail("MAIL@MAIL.COM", $subject, $message, $from);
        echo "Email sent! You will be redirected to the home page in 5 seconds.";
        header('refresh:5;url=index.html');
        }
    }  
?>

1 个答案:

答案 0 :(得分:0)

尝试修改电子邮件标题:

$sender = "$name <$email>";
$from = "MIME-Version: 1.0"."\n";
$from .= "Content-type: text/html; charset=utf-8"."\n";
$from .= "Content-Transfer-Encoding: 8bit"."\n";
$from .= "From: $sender"."\n";
$from .= "Return-Path: $sender"."\n";
$from .= "Reply-To: $sender\n";

我建议您考虑使用PHPMailer类作为源代码,请参阅https://github.com/PHPMailer/PHPMailerPHPMailer Connection error