How we can send attachment with E-mail using php?

This article based on how to attach file in E-mail form using php. The mail attachment function is used form send some text email message and  attachment file. We can send computer file using email attachment. Some  headers and Multipurpose Internet Mail Extensions(MIME) mail  parts are used for successfully submit the email with attachment. Now a days email is most commonly used. We used mail() function to send the attachment. Firstly we generate  the HTML form.
 HTML form is used  to collect the user Information. Form elements are different types of input elements, such as Name , Email and File.
 
In the  attachment form three  fields are  name,email and file. In the first field fill your name, Second field  your email address and then  third field you can click on browse button to select the file on your computer. In the form tag used "enctype="multipart/form-data" to upload the file. when you fill all the field correctly click on submit button attachment_mail.php page will be open. you can see the  Mail successfully submit on this screen message.Your form detail is successfully submitted and goes to receiver.


Example:
attachment_form.html

<html>
 <body>
  <form method="post" action="attachment_mail.php" enctype="multipart/form-data">
   <table align="center">
      <tr>
         <td>Name</td>
            <td><input type="text" name="name"></td>
        </tr>
       <tr>
          <td>Email</td>
             <td><input type="email" name="email"></td>
        </tr>
        <tr>
          <td><input type="file" name="photo"></td>
         </tr>

        <tr>
   <td colspan="2">
           <center>
            <input type="submit" name="submit" value="Submit">
           </center>
          </td>
 </tr>
 </form>
 </body>
</html>

attachment_mail.php
<?php
if(isset($_POST['submit']))
{
  //Following  code we are used to Upload the file on the server.Name,type, size are attributes for upload file.
  
  $photo = $_FILES['photo']['name'];
  $type = $_FILES['photo']['type'];
  $size = $_FILES['photo']['size'];
  $tmppath = $_FILES['photo']['tmp_name'];
   
  //CHECK THE FILE IS UPLOADED OR NOT 
  //EMPTY FUNCTION CHECK THE ARRAY IS EMPTY OR NOT 
  if(!empty($photo))
  {
    //image is a folder in which you will save image
    move_uploaded_file($tmppath,'image/'.$photo);
    //Send the E-mail script
    $your_message = $_POST['name'];
    $to = "abc@gmail.com"; //Recipient Email Address
    $subject = "Send E-mail with Attachment"; //Subject of Email
    $headers = "From: " . $_POST['email']. "  \r\nReply-To: abc@example.com";
    $random_hash = md5(date('r', time()));
    $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
    $attachment = chunk_split(base64_encode(file_get_contents('image/'.$photo))); // Set your file path here
    $message = "--PHP-mixed-$random_hash\r\n"."Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"\r\n\r\n";
    $message .= "--PHP-alt-$random_hash\r\n"."Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"."Content-Transfer-Encoding: 7bit\r\n\r\n";

    //Insert the html message.
    $message .= $your_message;
    $message .="\r\n\r\n--PHP-alt-$random_hash--\r\n\r\n";
    //include attachment code
    $message .= "--PHP-mixed-$random_hash\r\n"."Content-Type: application/zip; name=\"photo.png\"\r\n"."Content-Transfer-Encoding: base64\r\n"."Content-Disposition: attachment\r\n\r\n";
    $message .= $attachment;
    $message .= "/r/n--PHP-mixed-$random_hash--";
    //send the email script
    $mail = mail( $to, $subject , $message, $headers );
    echo $mail ? "Mail successfully submitted" : "Error occurred";
 }
}
?>

In the attachment_mail.php page firstly we upload the file on server code. In this code we declare some attributes are file name (name of the file), file type (determine the file type .jpg, png etc.), file size(check the size of the file in kb,mb etc.). After the attributes  empty function used for check the file is not empty. If the file is not empty we declare a file folder path which you save your  file on the  server. Then mail function is used to send your information and file by email attachment script. Some Parameters are used in mail function (such as $to,$subject,$message,$header). To Parameter is used for recipient email address. Subject parameter is specifies the subject of your  email. Headers parameter are specifies optional headers, like From, Cc, and Bcc.


Students Tech Life