Fred Brack   Fred Brack  
Raleigh, NC
Picture of the Cape Hatteras lighthouse

Using PHP to Send Mail from an HTML Form
by Fred Brack
Updated

This page documents how to use PHP to send an email to someone (typically yourself) when a visitor to your website fills in fields on a form and presses Submit.  Learning how to do this was my motivation for exploring PHP, because this is considered the best way to send mail from a web page for several reasons.

If there is any question whether or not PHP runs on your web server, then:  Test your own system for PHP support by clicking here.

You may also view my Introduction to PHP to learn or review the basics of using PHP.

Note that any page which uses PHP is usually expected to have a filetype of .php not .html.  The methodology shown here for sending mail only requires the page which will actually send the mail to have PHP in it, not the page which has the form, so this should not be a problem in adding a form to an existing page on your website.

PHP Mail

You can use PHP to initiate an email to yourself or anyone else, with or without a copy list or blind copy list.  Typically this would be done after a user fills out a form with requested data.  For example, you might wish to use a form for user requests for information from you, to assure yourself of gathering required data.  PHP includes a built-in function called mail to do this.  The format for this function follows:

   mail(to,subject,message,headers,parameters);

where:

  to = the email address of the recipient
  subject = the subject line of the email
  message = the contents of the email in HTML format including (for example) line breaks
  headers = from, cc, and bcc
  parameters = additional parameters to be passed to the SENDMAIL program; not discussed here

Example:

  <?php
  $to      = "me@mydomain.com";
  $subject = "Comments on Your Web Page";
  $message = "filled in based on input form";
  $from    = "filled in as discussed below";
  $headers = "From:" . $from;
  mail($to,$subject,$message,$headers);
  ?>

The $from value could be the actual email address of the form submitter (assuming you requested it on the form), or you could use a dummy email address such as "NOREPLY@mydomain.com."  The advantage of using the submitter's address would be the ease of REPLYing.  You would build the message from input form values passed to the PHP routine as discussed later.  You can read more about the mail function at w3schools.

The HTML Form

While not intended to be a full discussion of how to create an HTML form (via the <form> tag), we will give examples of relevant portions and show how to link the form to your PHP routine.  You can learn or review how to create the various elements of an HTML form at the w3schools HTML Forms page.

Here is a very simple example of an HTML form to allow a user to send you a message.  First, here's what the form looks like on your web page, then the HTML for it, followed by discussion.

The Form on the Web Page

Please submit your comments below.  Fields marked with an asterisk (*) are required.





(BTW, the form really does work to send me comments ...)

The Form HTML

  <p><strong>Please submit your comments below.&nbsp;
  Fields marked with an asterisk (*) are required.</strong></p>
  <form action="sendemail.php" method="post" style="font-family: 'Courier New', Courier, monospace">
    <label for="name">Your Name:&nbsp;&nbsp;</label>
      <input name="name" type="text" size="50"><br>
    <label for="email">Your Email*:</label>
      <input name="email" type="text" size="50" required><br>
    <label for="message">Your Message*:</label><br>
      <textarea name="message" cols="100" rows="5" required></textarea><br>
      <input name="submit" type="submit" value="Submit Comments">
  </form>

Discussion of the Form

The PHP Page to Send the Email

So, now we have three named fields (name, email, and message) being submitted to page sendemail.php.  If that statement is not 100% clear, please review the above HTML form before proceeding.

Let's talk now about the PHP processing of the form's fields.  Remember, the page with the PHP may optionally include regular HTML (in which case the page must start with the <html> tag, etc.), or it can consist of nothing but the PHP code.  We are only going to address the PHP coding here.

Variables passed to PHP via the POST method of the form are retrieved in this manner:

  $email = $_POST['email'];

Read that statement this way:  Set the arbitrarily named PHP variable $email (remember, all PHP variables begin with a dollar-sign) to the variable named email in the HTML form passed by the POST method.  You repeat this for each variable in the form (two more in our case), setting them to whatever names you want in your PHP code.  Figure out what you are going to do for the email's from address, and issue the PHP mail function accordingly.  Here is a simplified version of PHP code to accomplish this (with a reminder that a period is the concatenation indicator, and note the imbedded HTML):

  <?php
  $name    = $_POST['name'];
  $email   = $_POST['email']; // see below to validate address
  $msg     = $_POST['message'];
  $body    = $email . "(" . $name . ") wrote:<br>" . $msg;
  $to      = "me@mydomain.com";
  $subject = "Comments on Your Web Page";
  $headers = "From:" . $email;
  mail($to,$subject,$body,$headers);
  echo "<p>Thank you - your message has been sent. We will contact you shortly.</p>";
  echo "<p>Please click the Back Arrow or press Backspace to return to the previous page.</p>";
  ?>

There are some additional considerations ...

Resources

In Conclusion ...

Relatively speaking, I am a PHP amateur, and I welcome your feedback using the link below or the form on this page!

Fred