Hi,
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));
fclose($file);
$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.\
\
" .
"--{$mime_boundary}\
" .
"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 .= "\
\
" .
"--{$mime_boundary}--\
";
$ok = @mail($email_to, $email_subject, $email_message, $headers);
if($ok) {
echo "Sent";
} else {
die("Problem");
}
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!
Thanks,
Michael