php mail()不会通过xampp发送电子邮件

时间:2018-05-05 20:26:56

标签: php html

我正在尝试创建一个可以向我的电子邮件地址发送电子邮件的html表单,这是我的HTML代码

<form method="post" name="myemailform" action="form-to-email.php">
<p>
    <label for='name'>Enter Name: </label><br>
    <input type="text" name="name">
</p>
<p>
    <label for='email'>Enter Email Address:</label><br>
    <input type="text" name="email">
</p>
<p>
    <label for='message'>Enter Message:</label> <br>
    <textarea name="message"></textarea>
</p>
<input type="submit" name='submit' value="submit">

<script language="JavaScript">
    var frmvalidator  = new Validator("myemailform");
    frmvalidator.addValidation("name","req","Please provide your name"); 
    frmvalidator.addValidation("email","req","Please provide your email"); 
    frmvalidator.addValidation("email","email","Please enter a valid email 
address"); 
</script>

这是我的PHP代码

<?php
if(!isset($_POST['submit']))
{
    echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];
if(empty($name)||empty($visitor_email))
{
    echo "Name and email are mandatory!";
    exit;
}
if(IsInjected($visitor_email))
{
    echo "Bad email value!";
    exit;
}
$email_from = &$visitor_email;
$email_subject = &$subject;
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".

$to = "a@gmail.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');


// Function to validate against any email injection attempts
function IsInjected($str)
{
    $injections = array('(\n+)',
                        '(\r+)',
                        '(\t+)',
                        '(%0A+)',
                        '(%0D+)',
                        '(%08+)',
                        '(%09+)'
                        );
    $inject = join('|', $injections);
    $inject = "/$inject/i";
    if(preg_match($inject,$str))
    {
        return true;
    }
    else
    {
        return false;
    }
}
?> 

然而,当我在xampp中启动apache时,它没有发送电子邮件,我是否必须在真实服务器中运行它,或者我的php代码有问题 任何形式的帮助将不胜感激

0 个答案:

没有答案