PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: send mail
send mail - phponwebsites.com
Showing posts with label send mail. Show all posts

6 Aug 2014

Create contact form and send mail using PHP

                                  PHP allows you to create custom forms like contact form , registration form, login form. A websites should contains contact form for user contact them. You can create contact form and send user details to website owner using PHP mail. Let see create contact form using PHP.

1. Create contact form in HTML


                                 You need to create front end design for user view. This is the static page which is developed using HTML. It should be below HTML code.

<form action="#" method="post">
      <table id="tbl" align="center">
       <tr><td>Name:</td><td><input type="text" name="name" id="name"></td></tr>
       <tr><td>Phone:</td><td><input type="text" name="phone" id="phone"></td></tr>
  <tr><td>Email:</td><td><input type="text" name="mail" id="mail"></td></tr>
  <tr><td>Message:</td><td><textarea rows="5" cols="25" name="message" id="message"></textarea></td></tr>
       <tr><td></td><td><input type="submit" name="submit" id="submit" value="Submit"></td></tr>
      </table>

     </form>

2. Add CSS to HTML form


                             When you run above HTML file, it should not be attracted by user. So you need to add some CSS to that HTML file in order to make your contact form as attractive. Some of CSS styles are below.

 body {
 color:white;
 font-size:14px;
 }
 .contact {
    text-align:center;
    background: none repeat scroll 0% 0% #8FBF73;
    padding: 20px 10px;
    box-shadow: 1px 2px 1px #8FBF73;
    border-radius: 10px;
width:510px;
 }
 #name, #phone, #mail, #message {
    width: 250px;
    margin-bottom: 15px;
    background: none repeat scroll 0% 0% #AFCF9C;
    border: 1px solid #91B57C;
    height: 30px;
    color: #808080;
    border-radius: 8px;
    box-shadow: 1px 2px 3px;
}
#message {
 height:150px;
 width:300px;
}
#submit
{
    background:none repeat scroll 0% 0% #8FCB73;
    display: inline-block;
    padding: 5px 10px;
    line-height: 1.05em;
box-shadow: 1px 2px 3px #8FCB73;
    border-radius: 8px;
    border: 1px solid #8FCB73;
    text-decoration: none;
    opacity: 0.9;
    cursor: pointer;
color:white;
}
#er {
    color: #F00;
    text-align: center;
    margin: 10px 0px;
    font-size: 17px;

}


3. Add javascript for form validation


                                 Then you've to validate your form using javascript. You can validate empty textbox, validate valid email id and valid phone number using below javascript.

$(document).ready(function() {
$('#submit').click(function() {
var name=document.getElementById('name').value;
var phone=document.getElementById('phone').value;
var mail=document.getElementById('mail').value;
var message=document.getElementById('message').value;
var ph = /^([0-9-+]+)$/;
var chk = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
if(name=='')
{
 $('#er').html('Enter your name');
 return false;
}
if(phone=='')
{
 $('#er').html('Enter your phone number');
 return false;
}
if(!ph.test(phone) || phone.length!=10)
{
 $('#er').html('Enter valid phone number');
 return false;
}
if(mail=='')
{
 $('#er').html('Enter your mail');
 return false;
}
if(!chk.test(mail))
{
 $('#er').html('Enter valid email');
 return false;
}
if(message=='')
{
 $('#er').html('Enter your message');
 return false;
}
});

});

4. Send mail using PHP


                                   After designed HTML form, you need to get user details and send it to website owner using PHP mail(). Visit How to send mail using PHP. Let see below PHP script for send mail.

if(isset($_POST['submit']))
 {
  $name=$_POST['name'];
  $phone=$_POST['phone'];
  $mail=$_POST['mail'];
  $message=$_POST['message'];
  $to='guruparthiban19@gmail.com';
  $header='From: '.$mail;
  $subject=$name.' contacts you';
  $body="Name: ".$name."\n";
  $body.="Phone: ".$phone."\n";
  $body.="Email: ".$email."\n";
  $body="He/She says ".$message."\n";
  $m=mail($to,$subject,$body,$subject);
  if($m)
  {
   echo"<script>Message send successfully</script>";
  }
  else
  {
   $er="Message not sent";
  }
 }


