PHP Email Form

Submitted by: 
Language: 
Visitors have accessed this post 5053 times.

Introduction:
This tutorial will cover creating an email form with PHP/HTMl.

Pre-creation:
Before you do this, please note that some errors may be caused by your php.ini file located on your server (online or local) during the actual mail sending process (if they do not arrive, for example).

Steps of Creation:
Step 1:
First we are going to create our HTML form for the user to interact with. We will take three fields; Name, Their email, Message.

  1. <html>
  2. <head></head>
  3. <body>
  4. <form action='mail.php' method='POST'>
  5. <input type='text' name='nam' />
  6. <input type='text' name='email' />
  7. <input type='text' name='message' />
  8. <input type='submit' value='Send' name='sub' />
  9. </form>
  10. </body>
  11. </html>

Please note; the action is mail.php because that is what the file containing my email form is called (this avoids having to create multiple, un-needed files).

Step 2:
Next let's go to PHP and check if the form has been submitted (if the submit button is set).

  1. <?php
  2. if (isSet($_POST['sub'])) {
  3.  
  4. }
  5. ?>

Step 3:
Now we will set our local variables of the form values. Then we will check if all of the values are set; if they're not we'll echo an error.

  1. $name = $_POST['nam'];
  2. $email = $_POST['email'];
  3. $message = $_POST['message'];
  4. if ($name == '' || $email == '' || $message == '') {
  5. echo 'Not all the values have been set. Please try again.';
  6. }else{
  7.  
  8. }

Step 4:
Finally, we will use the mail function to submit the email with the given form values.

  1. $msg = 'NAME: '.$name.', EMAIL: '.$email.', MESSAGE: '.$message;
  2. $m = mail('<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>', 'New Email Contribution!', $msg);
  3. if ($m){
  4. echo 'Mail sent!';
  5. }else{
  6. echo 'Mail error!';
  7. }

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.