Simple Form to Email PHP Contact Form

PHP Script

<?php

// Define some constants
define( "RECIPIENT_NAME", "NAME" );
define( "RECIPIENT_EMAIL", "admin@website.com.au" );

// Read the form values
$success = false;
$name = isset( $_POST['name'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['name'] ) : "";
$senderEmail = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : "";
$phone = isset( $_POST['phone'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['phone'] ) : "";
$services = isset( $_POST['services'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['services'] ) : "";
$subject = isset( $_POST['subject'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['subject'] ) : "";
$website = isset( $_POST['website'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['website'] ) : "";
$message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";

$mail_subject = 'A contact request send by ' . $name;

$body = 'Name: '. $name . "\r\n";
$body .= 'Email: '. $senderEmail . "\r\n";


if ($phone) {$body .= 'Phone: '. $phone . "\r\n"; }
if ($services) {$body .= 'services: '. $services . "\r\n"; }
if ($subject) {$body .= 'Subject: '. $subject . "\r\n"; }
if ($website) {$body .= 'Website: '. $website . "\r\n"; }

$body .= 'message: ' . "\r\n" . $message;

// If all values exist, send the email
if ( $name && $senderEmail && $message ) {
 $recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
 $headers = "From: " . $name . " <" . $senderEmail . ">"; 
 $success = mail( $recipient, $mail_subject, $body, $headers );
 echo "

Thanks for contacting us. We will contact you ASAP!

<!-- /.inner -->"; }else { echo "

Something went wrong. Please try again.

<!-- /.inner -->"; } ?>

Basic website form HTML

Below is the HTML form sample. As we mentioned above, you can edit the style of this to match your websites design.


<form action="inc/sendemail.php" class="contact-form">
 
<!-- /.form-grp --> </div><!-- /.col-md-6 -->
<!-- /.form-grp --> </div><!-- /.col-md-6 -->
<!-- /.form-grp --> </div><!-- /.col-md-6 -->
<!-- /.form-grp --> </div><!-- /.col-md-6 -->
<!-- /.form-grp --> <button type="submit" class="thm-btn">Submit Now</button> </div><!-- /.col-md-6 --> </div><!-- /.row -->
<!-- /.form-result --> </form>

 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s