2017-09-05 154 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