2009-11-14 34 views
0

我試圖做在PHP中一個很簡單的郵件形式,但得到的錯誤:php郵件需要配置才能正常工作嗎?

PHP Notice: Undefined index: in /var/www/html/sites/send_contact.php on line 10, referer: http://example.com/contact2.php

我的PHP文件看起來像這樣:

<?php // send_contact.php 
// Contact subject 
$subject =$_POST["$subject"]; 
// Details 
$message=$_POST["$detail"]; 

// Mail of sender 
$mail_from=$_POST["$customer_mail"]; 
// From 
$name2=$_POST["$name2"]; 

$header="From: $mail_from\r\n"; 

// Enter your email address 
$to="[email protected]"; 

$send_contact=mail($to,$subject,$message,$header); 

// Check, if message sent to your email 
// display message "We've recived your information" 
if($send_contact){ 
echo "We've recived your contact information"; 
} 
else { 
echo "ERROR"; 
} 
?> 
+0

如果您的字段被命名爲subject,那麼您可以鍵入$ _POST [「subject」]。如果您希望$ _POST中的值存儲在$ subject中,請使用$ _POST [$ subject]。 – 2009-11-14 22:39:06

+0

以防萬一:一旦這個問題解決了,請務必在投入生產之前進行一些輸入清理...... http://www.phpbuilder.com/columns/ian_gilfillan20060412.php3 – grossvogel 2009-11-14 23:22:35

回答

4

最簡單的猜測是你訪問你的var時發生錯誤iables。

代替:

$name2=$_POST["$name2"]; 

使用這樣的:

$name2=$_POST["name2"]; 

或者,如果你知道其中的差別和目的都這樣做,請確保您的$name2變量是正確的名稱定義HTML表單字段。

順便說一句,我強烈建議使用像PHPMailer這樣的庫來發送電子郵件。
你的例子很簡單,mail()應該工作得很好,但對於任何更精細的(即有附件或HTML)或需要通過SMTP(使用或不使用身份驗證)使用外部郵件服務器發送,它將做得更好,併爲您節省大量時間。

對於一個更好的主意,檢查this example從他們的網站:

require_once('../class.phpmailer.php'); 
$mail = new PHPMailer(true); // the true param means it will throw exceptions on  errors, which we need to catch 

$mail->IsSMTP(); // telling the class to use SMTP 

try { 
    $mail->Host  = "mail.yourdomain.com"; // SMTP server 
    $mail->SMTPDebug = 2;      // enables SMTP debug information (for testing) 
    $mail->SMTPAuth = true;     // enable SMTP authentication 
    $mail->Host  = "mail.yourdomain.com"; // sets the SMTP server 
    $mail->Port  = 26;     // set the SMTP port for the GMAIL server 
    $mail->Username = "[email protected]"; // SMTP account username 
    $mail->Password = "yourpassword";  // SMTP account password 
    $mail->AddReplyTo('[email protected]', 'First Last'); 
    $mail->AddAddress('[email protected]', 'John Doe'); 
    $mail->SetFrom('[email protected]', 'First Last'); 
    $mail->AddReplyTo('[email protected]', 'First Last'); 
    $mail->Subject = 'PHPMailer Test Subject via mail(), advanced'; 
    $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';  // optional - MsgHTML will create an alternate automatically 
    $mail->MsgHTML(file_get_contents('contents.html')); 
    $mail->AddAttachment('images/phpmailer.gif');  // attachment 
    $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment 
    $mail->Send(); 
    echo "Message Sent OK<p></p>\n"; 
} catch (phpmailerException $e) { 
    echo $e->errorMessage(); //Pretty error messages from PHPMailer 
} catch (Exception $e) { 
    echo $e->getMessage(); //Boring error messages from anything else! 
} 
1

錯誤是告訴你什麼是錯的:

第10行:

$name2=$_POST["$name2"]; 

您正在使用 '$名2' 是它被定義。

PHP可以在字符串替代變量:

$var1 = "bar"; 
echo "foo $var1"; // prints "foo bar" 

在你的HTML使用類似以下內容:在PHP

<input type="...whatever..." name="name2" /> 

然後,假設該數據發佈後,您會使用訪問:

$name2 = $_POST["name2"]; 
+0

這正是我所做的,我的$ name2 = $ _POST [「name2」];聲明和你所說的完全一樣。 – Atma 2009-11-14 22:39:40

+0

在這種情況下,您的HTML沒有任何名爲「name2」的字段,或者至少它沒有在您的表單中發佈。否則,POST超全球將有一個名稱2鍵,你會得到沒有錯誤。你的網頁是否存在?鏈接將有所幫助。 – 2009-11-14 22:45:07

+0

是啊,它在:http://dropcollective.com/beta/dropcollective/subpages/contact2.php – Atma 2009-11-14 22:56:17

0

看來,當你希望你的代碼沒有通過$ _ POST數據。我建議你檢查的$ _POST鍵如下:

<?php 
print_r(array_keys($_POST)); 
?> 

如果它不顯示的$名2的值,這意味着你應該修改網頁發送表單數據send_contact.php。

相關問題