如何从字符串发送带有CSV附件的电子邮件

Ale*_*x L 2 php email

这是我的尝试,你可以告诉我我做错了什么吗?

allputcsv将数组转换为csv格式的字符串.那就是base64编码和chunk拆分,这似乎适用于普通文件,但是我应该做些什么,因为我使用的是字符串?

<?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);
?>
Run Code Online (Sandbox Code Playgroud)

注意:我必须使用我没有选择的邮件功能(我可以使用包装但我不愿意)并且我无法将文件保存在服务器端.

Dav*_*dom 25

您可以以更短更有效的方式轻松地将CSV创建代码包装到单个函数中.

CSV文件的正确MIME类型是text/csv.此外,您应该使用字符串连接和显式\r\n序列,因为RFC要求CRLF行分隔,并使用带有文字新行的heredoc/nowdoc/output缓冲,最终会出现2个问题:

  1. 您的缩进可能会搞砸消息格式
  2. 在ASCII模式下通过FTP复制文件可能会破坏行结尾

试试这个:

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.");
Run Code Online (Sandbox Code Playgroud)