Send PHP mail with Attachment

You probably know how to send email with PHP, it’s just few lines of code, but it gets bit tricky when you want to send an attachment with PHP email. So, today let’s find-out how we can send email with an attachment using PHP mail.

Create a HTML form with file input field similar to below, enctype attribute should be “multipart/form-data“, so that form-data is encoded as “multipart/form-data” when sent to server.

1
2
3
4
5
6
7
8
<form enctype="multipart/form-data" method="POST" action="">
    <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>
</form>

PHP mail with Attachment

Here’s the complete PHP code that sends PHP email with attachment. Just create a PHP file with code below and point your HTML form to the file, and don’t forget to change recipient address and email address in the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
if($_POST && isset($_FILES['my_file']))
{

    $from_email         = '[email protected]_domain.com'; //from mail, it is mandatory with some hosts
    $recipient_email    = '[email protected]_domain.com'; //recipient email (most cases it is your personal 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
   
    /* //don't forget to validate empty fields
    if(strlen($sender_name)<1){
        die('Name is too short or empty!');
    }
    */

   
    //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);
    fclose($handle);
    $encoded_content = chunk_split(base64_encode($content));

        $boundary = md5("sanwebe");
        //header
        $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));
       
        //attachment
        $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($recipient_email, $subject, $body, $headers);
    if($sentMail) //output success or failure messages
    {      
        die('Thank you for your email');
    }else{
        die('Could not send mail! Please check your PHP mail configuration.');  
    }

}

If you want bit advance Ajax version please check here.

That’s it, you can capture subject and message body from HTML form and use them in the code. I hope this code will be useful in your PHP projects. Good luck!

97 Comments Add Comment