Send form data by email

See more about:

Where are we?

You learned how to create forms, and use PHP to process form data. This lessons looks at how to email it.

This lesson’s goals

By the end of this lesson, you will learn:

  • How to use the <textarea> tag.
  • How to use PHP’s mail function to send an email message.

A contact form

In an earlier chapter, we created a template-based site with PHP. It included a contact form that looked like this:

Incomplete contact page

Figure 1. Incomplete contact page

Now we can finish it. This is what we want:

Complete contact page

Figure 2. Complete contact page

Here’s the code for the form.

<?php 
//Path from this page to the site root.
$path_to_root = '.';
//Title of this page.
$page_title = 'Contact';
require $path_to_root . '/library/start_page.inc';
require $path_to_root . '/library/header.inc';
require $path_to_root . '/library/left_nav.inc';
?>
<div id="center_region">
  <h1>Contact</h1>
  <p>Please complete the form and click Send.</p>
  <form action="send-contact-message.php" method="post">
    <p>
      Subject<br>
      <input type="text" name="subject" size="40">
    </p>
    <p>
      Comment<br>
      <textarea name="message" rows="5" cols="40"></textarea>
    </p>
    <p>
      <button type="submit">Send</button>
    </p>
  </form>
  <p><a href="index.php">Home</a></p>
</div>
<?php
require $path_to_root . '/library/footer.inc';
require $path_to_root . '/library/end_page.inc';
?>

Figure 3. Code for contact form

Lines 1 to 9 and 28 to 31 are the templating system at work. The form itself is the HTML from lines 13 to 35.

There’s a new form element on line 20: <textarea>. It creates the multi-line field in Figure 2. The rows attribute specifies the number of rows the field has. The cols attribute specifies the number of columns.

If we want the field to have contents when the page loads, we could add it inside the tag. For instance:

<textarea>(Type your message here)</textarea>

would show this when the page loads:

textarea with content

Figure 4. <textarea> with content

Emailing the message

send-contact-message.php (named in the action property of the form) will send the message. Here’s the code:

<h1>Contact</h1>
<?php
//Get message data.
$subject = $_POST['subject'];
$message = $_POST['message'];
//Email it.
mail(
  'email address',           //Where to send
  "Contact form - $subject", //Email subject
  $message                   //Message body
);
?>
<p>Thank you! Your message has been sent.</p>

Figure 5. send-contact-message.php

I’ve omitted the PHP that adds the header, nav bar, etc.

Lines 4 and 5 get the subject and the message. Nothing new there.

Lines 7 to 11 sends the email using the mail() function. We give the function three arguments:

  • The email address to send to. The usual form, e.g., you@yoursite.com.
  • The subject of the email.
  • The message.

You should replace the email address with your own, if you want to test the program.

A couple of things to notice. First, I split the function call over a few lines, to make it easier to read. I added comments at the end of each line, explaining the arguments.

Line 9 adds the text “Contact form – “ in front of the subject. This will help the recipient know where the message came from.

The mail() function will work only if your server is set up correctly. PHP has to know what program on the server to use to send email.

Most shared hosting servers will be set up correctly. However, your test server on your local machine (e.g., your XAMPP server) might have some trouble. That’s OK; it’s just for testing anyway.

You can try it.

Sending to Gmail

I’ve had trouble sending email to Gmail using the basic mail() function. Here’s the code I used in an application that worked:

$to_address = 'email address'; 
$subject = 'Card order';
$message = "Stuff stuff, thing thing";
$from = 'another email address';
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/plain; charset=iso-8859-2\n";
$headers .= "From: $from\n" . "Reply-To: $from\n" 
  . "X-Mailer: PHP/" . phpversion() . "\n";
mail($to_address, $subject, $message, $headers);

Figure 6. Sending email to Gmail

You may need to use this approach for some other services as well.

Summary

In this lesson, you learned:

  • How to use the <textarea> tag.
  • How to use PHP’s mail function to send an email message.

What now?

Let’s see how you can store form data to a file.


How to...

Lessons

User login

Log in problems? Try here


Dogs