HTML Form Data to Email Via PHP

First Method


<?php

if(isset($_POST['email'])) {

$email_to = "info@trusof.com";

$email_subject = "Summarized propose of the email";

//Errors to show if there is a problem in form fields.

function died($error) {

echo "We are sorry that we can procceed your request due to error(s)";

echo "Below is the error(s) list <br /><br />";

echo $error."<br /><br />";

echo "Please go back and fix these errors.<br /><br />";

die();

}

// validation expected data exists

if(!isset($_POST['first_name']) ||

!isset($_POST['last_name']) ||

!isset($_POST['email']) ||

!isset($_POST['telephone']) ||

!isset($_POST['comments'])) {

died('We are sorry to proceed your request due to error within form entries');

}

$first_name = $_POST['first_name']; // required

$last_name = $_POST['last_name']; // required

$email_from = $_POST['email']; // required

$telephone = $_POST['telephone']; // not required

$comments = $_POST['comments']; // required

$error_message = "";

$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

if(!preg_match($email_exp,$email_from)) {

$error_message .= 'You entered an invalid email<br />';

}

$string_exp = "/^[A-Za-z .'-]+$/";

if(!preg_match($string_exp,$first_name)) {

$error_message .= 'Invalid first name<br />';

}

if(!preg_match($string_exp,$last_name)) {

$error_message .= 'Invalid Last name<br />';

}

if(strlen($comments) < 2) {

$error_message .= 'Invalid comments<br />';

}

if(strlen($error_message) > 0) {

died($error_message);

}

$email_message = "Form details below.\n\n";

function clean_string($string) {

$bad = array("content-type","bcc:","to:","cc:","href");

return str_replace($bad,"",$string);

}

$email_message .= "Name:".clean_string($first_name)."\n";

$email_message .= "Last Name:".clean_string($last_name)."\n";

$email_message .= "EMAIL:".clean_string($email_from)."\n";

$email_message .= "Mobile No:".clean_string($telephone)."\n";

$email_message .= "Massage:".clean_string($comments)."\n";

// create email headers

$headers = 'From: '.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);

?>

<!-- include your own success html here -->

<br><br>

<h4 style="color:green;">Thank you for contacting us. We will be in touch with you very soon.</h4>

<?php

}

?>

Second method



<!----above form----->


<?php

if(!empty($_POST["send"])) {

$userName = $_POST["userName"];

$userEmail = $_POST["userEmail"];

$userPhone = $_POST["userPhone"];

$userMessage = $_POST["userMessage"];

$toEmail = "websolution90@gmail.com";

$mailHeaders = "Name: " . $userName .

"\r\n Email: ". $userEmail .

"\r\n Phone: ". $userPhone .

"\r\n Message: " . $userMessage . "\r\n";


if(mail($toEmail, $userName, $mailHeaders)) {

$message = "Your contact information is received successfully.";

}

}

?>


<!----above form----->




<!----below form----->


<?php if (! empty($message)) {?>

<div class='success'>

<strong><?php echo $message; ?> </strong>

</div>

<?php } ?>

<!----below form----->









Third Method


<?php

global $_POST;

$mail_to = 'yourpersonalemail@gmail.com'; //Your email here


// Required fields

$email = isset( $_POST['email'] ) ? strip_tags( trim( $_POST['email'] ) ) : '';

$name = isset( $_POST['name'] ) ? strip_tags( trim( $_POST['name'] ) ) : '';

$text = isset( $_POST['message'] ) ? strip_tags( trim( $_POST['message'] ) ) : '';

// Additional fields

$subject = isset( $_POST['subject'] ) ? strip_tags( trim( $_POST['subject'] ) ) : '';

$permalink = isset( $_POST['permalink'] ) ? strip_tags( trim( $_POST['permalink'] ) ) : '';

$phone = isset( $_POST['phone'] ) ? strip_tags( trim( $_POST['phone'] ) ) : '';

$company = isset( $_POST['company'] ) ? strip_tags( trim( $_POST['company'] ) ) : '';



$mail_subject = $subject != '' ? $subject : 'From Contact form on website';


$message = '<h3>You got a mail from website:</h3>' . '<br/>';

$message .= '<b>Name:</b> ' . $name . '<br/>';

$message .= '<b>Email:</b> ' . $email . '<br/>';


if ( ! empty( $permalink ) ) {

$message .= '<b>Website:</b> ' . $permalink . '<br/>';

}

if ( ! empty( $phone ) ) {

$message .= '<b>Phone:</b> ' . $phone . '<br/>';

}

if ( ! empty( $company ) ) {

$message .= '<b>Company:</b> ' . $company . '<br/>';

}


$message .= '<b>Message:</b> ' . $text . '<br/>';

$headers = 'MIME-Version: 1.0' . '\r\n';

$headers .= 'Content-type:text/html;charset=UTF-8' . '\r\n';


// More headers

$headers .= 'From: "'.$name.'"<' . $email . '>' . '\r\n';


mail( $mail_to, $mail_subject, $message, $headers );