2011-09-23 89 views
0

已解決 - 謝謝George Cummins!

我到處搜索,發現沒有解決方案。我對PHP很新,但我無法理解爲什麼這不起作用。

所以,這是我的HTML表單,它位於我的網站上的一個小框中。

<form name="contactform" action="contact/form-mailer.php" method="post"> 



<p>*Name: <input name="Name" id="Name" size="25"></p> 



<p>*Pickup from: <input name="pfrom" id="pform" size="25"></p> 



<p>*Destination: <input name="destin" id="destin" size="25"></p> 



<p>*E-mail: <input name="Email" id="Email" size="25"></p> 



<p>*Phone: <input name="Phone" id="Phone" size="25"></p> 

<p><input type="submit" value="Submit" name="submitform"><input type="reset" value="Reset" name="reset"></p> 

</form> 

這是我使用PEAR發送郵件的當前PHP。我還沒有將所有變量添加到$ body中,因爲我剛剛試圖讓它實際工作。

<?php 

    // Include the Mail package 
    //require "Mail.php"; 
    include 'Mail.php'; 


    // Identify the sender, recipient, mail subject, and body 
    $sender = "XXXXXX"; 
    $recipient = "XXXXXX"; 
    $subject = "FORM"; 
    $body  = $name; // Even when seting $name in the PHP file it still doesn't show! 

    // Identify the mail server, username, password, and port 
    $server = "ssl://smtp.gmail.com"; 
    $username = "XXXXXXXX"; 
    $password = "XXXXXXXX"; 
    $port  = "465"; 

    // Get form var's 
    $name = "Test"; // I set $name to "Test" in the PHP and still the variable does not appear in the email. 
    $destin = $_POST['destin']; 
    $email = $_POST['Email']; 
    $pfrom = $_POST['pfrom']; 

    // Set up the mail headers 
    $headers = array(
     "From" => $sender, 
     "To"  => $recipient, 
     "Subject" => $subject 
    ); 

    // Configure the mailer mechanism 
    $smtp = Mail::factory("smtp", 
     array(
     "host"  => $server, 
     "username" => $username, 
     "password" => $password, 
     "auth"  => true, 
     "port"  => 465 
    ) 
    ); 

    // Send the message 
    $mail = $smtp->send($recipient, $headers, $body); 

    if (PEAR::isError($mail)) { 
     echo ($mail->getMessage()); 
    } 

?> 

所以我不知道爲什麼這不起作用。我需要身體是這樣

$body = $name . " wants to go to " . $destin . " from " . $pfrom; 

但它只是沒有任何皮卡變量,不管我是否試圖從HTML表單讓他們或設置它們在PHP文件本身。我期待這很容易,但是這一直困擾着我一整天。非常令人沮喪。在任何地方我都找不到任何示例,告訴你如何去做,只有少數人問過這個確切的問題,但沒有得到明確的答案。希望我已經提供了足夠的細節!

謝謝。

+0

我沒有看到你在你的代碼中的任何地方分配'$ body'變量。除此之外,使用'var_debug()'來查看像'$ _POST'數組這樣的變量。 – Rijk

+0

靠近頂端:「$ body = $ name;」 - 我會嘗試var_debug() – Recy

回答

1

在您發佈的代碼,你嘗試設置的$body上線13值,但沒有設置的$name的值,直到行22 $name是你把它分配給$body時間空,所以$body不包含您希望的值。

要解決此問題,請將$body = ...行移動到其他分配下方。類似這樣的:

... 
// Get form var's 
$name = "Test"; // I set $name to "Test" in the PHP and still the variable does not appear in the email. 
$destin = $_POST['destin']; 
$email = $_POST['Email']; 
$pfrom = $_POST['pfrom']; 

// Populate $body 
$body = $name . " wants to go to " . $destin . " from " . $pfrom; 
... 
+0

哦,甜蜜的耶穌。我覺得自己像個傻瓜。這樣一個簡單的錯誤。非常感謝xD – Recy

0

你沒有提到你的php代碼文件保存在哪裏?它是否聯繫文件夾?並且還照看Mail.php的文件位置。該文件必須位於聯繫人文件夾下。