How do I create a PHP form mailer?
Print Email
Email this article
Close
Email this article
CloseYou are here: Home > Website Support > PHP
The HTML and PHP code below consist of a simple, self-contained email form.
Read over the code first, then read the explanation that follows:
// Your E-mail Address
$MYEMAIL = "<a href="mailto:you@example.com">you@example.com</a>";
if ($_GET['ACTION'] == "send-mail") undefined else undefined
?>
</p><form action="?ACTION=send-mail" method="post">
Your E-mail: <input name="EMAIL">
Subject: <input name="SUBJECT">
Message:<textarea name="MESSAGE" rows="5" cols="50"></textarea>
<input value="Submit Query" type="submit"> *
<input value="Reset" type="reset">
</form>
The first time the form is called, the variable $_GET[‘ACTION’] has no value. This causes the script to output “Hello! Use this form to send me mail!”, after which it exits the PHP code block and the rest of the HTML code is outputted as normal, making the mail form.
When the form is filled out and submitted, the page will be called again, but this time all of the form variables and the values the user gave will be available to PHP. By setting $_GET[‘ACTION’] to “send-mail” using a form action argument, the condition of the if statement is made true.
The mail function is used to send their message to you via email (see the PHP documentation link below for information on mail), and the message “Thanks for sending me a mail!” is printed.
The rest of the HTML then loads again as normal.
External Resources:
The PHP website: http://www.php.net/
DevShed’s PHP Area: http://www.devshed.com/c/b/PHP
Was this information helpful?
Yes NoThank you for your feedback
We are delighted to find that our article resolved your query.
Thank you for your feedback
We will resolve your query as soon as possible.
Please take a few moments to comment on your unresolved query. Simply tell us what your problem is. We guarantee that we'll get back to you within two hours (during office hours) in response to your query.
