How to send Email with Attachment

15/04/2017   04:16:03 PM

Hello, in this tutorial I will explain how to send email with attachment in php.

Email is very useful thing for every web project. Every web project needs an email system for sending information to the users. Many times we needs to send email with text or html content and attachment files. The mail() function doesn’t support attachment or HTML mail by default. You need to use different headers and MIME mail parts to make this possible. Please follow the short and easy tutorial given below.

1.Create index.php page and add the following code in it.

<form enctype="multipart/form-data" method="POST" action="send.php">

<label>Your Name <input type="text" name="sender_name" /> </label>

<label>Your Email <input type="email" name="sender_email" /> </label>

<label>Subject <input type="text" name="subject" /> </label>

<label>Message <textarea name="message"></textarea> </label>

<label>Attachment <input type="file" name="my_file" /></label>

<label><input type="submit" name="button" value="Submit" /></label>



The form will look like this: 

2.Add following code While defining the <form> tag.


This is to tell the browser that this form will be used to upload files.PHP makes use of mail() function to send an email. This function requires three mandatory arguments that specify the recipient's email address, the subject of the the message and the actual message additionally there are other two optional parameters.

mail( to, subject, message, headers, parameters);

3.Create send.php and add the following code in send.php page

if($_POST && isset($_FILES['my_file']))

    $from_email  = $_POST['sender_email'];  // sender email
    $to_email    = '';    //recipient email 
    //Capture POST data from HTML form and Sanitize them, 
    $sender_name    = filter_var($_POST["sender_name"], FILTER_SANITIZE_STRING); //sender name
    $reply_to_email = filter_var($_POST["sender_email"], FILTER_SANITIZE_STRING); //sender email used in "reply-to" header
    $subject        = filter_var($_POST["subject"], FILTER_SANITIZE_STRING); //get subject from HTML form
    $message        = filter_var($_POST["message"], FILTER_SANITIZE_STRING); //message
    //Get uploaded file data
    $file_tmp_name    = $_FILES['my_file']['tmp_name'];
    $file_name        = $_FILES['my_file']['name'];
    $file_size        = $_FILES['my_file']['size'];
    $file_type        = $_FILES['my_file']['type'];
    $file_error       = $_FILES['my_file']['error'];

    if($file_error > 0)
        die('Upload error or No files uploaded');
    //read from the uploaded file & base64_encode content for the mail
    $handle = fopen($file_tmp_name, "r");
    $content = fread($handle, $file_size);
    $encoded_content = chunk_split(base64_encode($content));

        $boundary = md5("sanwebe");
        $headers = "MIME-Version: 1.0\r\n"; 
        $headers .= "From:".$from_email."\r\n"; 
        $headers .= "Reply-To: ".$reply_to_email."" . "\r\n";
        $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n"; 
        //plain text 
        $body = "--$boundary\r\n";
        $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
        $body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
        $body .= chunk_split(base64_encode($message)); 
        $body .= "--$boundary\r\n";
        $body .="Content-Type: $file_type; name=".$file_name."\r\n";
        $body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
        $body .="Content-Transfer-Encoding: base64\r\n";
        $body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n"; 
        $body .= $encoded_content; 
    $sentMail = @mail($to_email, $subject, $body, $headers);
     die('Thank you for your email');
      die('Could not send mail! Please check your PHP mail configuration.');  




Support us by Sharing