PHP Pear - 为$ body按钮添加变量

时间:2011-09-23 11:52:41

标签: php email pear

这是我的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文件本身中。我原本以为这很容易,但这已经让我困扰了一天多了。很沮丧。我找不到任何能告诉你怎么做的例子,只有少数人问过这个确切的问题并没有得到明确的答案。

2 个答案:

答案 0 :(得分:1)

在您发布的代码中,您尝试在第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;
...

答案 1 :(得分:0)

您还没有提到您保存PHP代码文件的位置?是联系人文件夹吗?并且还要关注Mail.php的文件位置。此文件必须位于联系人文件夹下。