我正在创建一个表单,读取用户名和电子邮件,然后通过PHP文件向他们发送代金券,该文件如下所示:
<?php
$deal = "offer or discount details";
$to = "$email";
$subject = "Tonights wheree voucher - $deal";
$message = '
<html>
<body>
Test email sent to <?php echo $email; ?>.
</body>
</html>
';
$headers = "From: wheree <no-reply@wheree.co.uk>" . "'r'n" .
"Content-type: text/html" . "'r'n";
mail($to, $subject, $message, $headers);
?>
的形式:
<form class="cmxform" id="details-form" method="get" action="test.php">
<fieldset>
<p>
<input id="name" name="name" title="Please enter your full name. " size="18" class="tagcheck" minlength="5" maxlength="60" value="Name" onfocus="if(this.value==this.defaultValue){this.value=''};" onblur="if(this.value==''){this.value=this.defaultValue};" />
</p>
<p>
<input id="email" name="email" size="18" title="Please enter a valid email address. " class="required email" value="Email" onfocus="if(this.value==this.defaultValue){this.value=''};" onblur="if(this.value==''){this.value=this.defaultValue};" />
</p>
<p class="tickbox-wrapper">
<input id="accept-tick" type="checkbox" name="vehicle" class="required" title="You must accept our Terms & Conditions. " /> I accept the voucher usage <a href="/voucher-terms-and-conditions" alt="terms" class="terms-link">Terms & Conditions</a>.
</p>
<div id="RegisterErrors" style="display:none"></div>
<p>
<input class="submit" type="submit" value="Continue"/>
</p>
</fieldset>
</form>
这工作得很好,但不回变量$email
在收到的电子邮件?我也试过使用$_GET["email"]
,但再次没有成功。
提前感谢您的帮助。
您正在尝试将echo
语句放在单个引号字符串中。将其连接在:
$message = '
<html>
<body>
Test email sent to ' . $email . '.
</body>
</html>
';
或者更好的是,为$message
切换为双引号字符串,其中可以插入$email
。
$message = "
<html>
<body>
Test email sent to $email.
</body>
</html>
";
关于变量插值和单引号与双引号的区别的更多信息,请参阅PHP字符串手册。
如果你有很多变量和多行,第三个选择是HEREDOC语法,它可以避免完全引用。
$message =<<<MESSAGE
<html>
<body>
Test email sent to $email.
</body>
</html>
MESSAGE;
// Note that the MESSAGE; above MUST be at the start of its own line, with no indentation
您有$echo
在单引号内。在PHP中,变量不在单引号中展开。试着把它放在双引号里。或者,如建议的那样,如果想使用单引号,请将其连接在。
$foo = "HELLO";
echo 'Foo: $foo'; // Prints 'Foo: $foo'
echo 'Foo: ' . $foo; // Prints 'Foo: HELLO'
echo "Foo: $foo"; // Prints 'Foo: HELLO'
试试这个:
<?php
$deal = "offer or discount details";
$to = $_GET['email'];
$subject = "Tonights wheree voucher - $deal";
$message = '
<html>
<body>
Test email sent to '.$to.'.
</body>
</html>
';
$headers = "From: wheree <no-reply@wheree.co.uk>" . "'r'n" .
"Content-type: text/html" . "'r'n";
mail($to, $subject, $message, $headers);
?>