Skip to main content

Send Mail with File Attachment using PHP

In this post I'm going to show you how we can send mail with file attachment using PHP mail function. To do this we need to encode file into base64 encoding system using PHP base64_encode function and add it to PHP mail header parameter with MIME.

Email Function
function email($mail_from, $from_name, $mail_to, $subject, $message, $file){
    
    $file_name = basename($file); // Get file name
    $data = file_get_contents($file); // Read file contents 
    $file_contents = chunk_split(base64_encode($data)); // Encode file data into base64
    $uid = md5(time()); // Create unique boundary from timestamps 
    $headers = array();
    $headers[] = "MIME-Version: 1.0";
    $headers[] = "From: {$from_name}<{$mail_from}>";
    $headers[] = "Reply-To: {$mail_from}";
    $headers[] = "Content-Type: multipart/mixed; boundary=\"{$uid}\"";
    $headers[] = "This is a multi-part message in MIME format.";
    $headers[] = "--{$uid}";
    $headers[] = "Content-type:text/plain; charset=iso-8859-1"; // Set message content type
    $headers[] = "Content-Transfer-Encoding: 7bit";
    $headers[] = $message; // Dump message
    $headers[] = "--{$uid}";
    $headers[] = "Content-Type: application/octet-stream; name=\"{$file_name}\""; // Set content type and file name
    $headers[] = "Content-Transfer-Encoding: base64"; // Set file encoding base
    $headers[] = "Content-Disposition: attachment; filename=\"{$file_name}\""; // Set file Disposition
    $headers[] = $file_contents; // Dump file
    $headers[] = "--{$uid}--"; //End boundary
    // Send mail with header information
    if (mail($mail_to, $subject, '', implode("\r\n", $headers) ))
        return true;
}

Usages
$from = "mail@w3bees.com";
$name = "W3Bees";
$to = "name@server.com";
$subject = "My mail subject";
$message = "My message";
$file = 'path/to/file';
if(email($from, $name, $to, $subject, $message, $file)){
    echo "Success!";
}
else{
    echo "Error!";
}

Comments

  1. I am getting that error (Bad parameters to mail() function, mail not sent. ) when I am dumping the content of my file in the header (Line 21) ? why ??

    ReplyDelete
    Replies
    1. I'm so sorry. I made a mistake on email function. I updated the source. I hope now it will be work now.
      Thanks!

      Delete
  2. Ace Maxs Asli Di Apotik K24 - Selain di apotik k24 juga ace maxs asli bisa anda dapatkan disini

    ReplyDelete
  3. I have read your blog its very attractive and impressive. I like it your blog.

    PHP Training in chennai | PHP Training Course

    PHP Training in chennai | Online PHP Course

    ReplyDelete

Post a Comment

Popular posts from this blog

Multiple File Upload with Progress Bar using PHP & jQuery

Some days age I have created a post that deals with How we can upload file with progress bar using php and jQuery . Some of my readers asked me how we can upload multiple files with progress bar. In this post I am going to show you how we can upload multiple files with progress bar. We can upload multiple files with progress bar exactly same way as I shown previous. But we need to do some modification with our file upload form and our php script to handle multiple files.

Force File Download with PHP

If you want to control or track your downloadable contents then a force file downloader script can give you more option. Especially if you want make images or other stemming contents like music file (mp3, wav, ogg), video files (flv, mp4, ogg, 3gpp), css, JavaScripts or even a php file downloadable then you must need to use a force file downloader script. In this post I am going to share a simple but useful force file downloader script.

Multiple File Upload with PHP

Sometimes we need to allow our users to upload multiple file upload. On my previous post I was shown the basic of simple file upload with PHP. Today I am going to show you how to allow users to upload multiple files. It is almost similar like simple file upload but we need to do some modification with html markup and php code . multiple attribute is needed to add on the html markup. Major web browsers like Firefox, Chrome, Safari and IE 9+ support this attribute. Now I am going to show you how to upload multiple file with PHP and basic HTML form.