简体   繁体   English

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

[英]How to create and attach excel file with email in Mime php

I am trying to create an excel file and sending it as email without saving it on server. 我试图创建一个excel文件并以电子邮件形式发送而不将其保存在服务器上。 Email has cent successfully but without attachement. 电子邮件已成功完成,但没有附件。 Please help 请帮忙

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

Here is the code for sending the above file as attachemet 这是用于将上述文件作为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;
}

?>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM