【问题标题】:send an email exported cvs file php mysql发送电子邮件 导出的 csv 文件 php mysql
【发布时间】:2012-02-28 11:42:48
【问题描述】:

我正在尝试发送电子邮件导出的 csv 文件。但是,当我单击链接时,会弹出一个窗口来下载带有 MySQL 记录的 CVS。我如何将这个 csv 文件发送到特定的电子邮件地址?非常感谢您的帮助和想法。 最好的祝福。 这是我的代码

header("Content-type: application/x-msdownload");
    header("Content-Disposition: attachment; filename=log.csv");
    header("Pragma: no-cache");
    header("Expires: 0");
     $resultstr = array();
    foreach ($selectionlist as $result)
      $resultstr[] = $result;

    $ww=implode(",",$resultstr);

    function escape_csv_value($value) {
        $value = str_replace('"', '""', $value); // First off escape all " and make them ""
        if(preg_match('/,/', $value) or preg_match("/\n/", $value) or preg_match('/"/', $value)) { // Check if I have any commas or new lines
            return '"'.$value.'"'; // If I have new lines or commas escape them
        } else {
            return $value; // If no new lines or commas just return the value
        }
    }


    $sql = mysql_query("SELECT * FROM article 
    WHERE idArticle in ($ww) ORDER BY idArticle DESC"); // Start our query of the database

    $numberFields = mysql_num_fields($sql) or die('MySql Error' . mysql_error());; // Find out how many fields we are fetching

    if($numberFields) { // Check if we need to output anything
        for($i=0; $i<$numberFields; $i++) {
            $keys[] = mysql_field_name($sql, $i); // Create array of the names for the loop of data below
            $col_head[] = escape_csv_value(mysql_field_name($sql, $i)); // Create and escape the headers for each column, this is the field name in the database
        }
        $col_headers = join(',', $col_head)."\n"; // Make our first row in the CSV

        $data = '';
        while($info = mysql_fetch_object($sql)) {
            foreach($keys as $fieldName) { // Loop through the array of headers as we fetch the data
                $row[] = escape_csv_value($info->$fieldName);
            } // End loop
            $data .= join(',', $row)."\n"; // Create a new row of data and append it to the last row
            $row = ''; // Clear the contents of the $row variable to start a new row
        }
        // Start our output of the CSV
        /*header("Content-type: application/x-msdownload");
        header("Content-Disposition: attachment; filename=log.csv");
        header("Pragma: no-cache");
        header("Expires: 0");*/
        echo $col_headers.$data;

    } else {
        // Nothing needed to be output. Put an error message here or something.
        echo 'No data available for this CSV.';
    }

【问题讨论】:

  • $selectionlist 来自哪里?您的代码中可能存在 SQL 注入形式的安全问题。您应该阅读相应的PHP manual page
  • 要通过电子邮件发送 CSV,您需要使用适当的电子邮件功能而不是回显数据。最简单的方法是使用 Mail_Mime PEAR 包,它可以让您轻松地将 CSV 数据添加为附件。或者,您必须自己构建电子邮件,使用如下内容:finalwebsites.com/forums/topic/php-e-mail-attachment-script

标签: php mysql email csv export


【解决方案1】:

好的。首先,您必须保存 CSV 文件。如果您按照提到的方式设置标题,则文件将自动下载。请阅读这篇文章。

http://us2.php.net/manual/en/function.fputcsv.php

创建 CSV 文件后,您可以使用 PHP 邮件功能通过电子邮件发送它。如果您需要一些图书馆,只需检查一下。很容易实现。

http://www.redvodkajelly.com/code/php-email-class/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-16
    • 2011-07-21
    • 1970-01-01
    • 2012-06-15
    • 2016-04-13
    • 1970-01-01
    • 2011-08-14
    相关资源
    最近更新 更多