• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 388
  • Last Modified:

Sending a CSV file as an email attachment using PHP

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);

1 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: 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/
egoselfaxisAuthor Commented:
That worked great.  Thank you!

- yg
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now