PHP Pear - adding variables to $body message
This is my HTML form, which is inside a little box on my site.
<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>
And this is my current PHP for sending the mail using PEAR. I haven't added all of the variables into the $body yet, as I've just been trying to get it to actually work.
<?php
// Include the开发者_StackOverflow中文版 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());
}
?>
So I have no idea why this isn't working. I need body to be like this
$body = $name . " wants to go to " . $destin . " from " . $pfrom;
But it just doesn't pickup any variables, regardless of whether I am trying to get them from the HTML form or set them in the PHP file itself. I was expecting this to be really easy, but this has stumped me for over a day now. Very frustrating. I can find no example anywhere that shows you how to do it, and only a handful have asked this exact question and received no definitive answer.
In the code you posted, you attempted to set the value of $body
on line 13, but did not set the value of $name
until line 22. $name
is null at the time you assign it to $body
, so $body
does not contain the values you are hoping for.
To fix this problem, move the $body = ...
line below the other assignments. Something like this:
...
// 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;
...
You have not mention where you have kept your php code file? Is it contact folder? and also look after the file location of Mail.php. This file must be under contact folder.
精彩评论