I’m creating a form that reads in the users name and email, then emails them a voucher via a PHP file, which looks like this:
<?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);
?>
The form:
<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>
This works fine, but doesn’t echo the variable $email in the received email? I’ve also tried using $_GET["email"], but again to no success.
Thanks in advance for your help.
You are trying to place an
echostatement inside a single quoted string. Concatenate it in:Or better yet, switch to a double-quoted string for
$message, in which$emailcan be interpolated.See the PHP manual on strings for more information on variable interpolation and the difference between single and double-quoting.
A third option if you have lots of variables and multiple lines is the HEREDOC syntax, which avoids quoting entirely.