5. PHP script for create contact form and end mail


                                      Combine above all script and make one PHP file with .php extension. This is the contact form of your website. It should be like below.

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<style type="text/css">
 body {
 color:white;
 font-size:14px;
 }
 .contact {
    text-align:center;
    background: none repeat scroll 0% 0% #8FBF73;
    padding: 20px 10px;
    box-shadow: 1px 2px 1px #8FBF73;
    border-radius: 10px;
width:510px;
 }
 #name, #phone, #mail, #message {
    width: 250px;
    margin-bottom: 15px;
    background: none repeat scroll 0% 0% #AFCF9C;
    border: 1px solid #91B57C;
    height: 30px;
    color: #808080;
    border-radius: 8px;
    box-shadow: 1px 2px 3px;
}
#message {
 height:150px;
 width:300px;
}
#submit
{
    background:none repeat scroll 0% 0% #8FCB73;
    display: inline-block;
    padding: 5px 10px;
    line-height: 1.05em;
box-shadow: 1px 2px 3px #8FCB73;
    border-radius: 8px;
    border: 1px solid #8FCB73;
    text-decoration: none;
    opacity: 0.9;
    cursor: pointer;
color:white;
}
#er {
    color: #F00;
    text-align: center;
    margin: 10px 0px;
    font-size: 17px;
}
</style>
</head>
<body>
<?php
 error_reporting('E_ALL ^ E_NOTICE');
 if(isset($_POST['submit']))
 {
  $name=$_POST['name'];
  $phone=$_POST['phone'];
  $mail=$_POST['mail'];
  $message=$_POST['message'];
  $to='guruparthiban19@gmail.com';
  $header='From: '.$mail;
  $subject=$name.' contacts you';
  $body="Name: ".$name."\n";
  $body.="Phone: ".$phone."\n";
  $body.="Email: ".$email."\n";
  $body="He/She says ".$message."\n";
  $m=mail($to,$subject,$body,$subject);
  if($m)
  {
   echo"<script>Message send successfully</script>";
  }
  else
  {
   $er="Message not sent";
  }
 }
?>
<div class="contact">
<h1>Contact Us</h1>
     <div id="er"><?php echo $er; ?></div>
     <form action="#" method="post">
      <table id="tbl" align="center">
       <tr><td>Name:</td><td><input type="text" name="name" id="name"></td></tr>
       <tr><td>Phone:</td><td><input type="text" name="phone" id="phone"></td></tr>
  <tr><td>Email:</td><td><input type="text" name="mail" id="mail"></td></tr>
  <tr><td>Message:</td><td><textarea rows="5" cols="25" name="message" id="message"></textarea></td></tr>
       <tr><td></td><td><input type="submit" name="submit" id="submit" value="Submit"></td></tr>
      </table>
     </form>
</div>

<script type="text/javascript">
$(document).ready(function() {
$('#submit').click(function() {
var name=document.getElementById('name').value;
var phone=document.getElementById('phone').value;
var mail=document.getElementById('mail').value;
var message=document.getElementById('message').value;
var ph = /^([0-9-+]+)$/;
var chk = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
if(name=='')
{
 $('#er').html('Enter your name');
 return false;
}
if(phone=='')
{
 $('#er').html('Enter your phone number');
 return false;
}
if(!ph.test(phone) || phone.length!=10)
{
 $('#er').html('Enter valid phone number');
 return false;
}
if(mail=='')
{
 $('#er').html('Enter your mail');
 return false;
}
if(!chk.test(mail))
{
 $('#er').html('Enter valid email');
 return false;
}
if(message=='')
{
 $('#er').html('Enter your message');
 return false;
}
});
});
</script>
</body>

</html>


               When you run this file in your browser, you'll get below output on screen


Create contact form using php


                                     Now you can add this contact form to your website and make conversion with your clients.

13 Jul 2014

Send forgotten password to mail in login form using php and mysql

                       Already you know about validate login form with remember me and signout concepts using php. You want to know Login with remember me and signout using php and mysql
                       Now see about how to send forgotten password to mail using php?. First your table contains users mail id. Just imagine this is your table:


Mysql login table

Login form using php and mysql:

         
          Consider the following example:
login.php:

<html>
<head>
<style type="text/css">
 input{
 border:1px solid olive;
 border-radius:5px;
 }
 h1{
  color:darkgreen;
  font-size:22px;
  text-align:center;
 }
span{
  color:lightgreen;

 }
</style>
</head>
<body>
<h1>Login<h1>
<form action='#' method='post'>
<table cellspacing='5' align='center'>
<tr><td>User name:</td><td><input type='text' name='name'/></td></tr>
<tr><td>Password:</td><td><input type='password' name='pwd'/></td></tr>
<tr><td></td><td><input type='checkbox' name='remember' /> <span>Remember me</span></td></tr>
<tr><td></td><td> <span><a href="forgot.php">Forgot password</a></span></td></tr>
<tr><td></td><td><input type='submit' name='submit' value='Submit'/></td></tr>
</table>

