PHP邮件功能发送一个空的Excel工作表作为附件

时间:2019-02-13 14:22:20

标签: php excel php-5.6

我正在尝试将excel表格作为附件发送到邮件中(大约9MB大小),但始终以电子邮件中的excel表格为空结束。我正在使用PHP5.6发送xls文档。这是代码

@ExceptionHandler

此外,我已经在控制台中收到此警告消息。

$strToEmail = 'tess@gmail.com';
$strSubject = "Successfully generated Excel";
$strBody = "Successfully generated excel ";
sendmailWithAttachement($strToEmail, $strSubject, $strBody, __DIR__, "Book2.xls" );


function sendmailWithAttachement($mailto, $subject, $message,$path, $filename) {
    $file = $path . "/" . $filename;

    $content = file_get_contents($file);
    $content = chunk_split(base64_encode($content));

    // a random hash will be necessary to send mixed content
    $separator = md5(time());

   // carriage return type (RFC)
   $eol = "\r\n";

   $random_hash = md5(date('r', time()));
   // main header (multipart mandatory)
   $headers = "From:test@test.com" . $eol;
   $headers .= "MIME-Version: 1.0" . $eol;
   $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
   $headers .= "Content-Transfer-Encoding: 7bit" . $eol;
   $headers .= "This is a MIME encoded message." . $eol;

   // message
   $body = "--" . $separator . $eol;
   $body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
   $body .= "Content-Transfer-Encoding: BASE64" . $eol;
   $body .= $message . $eol;

   // attachment
   $body .= "--" . $separator . $eol;
   $body .= "Content-Type: application/vnd.ms-excel; name=\"" . $filename . "\"" . $eol;
   $body .= "Content-Transfer-Encoding: 7bit" . $eol;
   $body .= "Content-Disposition: attachment" . $eol;
   $body .= $content . $eol;
   $body .= "--" . $separator . "--";

   //SEND Mail
   if (mail($mailto, $subject, $body, $headers)) {
        echo "mail send ... OK"; // or use booleans here
    } else {
        echo "mail send ... ERROR!";
        print_r( error_get_last() );
    }
}

0 个答案:

没有答案