PHP Pear - 将变量添加到$ body消息(已解决) [英] PHP Pear - adding variables to $body message (resolved)
问题描述
已解决 - 谢谢George Cummins!
Resolved - Thank you George Cummins!
我已经搜索到任何地方,发现没有解决方案。我对PHP很新,但我根本无法理解为什么这不起作用。
I've searched everywhere and found no solution to this. I'm pretty new to PHP, but I just can't fathom why this isn't working.
所以,这是我的HTML表单,这里面有一点
So, 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>
这是我目前使用PEAR发送邮件的PHP。我还没有将所有变量添加到$ body中,因为我刚刚试图让它实际工作。
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 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;
但是它不会收到任何变量,无论我是否试图让他们从HTML表单或将其设置在PHP文件本身。我期待这是非常容易的,但是现在已经让我忍受了一天。很沮丧我在任何地方都可以找到任何示例,告诉你如何做到这一点,只有少数人问这个确切的问题,并没有得到确定的答案。希望我给了足够的细节!
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. Hopefully I've given enough detail!
谢谢。
推荐答案
在您发布的代码中,您尝试在第13行设置 $ body
的值,但未设置 $ name
直到第22行。 $ name
在您将其分配给 $ body
时为null $ body
不包含您希望的值。
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.
要解决此问题,请移动 $ body = ...
在其他作业下方。这样做:
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;
...
这篇关于PHP Pear - 将变量添加到$ body消息(已解决)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!