2013-04-23 261 views
1

在我的HTML頁面中,我創建了一個響應表單。 該操作是將te值發送到PHP文件。這個PHP文件必須照顧發送電子郵件。當在字段中輸入值並按下提交按鈕時,我會得到我的「成功」頁面(並且,當我填寫錯誤時,出現錯誤頁面)。所以這部分似乎工作正常。PHP郵件不發送電子郵件

我已經在我的Mac上安裝了MAMP作爲PHP服務器。在php.ini文件中,sendmail的路徑設置爲/usr/sbin/sendmail -t -i

據我所知,這是正確的。但是,不發送郵件。

有什麼建議嗎?請指教。

請參閱下面的互聯網上找到,並改變了代碼:

<?php 
/* 
This first bit sets the email address that you want the form to be submitted to. 
You will need to change this value to a valid email address that you can access. 
*/ 
$webmaster_email = "[email protected]"; 

/* 
This bit sets the URLs of the supporting pages. 
If you change the names of any of the pages, you will need to change the values here. 
*/ 
$feedback_page = "contact.html"; 
$error_page = "error_message.html"; 
$thankyou_page = "thank_you.html"; 

/* 
This next bit loads the form field data into variables. 
If you add a form field, you will need to add it here. 
*/ 
$naam = $_REQUEST['naam']; 
$email = $_REQUEST['email'] ; 
$telefoon = $_REQUEST['telefoon']; 
$website = $_REQUEST['website']; 
$opmerkingen = $_REQUEST['opmerkingen'] ; 

/* 
The following function checks for email injection. 
Specifically, it checks for carriage returns - typically used by spammers to inject a CC list.  
*/ 
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; 
} 
} 

// If the user tries to access this script directly, redirect them to the feedback form, 
if (!isset($_REQUEST['email'])) { 
header("Location: $feedback_page"); 
} 

// If the form fields are empty, redirect to the error page. 
elseif (empty($naam) || empty($email) || empty($opmerkingen)) { 
header("Location: $error_page"); 
} 

// If email injection is detected, redirect to the error page. 
elseif (isInjected($email_address)) { 
header("Location: $error_page"); 
} 

// If we passed all previous tests, send the email then redirect to the thank you page. 
else { 
mail("$webmaster_email", "Contactformulier van OpenWater", 
$comments, "From: $email"); 
header("Location: $thankyou_page"); 
} 
?> 
+2

請發佈您用來發送郵件的代碼。 – 2013-04-23 12:32:30

+0

@Jamie,謝謝你的支持。只需添加php代碼。 – A3O 2013-04-23 12:40:33

+1

正如其他人所提到的,也許你應該首先檢查一下PHP能否以最簡單的形式發送郵件。我不認爲它比這更簡單:'<?php var_dump(mail('[email protected]','test','test')); ?>'。你看到了什麼? – 2013-04-23 13:15:45

回答

1

可以有,爲什麼它不工作多發的原因。

首先,請確定:您真的確定php不發送郵件嗎?在某些情況下,接收方的收件箱中不會顯示帶有不正確標題的郵件(至少ms交換可能會給您帶來麻煩)。Zend Mail的郵件類是非常優秀的,但一定要RTM;)

如果您確認這不是問題,可以通過PHP郵件()函數發送它來嘗試。

關於sendmail:您是否安裝了sendmail(並且位於/ usr/sbin/sendmail)?

最後它也可能是一個防火牆問題。網絡或Mac上是否有防火牆?

+0

謝謝你的回覆。由於我對PHP很陌生,並不是每個人都清楚。可以說,因爲成功或錯誤頁面我得到的PHP頁面正在工作,但我沒有收到一封電子郵件。我將它發送到我自己的地址,在同一個mac上發送消息。 PHP的郵件功能?我怎麼做? Sendmail在我的Mac上是對的。關閉防火牆一段時間,只是發送消息沒有幫助。請指教。 – A3O 2013-04-23 12:56:04