</form>
<?php
session_start();
//your values are stored in cookies, then you can login without validate
if(isset($_COOKIE['name']) && isset($_COOKIE['pwd']))
{
    header('location:welcome.php');
}
// login validation in php
if(isset($_POST['submit']))
{
 mysql_connect('localhost','root','') or die(mysql_error());
 mysql_select_db('new')  or die(mysql_error());
 $name=$_POST['name'];
 $pwd=$_POST['pwd'];
 if($name!=''&&$pwd!='')
 {
   $query=mysql_query("select * from login where name='".$name."' and password='".$pwd."' ")  or die(mysql_error());
   $res=mysql_fetch_row($query);
   if($res)
   {
    if(isset($_POST['remember']))
{
  setcookie('name',$name, time() + (60*60*24*1));
  setcookie('pwd',$pwd, time() + (60*60*24*1));
}
    $_SESSION['name']=$name;
    header('location:welcome.php');
   }
   else
   {
    echo'You entered username or password is incorrect';
   }
 }
 else
 {
  echo'Enter both username and password';
 }
}
?>
</body>
</html>


             While you run the above file, you'll get output like this:


Login form using php

Fogot password in php:


                 The php script for send forgotten password to mail is:
forgot.php:


<html>
<head>
<style type="text/css">
 input{
 border:1px solid olive;
 border-radius:5px;
 }
 h1{
  color:darkgreen;
  font-size:22px;
  text-align:center;
 }

</style>
</head>
<body>
<h1>Forgot Password<h1>
<form action='#' method='post'>
<table cellspacing='5' align='center'>
<tr><td>Email id:</td><td><input type='text' name='mail'/></td></tr>
<tr><td></td><td><input type='submit' name='submit' value='Submit'/></td></tr>
</table>
</form>
<?php
if(isset($_POST['submit']))

 mysql_connect('localhost','root','') or die(mysql_error());
 mysql_select_db('new') or die(mysql_error());
 $mail=$_POST['mail'];
 $q=mysql_query("select * from login where mail='".$mail."' ") or die(mysql_error());
 $p=mysql_affected_rows();
 if($p!=0) 
 {
  $res=mysql_fetch_array($q);
  $to=$res['mail'];
  $subject='Remind password';
  $message='Your password : '.$res['password']; 
  $headers='From:guruparthiban19@gmail.com';
  $m=mail($to,$subject,$message,$headers);
  if($m)
  {
    echo'Check your inbox in mail';
  }
  else
  {
   echo'mail is not send';
  }
 }
 else
 {
  echo'You entered mail id is not present';
 }
}
?>
</body>
</html>

         Where,
                 mysql_query("select * from login where mail='".$mail."' "); is select the values from mysql table.
                 mysql_affected_rows() return the number of rows selected. If it returns 0, then there is no values is present in mysql table.
                 mysql_fetch_array() return the results an associated array. You want to know mysql_fetch_array().
                  Mail() is used to send password to mail. Yow want to know mail() in php.

              When you click the 'forgot password' in login page, you'll get output like this:

Forgot password in php

                You entered your email id in text box and click submit button. Now you can get message 'Check your inbox in mail' if the mail is send. Otherwise, you'll get 'Mail is not send' message. If you enetered mail id which is not present is mysql table, then you will get 'You entered mail is is not present' message.

11 Jun 2014

Send mail using php

                       You can send emails using php.

Mail() in php:


                       The mail() in php is used to send mail. The syndex for mail() is:

                    mail(to, subject, message, header)

Where,
            to - receiver mail id.
            subject - subject of mail.
            message - message to be sent.
            header - header of mail.

Note:
           You can send mail if you are in server. You've to make some changes in php.ini file, if you send mail from local server.

          You've to make following changes in your php.ini file to send mail.
                     [mail function]
                     ; For Win32 only.
                     ; http://php.net/smtp
                       SMTP =your ISP's mail server address
                     ; http://php.net/smtp-port
                       smtp_port = 25


                     ; For Win32 only.
                     ; http://php.net/sendmail-from
                       sendmail_from =example@gmail.com

How to send mail using php:


                      The following php script is used for send mail:

<?php
   $to='example@gmail.com';         
   $subject='Send mail using php';
   $message='This mail send using php';
   $headers='From: guruparthiban19@gmail.com';
   $mail=mail($to,$subject,$message,$headers);
   if($mail)
   {
    echo'Mail send successfully';
   }
   else
   {
    echo'Mail is not send';
   }
 ?>

                      If the the mail is send, then you'll get successful message. Otherwise you'll get 'Mail is not send' message. Now you can see mail in inbox.