PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: mail function
mail function - phponwebsites.com
Showing posts with label mail function. 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.

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.