CSV by email attachment - what mime type to use?


I’m trying to send a CSV file as an email attachment - here is my code ATM:

$fileatt = "$filename"; // Path to the file
$fileatt_type = "application/vnd.ms-excel"; // File Type
$fileatt_name = "$date"; // Filename that will be used for the file as the attachment

$email_from = "no-reply@domain.com"; // Who the email is from
$email_subject = "Your attached file"; // The Subject of the email
$email_message = "Sent Message";

$email_to = "myemail@domain.com";

$headers = "From: ".$email_from;

$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));

$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers .= "\
MIME-Version: 1.0\
" .
"Content-Type: multipart/mixed;\
" .
" boundary=\\"{$mime_boundary}\\"";

$email_message .= "This is a multi-part message in MIME format.\
" .
" .
"Content-Type:text/html; charset=\\"iso-8859-1\\"\
" .
"Content-Transfer-Encoding: 7bit\
" .
$email_message .= "\

$data = chunk_split(base64_encode($data));

$email_message .= "--{$mime_boundary}\
" .
"Content-Type: {$fileatt_type};\
" .
" name=\\"{$fileatt_name}\\"\
" .
"Content-Transfer-Encoding: base64\
" .
$data .= "\
" .

$ok = @mail($email_to, $email_subject, $email_message, $headers);

if($ok) {
echo "Sent";

} else {

The code works fine and I receive the data. However I can’t get the script to send the file as a .CSV file. I have tried using the “text/csv” and “text/comma-separated-values” MIME types and both send the file as a .txt file. Using the “application/vnd.ms-excel” MIME type it sends the file as an XLS file which doesn’t display the file correctly.

Am I doing something wrong? How can I get the PHP script to attach the file as a .csv file?

Any help would be greatly appreciated!


First thing I would do is use a a proper mailer, like SwiftMailer, instead of constructing the headers, etc yourself, just manipulate an easy API and voila.

There are a lot of errors in your code:

  • Some
    are wrong or missing.
  • Use Punycode for e-mail-adresses.
  • Use mb_encode_mimeheader for subject.
  • Add Content-Disposition info.
  • And all data must be put into the header.

Finally mail must look like:


Google: php mail attachments

I think, csv-files are not binary-files. So I would not use ‘b’.
file_get_contents ist easier than fopen…

For testing:

  • don´t use any @
  • At the beginning of your script: error_reporting(E_ALL|E_STRICT);

Does anyone have any idea on the MIME type issue? I understand the code may not be perfect but it sends an email fine, its just configuring it to send it as a .csv file im having trouble with.

MIME type is text/plain or text/csv for CSV files

Thanks for your reply Mark, when I use text/plain or text/csv to send the file as an attachement using the code above it sends the file attached as a .txt file, do you know anyway to get it to send as a .csv file?

Give the filename the correct extension: $fileatt_name = $date.‘.csv’;

Mark, thanks a lot, works perfectly, don’t know how I missed that one!