This code will help to mail files as attachment from the server folder. just you have to put address of file in server to the code and done... this is very helpful in sending automatic mail sending with doc, images or anything.

Send mail with file in server folder in php



<?php 

//get data from form  

$form_subject= "this is subject of the mail";

$form_message= "this is message body of the mail";

//fixed recipient details

$mail_to = "sendtothisemail@abc.com";

$mail_from = "fromxyg@xyz.com";

$mail_subject =$form_subject; 

   

# Open a file  

  $file = fopen("contact.txt", "r" );//change your file location  

  if( $file == false )  

  {  

     echo "Error in opening file";  

     exit();  

  }  

  # Read the file into a variable  

  $size = filesize("contact.txt");  

  $content = fread( $file, $size); 

  

  # encode the data for safe transit  

  # and insert after every 76 chars.  

  $encoded_content = chunk_split( base64_encode($content));  

    

  # Get a random 32 bit number using time() as seed.  

  $num = md5( time() );

  

  

  # Define the main headers.  

  $header = "From:xyz@example.com ";  

  $header .= "MIME-Version: 1.0 ";  

  $header .= "Content-Type: multipart/mixed; ";  

  $header .= "boundary=".$num." ";  

 

  //START- Encoding attachment for mail

  # Define the message section

  $message = "--".$num." ";  

  $message .= "Content-Type: text/plain ";  

  $message .= "Content-Transfer-Encoding:8bit ";  

  $message .= $message." ";  

  $message .= "--".$num." ";  

  

  # Define the attachment section  

  $message .= "Content-Type:  multipart/mixed; ";  

  $message .= "name="test.txt" ";  

  $message .= "Content-Transfer-Encoding:base64 ";  

  $message .= "Content-Disposition:attachment; ";  

  $message .= "filename="test.txt" ";  

  $message .= $encoded_content." ";  

  $message .= "--".$num."--";  

   //START- Encoding attachment for mail

 



  # Send email now  

  $result = mail($mail_to, $mail_subject, $message, $header);  

  if( $result == true ){  

      echo "Message sent successfully...";

     // header("Location:contact.php"); uncomment to go back to other page

   }else{  

      echo "Sorry, unable to send mail...";  

   }