PHP Pear-向$ body消息中添加变量 [英] PHP Pear - adding variables to $body message
问题描述
这是我的HTML表单,位于我网站上的一个小框中。
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.
推荐答案
在发布的代码中,您尝试在第13行上设置 $ body
的值,但未设置 $的值名称
直到第22行。 $ name
在将其分配给 $ body
,因此 $ 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屋!