[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Sending a CSV file as an email attachment using PHP

Posted on 2009-12-22
Medium Priority
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

Accepted Solution

72lions earned 2000 total points
ID: 26104744
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: http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php5_6/
See an example of sending an attachment here: http://zainal.wordpress.com/2007/01/10/sending-email-attachments-in-php-using-phpmailer-class/

Author Comment

ID: 26104994
That worked great.  Thank you!

- yg

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses
Course of the Month19 days, 20 hours left to enroll

872 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