PHP Email Script | Lessoncup Programming Blog

Tuesday, 10 December 2013

PHP Email Script

Hi, today i am going to tell you about how to send an email using PHP from localhost or Server, i have seen in many groups people are asking to send an email from localhost without upload in any server, we can do this by using SMTP ( Simple Mail Transfer Protocol ) Class file and PHPMailer Class file, these two files help you to send an email from WAMP / XAMP without upload your file in server just you need take SERVER and SMTP authentication. let's see how will do this


HTML Form

<div id="lessoncup">
<table width="75%" border="0" align="center" cellpadding="0" cellspacing="0" class="chart">
  <tr>
    <td width="3%" height="50" align="center" valign="middle" bgcolor="#666666" class="chart">&nbsp;</td>
    <td height="50" align="left" valign="middle" bgcolor="#666666" class="head">Feedback Form</td>
    </tr>

  <tr>
    <td height="50" align="center" valign="middle" class="chart">&nbsp;</td>
    <td height="50" align="left" valign="middle" class="chart">
   
          <div class="error"></div>
          <div id="loader"></div>
          <ul class="ul">
            <li> <span>Name:</span><br/>
              <input name="name" type="text" id="name" class="inp_box">
            </li>
            <li> <span>Mobile:</span><br/>
              <input name="mobile" type="text" id="mobile" class="inp_box">
            </li>
            <li> <span>Message:</span><br/>
              <textarea name="message" cols="" id="message" rows="" class="inp_box" style="height:100px; width:200px;"></textarea>
            </li>
            <li style="margin:0; padding-top:0;">
              <input name="send" type="button" value="Send" class="send">
            </li>
          </ul>
        
    </td>

  </tr>
  
</table>
</div>

JAVASCRIPT

<script type="text/javascript" src="jquery-1.10.2.min.js"></script>

<script>
  
  $(document).ready(function(){
   
   $('.send').click(function(){

  var name=$('#name').val();
  var mob=$('#mobile').val();
  var mess=$('#message').val();
  
  if(name==""){
   
   $('.error').fadeIn(400).html('<b>Enter Your Name..</b>');
   
  }else if(mob==""){
   
   $('.error').fadeIn(400).html('<b>Enter Your Mobile Number..</b>');
   
  }else if(!Number(mob)){
   
   $('.error').fadeIn(400).html('<b>Mobile number should be numbers</b>');
   
  }else if(mess==""){
   
   $('.error').fadeIn(400).html('<b>Enter Your Message..</b>');
   
  }else{
   
   var datasend='name='+name+'&mess='+mess+'&mobile='+mob;
   
   $('.error').hide();
   
   $("#loader").fadeIn(400).html('<img src="loader.gif" align="absmiddle">&nbsp;<span class="loading">Sending Mail...</span>');
   
   $('.send').css({'cursor':'wait'});
   $('.send').val('Sending mail..');
   
   $.ajax({
    
    type:'POST',
    data:datasend,
    url:'mailsend.php',
    cache:false,
    success:function(msg){
     
     $('.error').fadeIn("slow").addClass('success').html(msg);
     $('#name').val('');
     $('#mobile').val('');
     $('#message').val(''); 
     $('#loader').hide();
     $('.send').css({'cursor':'wait'});
     $('.send').val('Send');
     
    }
   
   
   });
   
  }
 });
   
   
  });
  
</script>

PHP

mailsend.php file in the bellow script include the phpmailer and smtp class files, and design the Email format Template with Subject, Headers and Body Message inside the message you can design any kind of email template, when the user received an email the formatted message display in template view and it increase the value of your service or brand.

after complete the template design it's time to config the SMTP Server with our content
<?php
extract($_REQUEST);
include "class.phpmailer.php";
include "class.smtp.php";

$sub = "Your Website Name - Contact Message from Website visitor";

$headers = "From: info@youdomain.com\r\nContent-type: text/html";

 $msg = "<html>
 <head>
   
 </head>
 <body>
 <table width=800 border=0 align=center cellpadding=0 cellspacing=0>
  <tr>
    <td align=left valign=top>&nbsp;</td>
  </tr>
  <tr>
   <td align=left valign=top>
<table width=800 border=0 cellpadding=0 cellspacing=0 bgcolor=#FFFFFF class=email_box>
      <tr>
        <td height=40 align=left valign=middle class=email_bg bg pad small_blak_heads1>Welcome</td>
      </tr>
      <tr>
        <td height=200 align=left valign=top class=pad><table width=70% border=0 cellspacing=0 cellpadding=0>
          <tr>
            <td height=30 colspan=3 valign=top class=small_blak_heads1>&nbsp;</td>
          </tr>
          <tr>
            <td height=30 colspan=3 valign=top class=small_blak_heads1>Contacted Person Name :&nbsp; <b>Mr/Mrs ".$name." </b>&nbsp;</td>
            </tr>
   <tr>
            <td height=30 colspan=3 valign=top class=small_blak_heads1>Mobile :&nbsp; <b>Mr/Mrs ".$mobile." </b>&nbsp;</td>
            </tr>
          <tr>
            <td width=7%>&nbsp;</td>
            <td width=17% height=30 align=right class=small_blak_heads1>Message :&nbsp;</td>
            <td width=76% height=30 class=pad><b>".$mess."</b></td>
          </tr>
   
        </table></td>
  </tr>
 
</table>


</body>     
</html>";

?>

SMTP Authentication

mailsend.php file added the bellow code is tell you how to config the SMTP, first we need provide Host Name, this is must because the emails will send via this host, next turn on the SMTPAuth to TRUE, and now provide a secured SMTP username / password, next add the email address in this $mail->AddAddress(); if you want to send to registered user just pass the email variable else give a static email address
<?php
$msg = stripslashes($msg);

$mail = new PHPMailer();
 $mail->IsSMTP();              // set mailer to use SMTP
 $mail->Host = "www.yourdomain.com";  // specify main and backup server
 $mail->SMTPAuth = true;     // turn on SMTP authentication
 $mail->Username = "info@yourdomain.com";  // SMTP username
 $mail->Password = "yourpassword"; // SMTP password
 
  $mail->From     = "info@yourdomain.com";
  $mail->FromName = $name;
  $mail->AddAddress("xyz@gmail.com");
  $mail->AddAddress("xyz1@gmail.com");
  $mail->IsHTML(true);                 // set email format to HTML
  $mail->Subject = $sub;
  $mail->Body = $msg;
  $mail->AltBody = "This is the body in plain text for non-HTML mail clients";
 
  $mail->Send();
  $mail->Body;

echo "Thank you to contact us!";

?>

Live Demo   Download Script

4 comments :

  1. Hi,
    very nice tutorial easy to understand thanks khasim

    ReplyDelete
  2. Thank you very much! know that you are helping me a lot with your tutorials. Alex from in brazil

    ReplyDelete
  3. Warning: fsockopen() [function.fsockopen]: unable to connect to www.gmail.com:25 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ) in C:\xampp\htdocs\mail\class.smtp.php on line 83
    Thank you to contact us!

    ReplyDelete

Mohammad Khasim
Blogger | Web Developer
Hyderabad - INDIA
lessoncup@gmail.com

PHP | My SQL | JQUERY
Web Development Tools | CMS | Frameworks | SEO

Get my Latest Lessons