PHP联系表单消息未收到

时间:2013-09-01 01:21:17

标签: php html css forms contact

您好,我的网站上有简单的电子邮件联系表格,表单的工作原理包括发送邮件的成功,但是,我没有收到指定网络邮件服务器的电子邮件。我正在运行最新的PHP。有些Web服务器缓存邮件,或者我没有看到此代码中的一些错误。

<?php

$EmailFrom = "email@mydomain.com";
$EmailTo = "email@mydomain.com";
$Subject = "Contacting Me";
$Name = Trim(stripslashes($_POST['Name'])); 
$Tel = Trim(stripslashes($_POST['Tel'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Message = Trim(stripslashes($_POST['Message'])); 

// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=index\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

<div id="contact-area">

            <form method="post" action="contactengine.php">
                <label for="Name">Name:</label>
                <input type="text" name="Name" id="Name" />

                <label for="Email">Email:</label>
                <input type="text" name="Email" id="Email" />

                <label for="Message">Thought:</label><br />
                <textarea name="Message" rows="20" cols="20" id="Message"></textarea>

                <input type="submit" name="submit" value="Transmit" class="submit-button" />
            </form>

            <div style="clear: both;"></div></div>

3 个答案:

答案 0 :(得分:2)

发送邮件比运行mail()命令更复杂,您需要查看电子邮件配置的php.ini设置,然后从那里继续进行故障排除。

如果您在未配置mail()函数运行的主机上运行,​​可以尝试使用以下opensource邮件程序:

PHPMailer

您可以尝试使用提供的示例并从您家中连接的本地开发堆栈(XAMPP,MAMP,WAMP)运行,如果配置正确,您应该能够接收邮件。

一旦没问题,您可以在服务器上试用,它也应该发送,如果没有,请检查发送邮件的发送端口是否被阻止。

使用PHPMailer的替代方法,您可以查找一个简单的PHP邮件程序并尝试使用它们。

答案 1 :(得分:1)

请勿使用print "<meta http-equiv=\"refresh\"...,因为在测试过程中,在发送同一封电子邮件的多个副本时会造成无限循环。

使用header('Location: ...');代替exit;

另外,您的所有<meta http-equiv=\"refresh\"都生成了解析错误。

工作示例(已成功收到电子邮件测试):

<?php

$EmailFrom = "email@mydomain.com";
$EmailTo = "email@mydomain.com";
$Subject = "Contacting Me";
$Name = Trim(stripslashes($_POST['Name'])); 
$Tel = Trim(stripslashes($_POST['Tel'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Message = Trim(stripslashes($_POST['Message'])); 

// validation
$validationOK=true;
if (!$validationOK) {

// echo "error 1";

header('Location: error.htm');

exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){

header('Location: http://www.example.com/');

exit;

}
else{

header('Location: error.htm');

exit;

}
?>

答案 2 :(得分:0)