Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 如何把php导出的Excel 作为邮件发送

如何把php导出的Excel 作为邮件发送

Jun 23, 2016 pm 01:54 PM
excel php send Export mail

现在实现了点击后下载excel,和发送文本邮件的功能,怎么能结合下,把php导出的excel作为附件发送就完美了。
1 .生成excel:

header("Content-type:application/octet-stream");    header("Accept-Ranges:bytes");    header("Content-type:application/vnd.ms-excel");      header("Content-Disposition:attachment;filename=".$filename.".xls");    header("Pragma: no-cache");    header("Expires: 0");    if (!empty($title)){        foreach ($title as $k => $v) {            $title[$k]=iconv("UTF-8", "GB2312",$v);        }        $title= implode("\t", $title);        echo "$title\n";    }    if (!empty($data)){        foreach($data as $key=>$val){            foreach ($val as $ck => $cv) {                $data[$key][$ck]=iconv("UTF-8", "GB2312", $cv);            }            $data[$key]=implode("\t", $data[$key]);        }        echo implode("\n",$data);    }
Copy after login


2 . 发送邮件:
用了phpmailer类库
$mail    = new PHPMailer();       $mail->CharSet    = 'UTF-8';               $mail->IsSMTP();                   $mail->SMTPAuth   = true;                     $mail->SMTPSecure = '';                       $mail->Host       = $config['SMTP_HOST'];  // SMTP 服务器      $mail->Port       = $config['SMTP_PORT'];  // SMTP服务器的端口号      $mail->Username   = $config['SMTP_USER'];  // SMTP服务器用户名      $mail->Password   = $config['SMTP_PASS'];  // SMTP服务器密码      $mail->SetFrom($config['FROM_EMAIL'], $config['FROM_NAME']);      $replyEmail       = $config['REPLY_EMAIL']?$config['REPLY_EMAIL']:$config['FROM_EMAIL'];      $replyName        = $config['REPLY_NAME']?$config['REPLY_NAME']:$config['FROM_NAME'];      $mail->AddReplyTo($replyEmail, $replyName);      $mail->Subject    = $subject;      $mail->MsgHTML($body);      $mail->AddAddress($to, $name);      if(is_file($attachment)){                   // 添加附件          $mail->AddAttachment($attachment);      }      return $mail->Send()
Copy after login


回复讨论(解决方案)

第7行处加入 
ob_start();

第23行后加入
$s = ob_get_flush();
file_put_contents($filename.".xls", $s);
$attachment = $filename.".xls";
执行邮件发送

第7行处加入 
ob_start();

第23行后加入
$s = ob_get_flush();
file_put_contents($filename.".xls", $s);
$attachment = $filename.".xls";
执行邮件发送


好的好的 我也试了ob_get_contents 半天还是没思路 谢谢老大指点  我试试看

第7行处加入 
ob_start();

第23行后加入
$s = ob_get_flush();
file_put_contents($filename.".xls", $s);
$attachment = $filename.".xls";
执行邮件发送


加了ob_start() excel打不开了,怎么能做到不弹出另存excel的输出呢,就附件只送到$s = ob_get_flush();这里。 不再浏览器界面上任何弹出

肯定是你哪里出错了,认真检查一下
你实际输出的是文本文件,用记事本就可打开

ob 函数的功能、用法,手册中都有

肯定是你哪里出错了,认真检查一下
你实际输出的是文本文件,用记事本就可打开

ob 函数的功能、用法,手册中都有


嗯嗯 看了用法  能做到禁止输出吗?  写完附件清除。。 不弹出另存

看了还问?
ob_get_clean

可能要写到磁盘上 ,在发送这个磁盘文件。。

汗!那样导出的还没有路径,你如何作为附件发送呢??
你这不是天方夜谭么?

提个思路,你可参考下:
先把excel保存在服务器上,然后获得该excel的路径,然后作为附件进行email发送,如果你不需要这个文件了,然后再执行删除操作就OK了

提个思路,你可参考下:
先把excel保存在服务器上,然后获得该excel的路径,然后作为附件进行email发送,如果你不需要这个文件了,然后再执行删除操作就OK了


en 就是不想生成文件 直接获取内容发送文件 然后删除临时文件。。 已经按 @xuzuning 大哥的方法实现 谢谢你
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles