Saturday, April 19, 2014

Send attachment in mail using php

Lets take a look about how to send attachment in mail using PHP

It is very important to send attachment in mail.



Functionality file:
1.Index.html
2.mail.php


HTML Code

<table cellspacing="0" cellpadding="0" border="0">
      <tbody>
        <tr>
          <td height="20" class="contacttxttitle" style="color:#990000; width:300px; font-family:Arial, Helvetica, sans-serif; font-size:16px;  padding:5px;"  colspan="2"> Enquiry Form </td>
        </tr>
      <form name="contactusform" action="mail.php" method="post" enctype="multipart/form-data">
        <tr>
          <td width="179" height="30" class="contactspl">Name :</td>
          <td width="271" height="30"><input type="text" size="30" id="uname_1" name="uname" required>
          </td>
        </tr>
        <tr>
          <td height="30" class="contactspl">Contact No :</td>
          <td height="30"><input type="tel" size="30" pattern="\d{10}" id="contact_1" name="contact" required>
          </td>
        </tr>
        <tr>
          <td height="30" class="contactspl">Email :</td>
          <td height="30"><input type="email" size="30" id="email_1" name="email" required>
          </td>
        </tr>
        <tr>
          <td height="30" class="contactspl">Message :</td>
          <td height="30"><textarea rows="4" cols="23" id="comment_1" name="comment" required></textarea>
          </td>
        </tr>
        <tr>
          <td height="30" class="contactspl">Resume :</td>
          <td height="30"><input type="file" name="fileAttach" required />
          </td>
        </tr>
        <tr>
          <td height="30" align="center" colspan="2"><input type="submit" value="Submit" id="submit" name="submit">
          </td>
        </tr>
      </form>
      </tbody>
   
    </table>

PHPCode

<?php if(isset($_POST['submit']))
{
$strTo ="test@gmail.com";//Replace with your mail
$strSubject ='Resume';
$cname = nl2br($_POST["uname"]);
$email = nl2br($_POST["email"]);
$contact = nl2br($_POST["contact"]);
$comment = nl2br($_POST["comment"]);
//*** Uniqid Session ***//
$strSid = md5(uniqid(time()));
$strHeader = "";
$strHeader .= "From:
".$_POST["cname"]."<".$_POST["email"].">\nReply-To: ".$_POST["email"]."";
$strHeader .= "MIME-Version: 1.0\n";
$strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";
$strHeader .= "This is a multi-part message in MIME format.\n";
$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-type: text/html; charset=utf-8\n";
$strHeader .= "Content-Transfer-Encoding: 7bit\n\n";
$strHeader .= "Name:".$cname."\n\n".'<br>';
$strHeader .= "Email Id:".$email ."\n\n".'<br>';
$strHeader .= "Contact Number:".$contact ."\n\n".'<br>';
$strHeader .= "Message:".$comment ."\n\n";


//*** Attachment ***//
if($_FILES["fileAttach"]["name"] != "")
{
$strFilesName = $_FILES["fileAttach"]["name"];
$strContent =
chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));
$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";
$strHeader .= "Content-Transfer-Encoding: base64\n";
$strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";
$strHeader .= $strContent."\n\n";
}

$flgSend = @mail($strTo,$strSubject,null,$strHeader);  // @ = No Show Error //
if($flgSend)
{
echo "Mail sent successfully";
}
else
{
echo "Error.Please try again";
}
}
?>

No comments:

Post a Comment