Sending a CSV file as an email attachment using PHP

Posted on 2009-12-22
Last Modified: 2013-12-13
I'm trying to send a CSV file as an email attachment using PHP, and am running into some problems with it.  The problem is that although the email gets sent/received -- the body of the received message is completely empty, and there is no attachment.  How can I resolve this?

My code is below.  Thanks in advance.

- Yvan


                        $mime_boundary = md5(date('r', time()));
                        $headers = "From: {$fromemail}\r\nReply-To: {$fromemail}";
                        $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";                        
                        $attachment = chunk_split(base64_encode(file_get_contents($filename)));
                        $subject = "My Subject Line";
                  $message = "
                  Content-Type: multipart/alternative; boundary='PHP-alt-$random_hash'
                  Content-Type: text/plain; charset='iso-8859-1'
                  Content-Transfer-Encoding: 7bit
                  Your regsitrations CSV fileis attached
                  Content-Type: text/html; charset='iso-8859-1'
                  Content-Transfer-Encoding: 7bit
                  <p>Your regsitrations CSV file is attached</p>
                  Content-Type: application/zip; name=$attachment
                  Content-Transfer-Encoding: base64
                  Content-Disposition: attachment
                  mail($toemail, $subject, $message, $headers);

Question by:egoselfaxis
    LVL 4

    Accepted Solution

    I would recommend that you use the PHPMailer class to send an email! It is very simple to add an attachment!

    Download it from here:
    See an example of sending an attachment here:

    Author Comment

    That worked great.  Thank you!

    - yg

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Suggested Solutions

    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now