2012-11-27 107 views
1

我已經導出了一個xls文件,但我想郵件附件中的這個xls文件。如何發送附件與php郵件

exportexcel.php

<?php 
session_start(); 


//export format 
$fileNamed = $username."_".$date_time; 
header ("Content-type: application/x-msexcel"); 
header ("Content-Disposition: attachment; filename=$fileNamed.xls"); 
header ("Content-Description: PHP/INTERBASE Generated Data"); 

echo $data; 
?> 

上述PHP文件出口XLS什麼都在$數據。但我想用附加的xls文件將這個xls文件郵寄到郵件中。

回答

0

電子郵件可以分成許多部分,由邊界分隔,然後是Content-Type和Content-Disposition。

<?php 
$boundary = '-----=' . md5(uniqid (rand())); 
?> 

您可以將Word文檔,如果你指定:

<?php 
$message .= "Content-Type: application/msword; name=\"my attachment\"\n"; 
$message .= "Content-Transfer-Encoding: base64\n"; 
$message .= "Content-Disposition: attachment; filename=\"$theFile\"\n\n"; 
?> 

當添加你必須打開它,並用的fopen閱讀並添加文件

的邊界如下初始化消息內容:

<?php 
$path = "whatever the path to the file is"; 
$fp = fopen($path, 'r'); 
do //we loop until there is no data left 
{ 
     $data = fread($fp, 8192); 
     if (strlen($data) == 0) break; 
     $content .= $data; 
     } while (true); 
$content_encode = chunk_split(base64_encode($content)); 
$message .= $content_encode . "\n"; 
$message .= "--" . $boundary . "\n"; 
?> 

添加需要的標題併發送!

​​