How to e-mail a HTML form with PHP

HTML forms are easy to make, but sending a form as an e-mail is a lot harder. There are many ways to do this, but I only review one in this article. HTML combined with a little PHP scripting.

First of all

Make sure that your host supports PHP. You can ask your host or make a file named phpinfo.php containing only this:
<?php
phpinfo();
?>

Put that file on your server. If you see something like this your host has PHP support.

The HTML part

Now we have to make the HTML form. I expect that most of you will know how to make so I will not talk about it thoroughly. For example, we will use this simple HTML form:

<form action="mail.php"
method="post">
First name:
<input type="text" name="firstname">
<br>
Last name:
<input type="text" name="lastname">
<input type="submit" value="Submit">
</form>

Pay attention to the action-attribute and the method-attribute. The action determines to which file the data must sended and the method determines how it must be send; either post or get (in the url, like index.php?lang=en&page=home). We will choose for post, as you don’t won’t passwords or other private information to show up in the url. An live example of this form can be found here.

The PHP part

As you can see, the HTML form refers to a file name mail.php. Now we have to make this file. Here is the whole script:
<?php
// Name and E-mail of the Recipient
$name_recipient = "Onyx";
$email_recipient = "info@onyx-design.net";

// The name and email of the sender
$name_sender = $_POST['lastname'];
$email_sender = "noreply@onyx-design.net";

// The subject of the email
$subject = "How to send a mail form by PHP";

// the headers of the email (you can also add a CC or BCC this way)
$headers = "From: ".$name_sender." <".$email_sender.">\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "Reply-To: ".$name_sender." <".$email_sender.">\r\n";

// the message
$message = "
-------------------------------------
How to send a mail form with PHP
-------------------------------------
This form was sent to you by:

First Name:".$_POST['firstname']."
Last Name:".$_POST['lastname']."


";
// Ad <br> to the message
$bericht = nl2br($message);

// Send the mail
mail($email_recipient, $subject, $message, $headers);

?>

$_POST['...']

The data which is sent by the HTML form can be recovered by PHP via the $_POST. If you look back to the HTML code, you see that the <input> has a name-attribute, for example: name=lastname. You can retrieve the data of that field with this PHP-code: $_POST['lastname']. That is what I did in this script.

nl2br()

This function inserts HTML-line breaks into the message. The function is XHTML valid (<br /> instead of <br>).

mail()

The mail()-function has four parameters: to, subject, message, headers. More information about the mail()-function can be found here.

Done!

Now you now how to make a mail-form. If you have anything to ask feel free to place a comment.


  • BROWSE / IN PHP

COMMENTS (4) / ADD YOUR OWN COMMENT

Really great thanks

By almog on February 19th, 2008 at 9:30 pm

Thanks for the guide to making the PHP side of the form…

One thing, just want to add a thank you page once it has been submit?

Thanks

By Michael on February 26th, 2009 at 8:36 am

@Michael, just put the HTML code that you want to have on the thank you page in the “mail.php” file, after the PHP script ;)

By Fabian on February 26th, 2009 at 5:27 pm

Hi thank you so much for the really helpful tutorial. You really really help me out. Just one quick question. Is it possible to add an upload image into the form?

Thank you
Ciaran

By Ciaran on September 21st, 2009 at 8:28 pm

SPEAK / ADD YOUR COMMENT
Comments are moderated.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Return to TopTo top