February 4, 2014 11:21 am

How to send email with attachment in PHP

I have received requests from my readers to write an article on send email with attachment, so in this tutorial I am going to show you how to send email with attachment in PHP. We have already write a tutorial on send email with SMTP and PHP Mailer. In this tutorial we didn’t go to re-invent the complete code simply add attachment and proceed.

How to send email with attachment in PHP

Also Read: Send Email with SMTP and PHP Mailer

I have used SMTP email using phpmailer (A full-featured email creation and transfer class for PHP) library.

PHP Code:

This is the simple code to send email to any one without attachment no you have to just add a single line and send this email with attachment.

File attachment:

Just using this line of code before $mail->Send() statement it will attach that file and send it. if you want to attach multiple files then add separate attachment method for each file.

If you want more option in attachment like name, MIME and file type use it like this.


To upload a file you can use our file upload tutorial and send that file in attachment.

I hope it helps please do share a with your friends circles and give us your feedback so we can improve our upcoming tutorials.

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.

Tutorial Categories:
  • jhgj


  • Andy Chan

    i encounter a problem that sending email with attachment via smtp and phpmailer.
    but could not attach any files . The message shown by submitting “Cannot assess the file”
    Could you specify more in statement PHP ” $mail->AddAttachment($path,$name,$encoding,$type); ” ?

    $path refer to name= attachment01 , means to link $_FILES[attachment01]

    Get confused about $path and $name , how i should set in this case correctly .
    i would be grateful.

    • Simply send $path of file like this: $mail->AddAttachment(“images/logo.png”); // $path: is your file path which you want to attach like $path = “images/logo.png”;

      • Andy Chan

        Is not local path. Receive a alert “cannot access the files.” when user fill out the form with attachment , automatically sent to a designated email through smtp . but doesn’t include files.