Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7862635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:13:06+00:00 2026-06-02T23:13:06+00:00

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

  • 0

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?

<?php

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

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

    rewind($fp);
    $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 = 'youraddress@example.com'; 
    $subject = 'Test email with attachment'; 
    $random_hash = md5(date('r', time())); 
    $headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com"; 
    $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"--".$random_hash."\""; 
    $attachment = chunk_split(base64_encode(allputcsv($arr))); 
    ob_start();
    ?> 
    --<?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/vnd.ms-excel;
     name="test.csv"
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment;
     filename="test.csv"

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

    <?php 
    $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));

send_mail($array);
?>

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:13:09+00:00Added an answer on June 2, 2026 at 11:13 pm

    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
      rewind($fp);
    
      // Return the data
      return stream_get_contents($fp);
    
    }
    
    function send_csv_mail ($csvData, $body, $to = 'youraddress@example.com', $subject = 'Test email with attachment', $from = 'webmaster@example.com') {
    
      // 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.");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my attempt in jquery that kind of works but something is going
Total noob here. This is my first c# attempt, its a console application that
My methodology may be wrong here (or implemented better). If so, please let me
Here is my attempt: function makeAllTextBlack() { var document = app.activeDocument, textFrames = document.textFrames,
How to dynamically change the timeout of the cycle plugin here is my attempt
I'm trying to calculate time blocks from given date range. Here's my attempt: <?php
Here is my first attempt at validating XML with XSD. The XML file to
Here in stackoverflow, if you started to make changes then you attempt to navigate
Rookie C++ Programmer here again I'm using VC++ VS2008 and making an attempt at
I'm getting this error: -[NSCFArray insertObject:atIndex:]: attempt to insert nil Here is the .m

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.