I got some help with an email form, and I feel that I am almost there as the script sends an email but need a few tweaks before I put the form up. Here is my code right now:
index.html:
<div id="main">
<form method="post" action="mailer.php">
<div id="text">
Please enter your email address.
</div>
<input type="text" name="q" id="search" />
<input type="submit" name="submit" id="submit" value="Go!" />
</form>
</div>
mailer.php:
<?php
$email = addcslashes($_REQUEST['q']) ;
if ($email==FALSE){
echo "You forgot to enter your email";
}
else
mail( "example@gmail.com", "E-Mail entered",
"E-Mail entered: $email");
header( "Location: http://www.example.com/thankyou.html" );
?>
A few issues I am running into:
The email being sent does not actually include the email entered, the email comes from Apache@ipaddress.ec2.internal and the Body is Email Entered:
which does not include the email string – is there something buggy with the code?
Also, my if statement doesn’t seem to work. Even if I leave the box black, it still assumes a valid email address was sent.
Finally, is there a parameter that sees if the address is in the correct format? ie: includes the @ the . and the domain?
Many thanks for any help!
First off, you can use
filter_var($email, FILTER_VALIDATE_EMAIL)to test the submitted address. This function returnsfalseif it’s not valid. Second,mail()requires a 4th parameter to assign a return address in your message header. Here’s an example:Regarding your if/else statement, test
$_POST['q'] == NULLfirst, then change$email = addcslashes($_REQUEST['q']);to$email = str_replace(array('\'', '"'), '', $_POST['q']);– no real reason to escape characters in this case. Just take em’ out.Edit: This is what your code should look like:
Does that make a little more sense?
Do note that because of modern spam filters, this method may not make it to every recipient. Creating useful email headers can be a bit of an art-form that takes some practice.