Thursday, March 13, 2014

Email Contact PHP Source Codes

Hello PHP Lovers,
Today, we are going to talk about php mail function.  It's something by which we can send email to anybody from a html format. For this project we are going need two steps.

Step 1: Creating a HTML form which contains fields, NAME, EMAIL, PHONE and MESSAGE writing infields.
Step 2: Putting my provided PHP codes into it.



I'm not explaining everything details as I'm guessing that you are expert in HTML/CSS and you will find what to do by seeing the source codes.



<?php
if(isset($_POST['Email'])) {
   
    // Replace my email address in which you want to receive emails from customer
    $email_to = "rubel007cse[at]gmail.com";
    $email_subject = "Customer Email";
   
   
    function died($error) {
echo "Error somewhere!";
        die();
    }
   
    if(!isset($_POST['Name']) ||
        !isset($_POST['Email']) ||
        !isset($_POST['Phone']) ||
        !isset($_POST['Message'])) {
        died('fill up all the html fields');    
    }
   
    $first_name = $_POST['Name'];
    $email_from = $_POST['Email'];
    $telephone = $_POST['Phone'];
    $comments = $_POST['Message'];
   
    $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 .= 'Incorrect Email Address mate!<br />';
  }

  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Mail from Customer.\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 .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Phone: ".clean_string($telephone)."\n";
    $email_message .= "Message: ".clean_string($comments)."\n";
 

$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);

echo "Thank You for using php mail function";
}
?>


That will work for you.
PHP is something what I always love to me with. Wait for my next article and don't forget to subscribe with me if you are loving these and want to have more exciting php souce codes!

Thank You


Get Free HTML templates at CarnivalBD IT

No comments:

Post a Comment