+0

從上面的鏈接: <?php $ to ='[email protected]'; $ subject ='subject'; $ message ='hello'; $ headers ='From:[email protected]'。 「\ r \ n」。 '回覆:[email protected]'。 「\ r \ n」。 'X-Mailer:PHP /'。 phpversion(); 郵件($ to,$ subject,$ message,$ headers); ?> 適合你的作品? – f4der 2013-04-23 14:20:15

+0

如上所述:我對PHP非常陌生,並且對於如何在您的評論中實現或測試代碼沒有深入瞭解。請指教。 – A3O 2013-04-23 14:32:09

1

嘗試修改您的mail()功能位:

mail("$webmaster_email", "Contactformulier van OpenWater", 
$comments, "From: $email"); 

$headers = 'MIME-Version: 1.0' . PHP_EOL; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . PHP_EOL; 
$headers .= "FROM: YourDisplayName.com <".$email.">" . PHP_EOL; 
mail($webmaster_email, "Contactformulier van OpenWater", $comments, $headers); 

讓我知道這對你的作品。如果您在正文中使用HTML格式(這將爲您的$comments),則會定義多個headers

編輯(見下面我的意見):

$headers = 'MIME-Version: 1.0' . PHP_EOL; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . PHP_EOL; 
$headers .= "FROM: YourDisplayName.com <".$email.">" . PHP_EOL; 
if (mail($webmaster_email, "Contactformulier van OpenWater", $comments, $headers)) 
    echo 'Success'; 
else 
    echo 'Mail not sent'; 
+0

我在一個單獨的窗口中在瀏覽器中獲得一個空白頁面。沒有收到郵件。 – A3O 2013-04-23 13:01:56

+0

好吧,在標題下面添加一個額外的檢查,這樣你就可以看到它是否返回任何東西或什麼都沒有:'if(mail($ webmaster_email,「Contactformulier van OpenWater」,$ comments,$ headers)){echo'Success' ;} else {echo'Mail not sent';}' – 2013-04-23 13:08:20

+0

謝謝你在這裏幫助我。我很抱歉,但我仍然得到一個空白頁(send-mail.php),並沒有回聲。 – A3O 2013-04-23 13:13:16

1

錯誤報告

空白頁可能意味着頁和錯誤顯示錯誤關閉。粗略的搜索顯示,默認情況下,MAMP不顯示PHP錯誤,因此您應該首先嚐試啓用錯誤顯示,方法是在php.ini中將error_reporting設置爲E_ALL,以及display_errorsOn(我發現this blog post about error reporting in MAMP,可能或可能不相關)。

之後,您應該看到是什麼導致您的腳本中的問題。

Sendmail配置

還有一點要注意瞭解電子郵件發送的是,當你正在使用MAMP,你可能有後綴或exim作爲MTA,這兩個假裝是sendmail的,也就是說, sendmail路徑上的鏈接實際上是exim或postfix。至少在exim的情況下,-i標誌使它等待更多的輸入,所以電子郵件永遠不會被髮送。

如果可以,請嘗試將您的php.ini中的sendmail_path/usr/sbin/sendmail -t -i更改爲/usr/sbin/sendmail -t。我認爲這應該有所幫助。

+0

仍然在我的瀏覽器中獲得一個空白的send_mail.php窗口,沒有成功或錯誤頁面。我仍然沒有收到消息。 – A3O 2013-04-23 13:32:44

+0

我在關於如何啓用錯誤顯示的答案中添加了新的部分,以便您確切地看到問題出在哪裏。 – pilsetnieks 2013-04-23 13:49:25

+0

謝謝你,我已經改變了我的php.ini文件,它現在給我66行上的一個分析錯誤。從另一個答案我改變了我的代碼,66行是其中之一。該行是:'headers ='MIME-Version:1.0'。 PHP_EOL;'我必須改回它嗎? – A3O 2013-04-23 13:59:02