PHP Contact form with smtp authentication




Contact forms are the easiest way to let the visitor contact you, without revealing your email address on the web page, which would surely make your email be added in spam lists.

Contact forms can be built with various scripting languages, but the most popular contact forms are PHP contact forms. They are very flexible and can be linked with databases but PHP is not very easy to learn, especially by beginners in web development. We will show you a short tutorial on how to create a simple PHP contact form.

The following PHP contact form is based on PHP OOP, Javascript and AJAX method. Please copy and paste it.
Click here to view this form: Dristikonn

HTML PART:

<div class="contact-form-bottom">
<div class="col-md-4 address">
<address>
</address>
</div>
<div class="col-md-4 contact-form">
<h3 id="help"></h3>
<form>
<div class="contact-form-row">
<div>
<span>Name</span>
<input type="text" class="text" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = '';}" name="uname" id="uname">
</div>
<div>
<span>Email</span>
<input type="text" class="text" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = '';}" name="uuemail" id="uuemail">
</div>
<div class="clearfix"> </div>
</div>
</form>
</div>
<div class="col-md-4 contact-form-row ccomments">
<div>
<span>Enter text</span>
<textarea onfocus="this.value = '';" onblur="if (this.value == '') {this.value = '';}" name="utext" id="utext"></textarea>
</div>
<input type="submit" value="Submit" onclick="help()" />
</div>
<div class="clearfix"></div>
</div>



PHP PART:

You need class.phpmailer.php and class.smtp.php for using this contact form. Because it is totally based on SSL/TLS method by Google.
You also need dbConnect.php and owner_class.php for using PHP OOP concept.

dbConnect.php:


<?php

error_reporting(0);

sleep(0.3);

ini_set('session.cookie_httponly',true);



session_start();



if(isset($_SESSION['last_ip']) === false){

$_SESSION['last_ip'] = $_SERVER['REMOTE_ADDR'];

}
if($_SESSION['last_ip'] !== $_SERVER['REMOTE_ADDR']){
session_unset();
session_destroy();
}


$DB_host = "localhost";
$DB_user = "root";
$DB_pass = "your password";
$DB_name = "your database name";


try{
$DB_con = new PDO("mysql:host={$DB_host};dbname={$DB_name}",$DB_user,$DB_pass);
$DB_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
echo $e->getMessage();
}
include_once 'owner_class.php';
$owner = new OWNERUSER($DB_con);

owner_class.php:

<?php
error_reporting(0);
sleep(0.3);

class OWNERUSER{
private $db;
function __construct($DB_con){
$this->db = $DB_con;
}
public static function generate(){
return $_SESSION['token']= trim(base64_encode(openssl_random_pseudo_bytes(32)));
}


public function is_loggedin()//public function for session checking

{
if(isset($_SESSION['user_session'])){
return true;
}






}




help.php

<?php
error_reporting(0);
sleep(0.3);
include_once 'dbConnect.php';
require 'class.phpmailer.php';

$uname = trim(htmlspecialchars($_POST['uname'],ENT_QUOTES|ENT_HTML401));
$uuemail = trim(htmlspecialchars($_POST['uuemail'],ENT_QUOTES|ENT_HTML401));
$utext = trim(htmlspecialchars($_POST['utext'],ENT_QUOTES|ENT_HTML401));

$mainmail = "your mail where you want to see the sent mails";
$mainname = "your name";

if($uname === ''){
echo 'Name is empty. ';
}else if($uuemail === ''){
echo 'Email is empty. ';
}else if(!filter_var($uuemail, FILTER_VALIDATE_EMAIL)){
echo 'This is not an email format';
}else if($utext === ''){
echo 'Body is empty';
}
else{


$mail = new PHPMailer;
$mail->isSMTP();

$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;  

$mail->Username = "your gmail address";
$mail->Password = "your gmail password";   

$mail->SMTPSecure = "tls";                            
$mail->Port = 587;

$mail->From = $uuemail;
$mail->FromName = $uname;

$mail->addAddress($mainmail,$mainname);
$mail->isHTML(true);

$mail->Subject = "Query for user";
$mail->Body = "<i>".$uname."(".$uuemail.")"." says</i> ".$utext;
$mail->AltBody = "Thank you";

if(!$mail->send()) {
   echo "Please try again";
}else{
echo 'Query has been sent successfully';
}
}



JAVASCRIPT PART:
You need jquery plugin for running this function.

function help(){



var uname=document.getElementById("uname").value;

var uuemail=document.getElementById("uuemail").value;

var utext=document.getElementById("utext").value;



dataString="uname="+uname+"&uuemail="+uuemail+"&utext="+utext;

$.ajax({

type:"POST",

url:"help.php",
data:dataString,
cache:false,
success:function(e){
$("#help").show().html(e);
}
});
}


PHP Contact form with smtp authentication PHP Contact form with smtp authentication Reviewed by Unknown on Sunday, October 23, 2016 Rating: 5

No comments:

Powered by Blogger.