How to send email from a contact form with PHP

Want to know how to send email from a PHP script? Need visitors to send feedback to your email?

You can use a PHP contact form.

In this tutorial you’ll learn how to create contact form with a php script that will send a simple text-only email.

For this tutorial I assume you have a working PHP installation and the mail() function is properly set up.

Copy the following code and name the file as email.php

Please review the following code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
if(isset($_POST['process']) and $_POST['process']==1) {
    $sendto = "myemail@mydomain.com";
    $subject = $_POST['subject'];
    $name = $_POST['name'];
    $message = 'Message from:'.$_POST['name'].'<br />'.$_POST['comments'];
    $mailheaders = 'From: My Website <myemail@mydomain.com>';

    if(!mail($sendto, $subject, $message)) {
        echo '<p style="color:#C00;">Error: Email could not be sent.</p>';
    }
    else {
        echo '<p style="color:#00F;">Message has been sent!</p>';
        exit();
    }
}

<strong>Contact us</strong>
<form name="contact" action="'.$_SERVER['PHP_SELF'].'" method="POST">
<input type="text" name="name" />
<input type="text" name="subject" />
<textarea name="comments">
</textarea>
<input type="hidden" name="process" value="1" />
</form>

The HTML Form

1
2
3
4
5
6
7
8
<form name="contact" action="<?php echo $_SERVER['PHP_SELF'].' ?>" method="POST">
<input type="text" name="name" />
<input type="text" name="subject" />
<textarea name="comments" rows="10" cols="50" >
</textarea>
<input type="hidden" name="process" value="1" />
<input type="submit" value="Send Message!"
</form>

action=”" tells the form to send the form results to itself using the method POST. You could have also inputted action=”email.php” which would be the same. I prefer using $_SERVER['PHP_SELF'] better because it doesn’t matter what we name the php file, it will always send the form results to itself.

We have 3 fields:

  • name: for the user to input his/her name
  • subject: for the user to input the email’s subject
  • comments: the body of the email

process: is a hidden fields that will send a value of 1 when the user clicks the Send Message! button. We use this hidden field to let the PHP code know that the user has clicked on the button and that it should process the code that sends the email

When the user clicks the Send Message! button, a $_POST array is created. $_POST is an array of variable names and values sent by the HTTP POST method. The $_POST variable is used to collect values from a form that uses the POST method.

We use $_POST['name'], $_POST['subject'], $_POST['comments'] and $_POST['process'] to collect the information the user entered.

The PHP code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
if(isset($_POST['process']) and $_POST['process']==1) {
    $sendto = "myemail@mydomain.com";
    $subject = $_POST['subject'];
    $name = $_POST['name'];
    $message = 'Message from:'.$_POST['name'].'<br />'.$_POST['comments'];
    $mailheaders = 'From: My Website <myemail@mydomain.com>';

    if(mail($sendto, $subject, $message, $mailheaders)) {
        echo '<p style="color:#00F;">Message has been sent!</p>';
    }
    else {
        echo '<p style="color:#C00;">Error: Email could not be sent.</p>';
    }
}

if(isset($_POST['process']) and $_POST['process']==1) allows us to process the code inside the if statement ONLY when the submit button has been clicked.

The mail() function takes 4 parameters: recepient, subject, message and additional mail headers. Those 4 parameters are setup as follows:

  • recepient: $sendto
  • subject: $subject
  • message: combine $_POST['name'] and $_POST['comments'] to compose the message
  • additional mail headers: $mailheaders contains the information that displayes in the From column of your email application

We need to have a way of letting the user know that his/her email was sent.

The mail() function returns TRUE if the email was successfully sent, FALSE otherwise.

In our code, the mail($sendto, $subject, $message, $mailheaders) expression returns a TRUE value and the if statement is executed letting the user know that the “Message has been sent!” in blue letters. If the expression returns a FALSE value (the email was not sent), the script echoes “Error: Email could not be sent” in red letters.

There is a lot more about sending emails with PHP. For more information about the mail PHP function read the php.net official documentation

Leave a Reply