如何在 Mime php 中创建和附加带有电子邮件的 excel 文件
Posted
技术标签:
【中文标题】如何在 Mime php 中创建和附加带有电子邮件的 excel 文件【英文标题】:How to create and attach excel file with email in Mime php 【发布时间】:2016-12-08 18:09:28 【问题描述】:我正在尝试创建一个 Excel 文件并将其作为电子邮件发送而不将其保存在服务器上。电子邮件已成功分,但没有附件。请帮忙
<?php
include 'Mail.php';
include 'Mail/mime.php' ;
//creating excel file
header('Content-type: application/excel');
$t=time();
$filename ="rep$t.xls";
header('Content-Disposition: attachment; filename='.$filename);
$data = '<html xmlns:x="urn:schemas-microsoft-com:office:excel">
<head>
<!--[if gte mso 9]>
<xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name>Sheet 1</x:Name>
<x:WorksheetOptions>
<x:Print>
<x:ValidPrinterInfo/>
</x:Print>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
</x:ExcelWorkbook>
</xml>
<![endif]-->
</head>
<body>
<table><tr>
<th>SL</th>
<th>id</th>
<th>DATE TIME</th>
<th>Name</th>
<th>Roll No</th>
<th>Class</th>
<th>Section</th>
<th>FEE</th>
<th>STATUS</th>
</tr></table>
</body></html>';
这是将上述文件作为附件发送的代码
// sendign email
$from = "aaa@aaa.com";
$subject = "Details\r\n\r\n";
$to="aaa2@aaa.com";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject );
$abc="Please find today file attached herewith";
$crlf = "\n";
$mime = new Mail_mime($crlf);
// Setting the body of the email
$mime->setHTMLBody($abc);
$mime->addAttachment($filename, 'text/plain');
$body = $mime->get();
$headers = $mime->headers($headers);
$smtp = Mail::factory('smtp',
array ('host' => $emailhost,
'auth' => true,
'username' => '',
'password' => ''));
$mail=$smtp->send($to, $headers, $body);
if (PEAR::isError($mail))
echo 'There was a problem sending the email.';
else
echo '<br>A message has been sent on your email address '.$cemail;
?>
【问题讨论】:
【参考方案1】:使用 Phpmailer 附件功能完成你的任务
【讨论】:
以上是关于如何在 Mime php 中创建和附加带有电子邮件的 excel 文件的主要内容,如果未能解决你的问题,请参考以下文章