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


<div id="lessoncup">
<table width="75%" border="0" align="center" cellpadding="0" cellspacing="0" class="chart">
    <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>

    <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> <span>Mobile:</span><br/>
              <input name="mobile" type="text" id="mobile" class="inp_box">
            <li> <span>Message:</span><br/>
              <textarea name="message" cols="" id="message" rows="" class="inp_box" style="height:100px; width:200px;"></textarea>
            <li style="margin:0; padding-top:0;">
              <input name="send" type="button" value="Send" class="send">



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


  var name=$('#name').val();
  var mob=$('#mobile').val();
  var mess=$('#message').val();
   $('.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>');
   var datasend='name='+name+'&mess='+mess+'&mobile='+mob;
   $("#loader").fadeIn(400).html('<img src="loader.gif" align="absmiddle">&nbsp;<span class="loading">Sending Mail...</span>');
   $('.send').val('Sending mail..');


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
include "class.phpmailer.php";
include "class.smtp.php";

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

$headers = "From:\r\nContent-type: text/html";

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



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
$msg = stripslashes($msg);

$mail = new PHPMailer();
 $mail->IsSMTP();              // set mailer to use SMTP
 $mail->Host = "";  // specify main and backup server
 $mail->SMTPAuth = true;     // turn on SMTP authentication
 $mail->Username = "";  // SMTP username
 $mail->Password = "yourpassword"; // SMTP password
  $mail->From     = "";
  $mail->FromName = $name;
  $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";

echo "Thank you to contact us!";


Live Demo   Download Script


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

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

  3. Warning: fsockopen() [function.fsockopen]: unable to connect to (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!

  4. Hmm!! This blog is really cool, I’m so lucky that I have reached here and got this awesome information.
    brand design agency SF


Mohammad Khasim
Blogger | Web Developer
Hyderabad - INDIA

Web Development Tools | CMS | Frameworks | SEO

Get my Latest Lessons