March 7, 2024 5:02 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

[wpdm_file id=73]

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:

<?php
    require_once('PHPMailer_v5.1/class.phpmailer.php'); //library added in download source.
    $msg  = 'Hello World';
    $subj = 'test mail message';
    $to   = '[email protected]';
    $from = '[email protected]';
    $name = 'My Name';

    echo smtpmailer($to,$from, $name ,$subj, $msg);

    function smtpmailer($to, $from, $from_name = 'Example.com', $subject, $body, $is_gmail = true)
    {
        global $error;
        $mail = new PHPMailer();
        $mail->IsSMTP();
        $mail->SMTPAuth = true; 

        $mail->SMTPSecure = 'ssl'; 
        $mail->Host = 'smtp.gmail.com';
        $mail->Port = 465;  
        $mail->Username = '[email protected]';  
        $mail->Password = '*******';   

        $mail->IsHTML(true);
        $mail->From="[email protected]";
        $mail->FromName="Example.com";
        $mail->Sender=$from; // indicates ReturnPath header
        $mail->AddReplyTo($from, $from_name); // indicates ReplyTo headers
        //$mail->AddCC('[email protected]', 'CC: to phpgang.com');
        $mail->Subject = $subject;
        $mail->Body = $body;
        $mail->AddAddress($to);
        if(!$mail->Send())
        {
            $error = 'Mail error: '.$mail->ErrorInfo;
            return true;
        }
        else
        {
            $error = 'Message sent!';
            return false;
        }
    }
?>

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:

$mail->AddAttachment($path); // $path: is your file path which you want to attach like $path = "reload.png";

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.

$mail->AddAttachment($path,$name,$encoding,$type); // advance parameters for attachment.

 

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

[wpdm_file id=73]

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:

4 responses to “How to send email with attachment in PHP”

  1. Andy Chan says:

    Hello
    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.

    • Huzoor Bux says:

      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 says:

        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.

Leave a Reply

Your email address will not be published. Required fields are marked *