我在我的个人 Ubuntu LAMP 服务器上托管我的网站。一切正常,但联系表格。我已将 Postfix 配置为通过我的 gmail smtp 中继电子邮件,它运行良好。我知道我的服务器正在发送电子邮件。我已经使用以下代码多次验证了这一点到各种地址:
echo "Test mail from postfix" | mail -s "Test Postfix" you@example.com
我还通过创建一个小的 PHP 文件“test.php”并将其保存在我的 /var/www 目录中来验证 PHP 是否正常工作。然后我通过在浏览器中输入http://www.mywebsite.com/test.php来调用该文件。无论发送到地址如何,它都始终有效。
因此,知道服务器发送电子邮件,并且 PHP 正在运行,这将引导我找到代码,或者当单击联系表单上的发送按钮时调用 PHP 文件相关的一些问题。
这是联系表格的html代码:
<form method="post" name="contact" action="mail.php">
<input type="hidden" name="post" value="Send" />
<label for="name">Name:</label> <input type="text" id="name" name="name" class="required input_field" />
<label for="email">Email Address:</label> <input type="text" id="email" name="email" class="validate-email required input_field" />
<label for="url">Phone:</label> <input type="text" name="url" id="url" class="input_field" />
<label for="text">Message:</label> <textarea id="text" name="text" rows="0" cols="0" class="required"></textarea>
<input style="font-weight: bold;" type="submit" class="submit_btn" name="submit" id="submit" value="Send" />
<input style="font-weight: bold;" type="reset" class="submit_btn" name="reset" id="reset" value="Reset" />
</form>
显然,此代码设置在页面内并具有匹配的 CSS。
这是 PHP 文件“mail.php”:
<?php
if(isset($_POST['email'])) {
// CHANGE THE TWO LINES BELOW
$email_to = "tljefam@gmail.com";
$email_subject = "OYH Tech Website Email";
function died($error) {
// error code
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['author']) ||
!isset($_POST['email']) ||
!isset($_POST['url']) ||
!isset($_POST['text'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['author']; // required
$email_from = $_POST['email']; // required
$url = $_POST['url']; // not required
$text = $_POST['text']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
$error_message .= 'The name you entered does not appear to be valid.<br />';
}
if(strlen($text) < 2) {
$error_message .= 'The message you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Phone: ".clean_string($url)."\n";
$email_message .= "Message: ".clean_string($text)."\n";
// email headers
$headers = 'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
?>
当我在联系表单上单击发送时,它会加载一个空白页面,仅此而已。没有错误代码或任何类型的消息,更重要的是没有发送电子邮件。某些浏览器会给出以下消息: 网站在检索http://www.mywebsite.com/mail.php时遇到错误它可能因维护而关闭或配置不正确。
需要明确的是,我只希望此联系表将电子邮件发送到我的 gmail 地址,仅此而已。我没有在此服务器上收到电子邮件。
任何帮助将不胜感激。我已经为此工作了 4 天。