2016-08-03 80 views
0

我想创建一个excel文件并将其作为电子邮件发送,而不必将其保存在服务器上。电子邮件已成功,但没有附带。请帮助如何使用电子邮件在Mime中创建和附加excel文件php

<?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>'; 

下面是发送上述文件作为attachemet

// sendign email 
$from = "[email protected]"; 
$subject = "Details\r\n\r\n"; 
$to="[email protected]"; 
$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; 
} 

?> 

回答

0

使用PHPMailer的附件功能来完成你的任务

代码
相关问题