2011-05-18 47 views
1

聯繫表格工作得很好,但我不知道如何設置「回覆郵件」。 PHP代碼如下:PHP表單 - 問題與回覆電子郵件

<?php 
// Get Data 
$name = strip_tags($_POST['name']); 
$email = strip_tags($_POST['email']); 
$message = strip_tags($_POST['message']); 

// Send Message 
mail("Message from $name", 
"Name: $name\nEmail: $email\nMessage: $message\n", 
"From: $name <[email protected]>"); 
?> 

我試圖做的是取代「[email protected]」與$電子郵件,但由於某種原因,它崩潰,從不發送任何東西。

+0

當它「崩潰」是一個錯誤產生? – David 2011-05-18 18:33:11

+0

可能因爲您沒有爲'mail()正確指定參數而失敗請參閱文檔:http://us.php.net/manual/en/function.mail.php – 2011-05-18 18:35:37

回答

4

它只是您在郵件標題塊中缺少的Reply-to: [email protected]標題?此外,看起來你錯過了mail()函數的第一個參數,它應該是它發送到的地址。

Reply-to標頭添加到mail()的第三個參數中。

// Send Message 
mail($to_address, "Message from $name", 
    // Message 
    "Name: $name\nEmail: $email\nMessage: $message\n", 
    // Additional headers 
    "From: $name <[email protected]>\r\nReply-to: [email protected]" 
); 

編輯我錯過了這個問題一個逗號,並認爲整個塊是消息,包括名稱&。上面編輯。我看到你已經有一個頭塊。

+0

我會試一試。 – Renan 2011-05-18 18:38:41

+0

您應該用'\ r \ n'分隔標題。 – dtbarne 2011-05-18 18:39:12

+0

我做了更改,並用$ email替換了[email protected],但仍在回覆字段中顯示<[email protected]>。 – Renan 2011-05-18 18:45:07

0

您沒有使用郵件功能的正確參數。看看在documentation

bool mail (string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]]) 

在你的情況,這將是:

mail($to, 
$subject, 
$message, 
"From: $name <[email protected]>"); 

假設你給了它一個$來(這是指誰發送電子郵件)和$主題(電子郵件的主題)。

0

把這個片斷:

<?php 
    //define the receiver of the email 
    $to = '[email protected]'; 
    //define the subject of the email 
    $subject = 'Test email'; 
    //define the message to be sent. Each line should be separated with \n 
    $message = "Hello World!\n\nThis is my first mail."; 
    //define the headers we want passed. Note that they are separated with \r\n 
    $headers = "From: [email protected]\r\nReply-To: [email protected]"; 
    //send the email 
    $mail_sent = @mail($to, $subject, $message, $headers); 
    //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
    echo $mail_sent ? "Mail sent" : "Mail failed"; 
    ?> 

在代碼中,你錯過了第一個參數,女巫應該是誰。