如何在Mime php中使用电子邮件创建和附加excel文件

时间:2016-08-03 06:58:09

标签: php excel email mime

我正在尝试创建一个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>';

以下是将上述文件作为attachemet

发送的代码
// 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 个答案:

答案 0 :(得分:0)

使用Phpmailer附件功能完成您的任务