如何将文件作为电子邮件附件发送?

时间:2013-02-25 13:13:36

标签: php excel spreadsheet

John McNamara的电子表格WriteExce可以正常下载excel文件。

目前使用此函数从mysql导出数据并生成excel。正常工作以执行操作下载,但我想创建动作电子邮件。我已经完成了所有设置,但我只需要更改动作下载以保存到tmp,例如

但我如何生成Excel tmp并通过电子邮件发送?

    <?php
        include("include/conexion.php"); // CONECT TO BBDD
        require_once('include/write_excel/Worksheet.php'); // CLASS EXCEL
        require_once('include/write_excel/Workbook.php'); // CLASS EXCEL
        include_once("include/Classes/class.phpmailer.php"); // CLASS EMAIL

        header("Content-type: application/vnd.ms-excel");
        header("Content-Disposition: inline; filename=$filename" );

        $filename = "Listado.xls";

        // Creating a workbook
        $workbook = new Workbook("-");
        $worksheet1 =& $workbook->add_worksheet('Listado');

        $formato_texto_negrita =& $workbook->add_format();
        $formato_texto_negrita->set_bold(0x2BC);
        $formato_texto_negrita->set_align('left');
        $formato_texto_negrita->set_size(8);

        $formato_texto =& $workbook->add_format();
        $formato_texto->set_align('left');
        $formato_texto->set_size(8);

        $formatot2 =& $workbook->add_format();
        $formatot2->set_align('right');
        $formatot2->set_size(8);

        $formato_numero =& $workbook->add_format();
        $formato_numero->set_align('right');
        $formato_numero->set_num_format('0.000,00;[Red]-0.000,00');
        $formato_numero->set_size(8);

        $i=3;
        $j=0;
        $k=0;
        $opcionListado = $_POST['opcionListado'];

        $worksheet1->set_column($k,$k++,10); // CLAVE
        $worksheet1->set_column($k,$k++,10); // Recepcion
        $worksheet1->set_column($k,$k++,15); // Ubicacion
        // NAME OF COLUMNS…..

        $res = mysql_query($query, $conexion);

        for($l=0;$row=mysql_fetch_array($res);$l++)
        {
        // DATE ROWS…….
        }

        file_put_contents($filename, $workbook->close());
?>

并且电子邮件代码包含在其他php中。

2 个答案:

答案 0 :(得分:0)

本文应该对您有所帮助:

http://www.html-form-guide.com/email-form/php-email-form-attachment.html

如果不这样做,我建议您只使用Google,因为此主题已经多次覆盖。

答案 1 :(得分:0)

只需像生成tmp目录一样生成电子表格。然后我建议你使用php邮件libary swift mail。

看一下这个例子

http://swiftmailer.org/docs/messages.html#attaching-dynamic-content