Alex L Alex L - 1 year ago 161
PHP Question

How to send an email with a CSV attachment from a string

Here is my attempt at it can you let me know what I am doing wrong?

allputcsv converts the array into a csv formatted string. That is then base64 encoded and chunk split, this seems to work for normal files but is there something different I should do because I am using a string?


function sputcsv($row, $delimiter = ',', $enclosure = '"', $eol = "\n")
static $fp = false;
if ($fp === false)
$fp = fopen('php://temp', 'r+');

if (fputcsv($fp, $row, $delimiter, $enclosure) === false)
return false;

$csv = fgets($fp);

if ($eol != PHP_EOL)
$csv = substr($csv, 0, (0 - strlen(PHP_EOL))) . $eol;
return $csv;

function allputcsv($arr) {
$str = "";
foreach($arr as $val) {
$str .= sputcsv($val);
return $str;

function send_mail($arr) {
$to = '';
$subject = 'Test email with attachment';
$random_hash = md5(date('r', time()));
$headers = "From:\r\nReply-To:";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"--".$random_hash."\"";
$attachment = chunk_split(base64_encode(allputcsv($arr)));
--<?php echo $random_hash; ?>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Hello World!!!
This is simple text email message.

--<?php echo $random_hash; ?>
Content-Type: application/;
Content-Transfer-Encoding: base64
Content-Disposition: attachment;

<?php echo $attachment; ?>
--<?php echo $random_hash; ?>--

$message = ob_get_clean();
$mail_sent = @mail( $to, $subject, $message, $headers );

$array = array(array(1,2,3,4,5,6,7),array(1,2,3,4,5,6,7),array(1,2,3,4,5,6,7));


Notes: I have to use the mail function I don't have a choice (I could use a wrapper but I would prefer not to) and I cant save the file on the server side.

Answer Source

You can wrap the CSV creation code into a single function easily in a much shorter and more efficient way.

The correct MIME type for CSV files is text/csv. Also, you should use string concatenation and explicit \r\n sequences, since RFCs call for CRLF line separations and by using heredoc/nowdoc/output buffering with literal new lines, you will end up with 2 problems:

  1. Your indentation may screw up the message format
  2. Copying the file via FTP in ASCII mode may break the line endings

Try this instead:

function create_csv_string($data) {

  // Open temp file pointer
  if (!$fp = fopen('php://temp', 'w+')) return FALSE;

  // Loop data and write to file pointer
  foreach ($data as $line) fputcsv($fp, $line);

  // Place stream pointer at beginning

  // Return the data
  return stream_get_contents($fp);


function send_csv_mail ($csvData, $body, $to = '', $subject = 'Test email with attachment', $from = '') {

  // This will provide plenty adequate entropy
  $multipartSep = '-----'.md5(time()).'-----';

  // Arrays are much more readable
  $headers = array(
    "From: $from",
    "Reply-To: $from",
    "Content-Type: multipart/mixed; boundary=\"$multipartSep\""

  // Make the attachment
  $attachment = chunk_split(base64_encode(create_csv_string($csvData))); 

  // Make the body of the message
  $body = "--$multipartSep\r\n"
        . "Content-Type: text/plain; charset=ISO-8859-1; format=flowed\r\n"
        . "Content-Transfer-Encoding: 7bit\r\n"
        . "\r\n"
        . "$body\r\n"
        . "--$multipartSep\r\n"
        . "Content-Type: text/csv\r\n"
        . "Content-Transfer-Encoding: base64\r\n"
        . "Content-Disposition: attachment; filename=\"file.csv\"\r\n"
        . "\r\n"
        . "$attachment\r\n"
        . "--$multipartSep--";

   // Send the email, return the result
   return @mail($to, $subject, $body, implode("\r\n", $headers)); 


$array = array(array(1,2,3,4,5,6,7), array(1,2,3,4,5,6,7), array(1,2,3,4,5,6,7));

send_csv_mail($array, "Hello World!!!\r\n This is simple text email message.");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download