PHP Pear -向$body消息添加变量


PHP Pear - adding variables to $body message

这是我的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文件本身中设置它们。我原以为这很简单,但这已经难倒我一天了。非常令人沮丧。我找不到任何例子来告诉你如何做,只有少数人问过这个确切的问题,并且没有得到明确的答案。

在您发布的代码中,您试图在第13行设置$body的值,但直到第22行才设置$name的值。当您将$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;
...

你没有提到你在哪里保存你的php代码文件?是联系人文件夹吗?并注意Mail.php的文件位置。这个文件必须在联系人文件夹下