2017-09-05 128 views
0

我有一个不发送邮件的PHP邮件表单。该脚本像用户应该那样运行;把他们带到“谢谢你!”页面,但邮件永远不会到它应该去的电子邮件地址。我该如何解决这个问题?我试图通过其他线程解决它在stackoverflow,但我的问题是独特的。PHP邮件表单不发送邮件

PHP

<?php 
/* Set e-mail recipient */ 
$myemail = "[email protected]"; 

/* Check all form inputs using check_input function */ 
$name = check_input($_POST['name'], "Enter your name"); 
$subject = check_input($_POST['subject'], "Enter a subject"); 
$email = check_input($_POST['email']); 
$message = check_input($_POST['message'], "Write your message"); 

/* If e-mail is not valid show error message */ 
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) 
{ 
show_error("E-mail address not valid"); 
} 
/* Let's prepare the message for the e-mail */ 
$message = " 

Name: $name 
E-mail: $email 
Subject: $subject 

Message: 
$message 

"; 

/* Send the message using mail() function */ 
mail($myemail, $subject, $message); 

/* Redirect visitor to the thank you page */ 
header('Location: http://www.thewebsite.com/thankyou.html'); 
exit(); 

/* Functions we used */ 
function check_input($data, $problem='') 
{ 
$data = trim($data); 
$data = stripslashes($data); 
$data = htmlspecialchars($data); 
if ($problem && strlen($data) == 0) 
{ 
show_error($problem); 
} 
return $data; 
} 

function show_error($myError) 
{ 
?> 
<html> 
<body> 

<p>Please correct the following error:</p> 
<strong><?php echo $myError; ?></strong> 
<p>Hit the back button and try again</p> 

</body> 
</html> 
<?php 
exit(); 
} 
?> 
+0

你使用wordpress吗? –

+0

那么'john.doe @ example.com'不是一个有效的电子邮件吗? https://regex101.com/r/OvRqyw/1 – Andreas

+0

@Sreejith学士学位,不,我使用这个我建立自己的网站。 HTML,CSS和JavaScript我很熟悉,但PHP我不是。 :) –

回答

1

功能邮件返回一个布尔值。正如文件所述,...

如果邮件成功接收传送,返回TRUE,否则返回FALSE 。

重要的是要注意,仅仅因为邮件被接受为 交付,它并不意味着邮件实际上会到达预期的 目的地。

因此,...存储邮件返回的值,以检查是否出现问题。

$mailSent = mail(...); 
if (!$mailSent) { 
    echo "Something went wrong"; 
} 

如果你使用的是Windows,......你要知道,

当PHP是跟一个SMTP服务器直接,如果上了一个句号发现 上的开始

行,它被删除。为了解决这个问题,用双点代替 这些事件。

<?php 
$text = str_replace("\n.", "\n..", $text); 
?> 

另一个最终检查做的是,...你的SMTP服务器安装在运行该脚本的机器?如果没有安装一个。根据您的操作系统或* AMP系统,可能有可能缺少某些东西:smtp可能是空的。此代码可能在生产环境中工作,但不在个人计算机中。

+0

谢谢你的信息,我的SMTP服务器正在我的机器上工作,无论如何我被指示检查。我为服务器机器使用OS X 10.6.8;老,我知道......但它通常能完成工作。 @sensorario –

+0

我强烈建议你不要太使用PHP的mail()方法 - 它不安全或不安全。详情请看这里:https://blog.ripstech.com/2017/why-mail-is-dangerous-in-php/ – slothluvchunk