我已经导出了一个xls文件,但我想将这个xls文件邮寄到邮件的附件中。
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中导出了$ data中的内容。但我想将这个xls文件邮寄到附有xls文件的邮件中。
答案 0 :(得分: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";
?>
添加所需的标题并发送!
<?php
$headers = "From: \"Me\"<me@example.com>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"";
mail('myAddress@example.com', 'Email with attachment from PHP', $message, $headers);