How to send mail with attachment in php

It is quite easy to send an email with attachment in php. To send the attachment you must set the header to multipart as given in this code. But you don't worry about that. The code is given here. Please take a look. Thanks !!
If you are looking to send simple or html mail with php you can have a look at this post.

Php script to send email with attachment

 
 <?php
  if(isset($_REQUEST['submit'])){
   $to = "toemail@example.com";
   $subject = "This is the subject line";
   $message = "These are the messages <br />
               Place all your messages here";

   $hash   = md5(uniqid(time()));
   $header = "";
  
   $header .= "MIME-Version: 1.0\n";
   $header .= "Content-Type: multipart/mixed;boundary=\"".$hash."\"\n\n";
   $header .= "This is a multi-part message in MIME format.\n";
  
   $header .= "--".$hash."\n";
   $header .= "Content-type: text/html; charset=utf-8\n";
   $header .= "Content-Transfer-Encoding: 7bit\n\n";
   $header .= $message."\n\n";
 
 # File Attachment

 if($_FILES["resume"]["name"] != ""){
   $file_name = $_FILES["resume"]["name"];
   $cntnt = @file_get_contents($_FILES["resume"]["tmp_name"]);
   $file_contnt = chunk_split(base64_encode($cntnt)); 
   $header .= "--".$hash."\n";
   $header .= "Content-Type: application/octet-stream;name=\"".$file_name."\"\n"; 
   $header .= "Content-Transfer-Encoding: base64\n";
   $header .= "Content-Disposition: attachment;filename=\"".$file_name."\"\n\n";
   $header .= $file_contnt."\n\n";
  }

 $ok = @mail($to,$subject,$message,$header);

  if($ok){
   echo "Mail has been sent successfully";
    }else{
   echo "Some error occured. sorry !!";
   }
 }
 ?>

Html Form


  <form name="attach" method="post" enctype="multipart/form-data" >
     <input type="file" name="resume" id="file" /><br />
     <input name="submit" value="Send" type="submit" />
  </form>

How to send mail with attachment in php How to send mail with attachment in php Reviewed by Altaf Hussain on 6:44 PM Rating: 5

1 comment:

  1. my name is aftab khan plzz contact mee +92-3016841022

    ReplyDelete

Altaf Web. Powered by Blogger.