如何把php匯出的Excel 作為郵件發送

來源:互聯網
上載者:User
現在實現了點擊後下載excel,和發送文本郵件的功能,怎麼能結合下,把php匯出的excel作為附件發送就完美了。
1 .產生excel:
header("Content-type:application/octet-stream");    header("Accept-Ranges:bytes");    header("Content-type:application/vnd.ms-excel");      header("Content-Disposition:attachment;filename=".$filename.".xls");    header("Pragma: no-cache");    header("Expires: 0");    if (!empty($title)){        foreach ($title as $k => $v) {            $title[$k]=iconv("UTF-8", "GB2312",$v);        }        $title= implode("\t", $title);        echo "$title\n";    }    if (!empty($data)){        foreach($data as $key=>$val){            foreach ($val as $ck => $cv) {                $data[$key][$ck]=iconv("UTF-8", "GB2312", $cv);            }            $data[$key]=implode("\t", $data[$key]);        }        echo implode("\n",$data);    }


2 . 發送郵件:
用了phpmailer類庫
$mail    = new PHPMailer();       $mail->CharSet    = 'UTF-8';               $mail->IsSMTP();                   $mail->SMTPAuth   = true;                     $mail->SMTPSecure = '';                       $mail->Host       = $config['SMTP_HOST'];  // SMTP 伺服器      $mail->Port       = $config['SMTP_PORT'];  // SMTP伺服器的連接埠號碼      $mail->Username   = $config['SMTP_USER'];  // SMTP伺服器使用者名稱      $mail->Password   = $config['SMTP_PASS'];  // SMTP伺服器密碼      $mail->SetFrom($config['FROM_EMAIL'], $config['FROM_NAME']);      $replyEmail       = $config['REPLY_EMAIL']?$config['REPLY_EMAIL']:$config['FROM_EMAIL'];      $replyName        = $config['REPLY_NAME']?$config['REPLY_NAME']:$config['FROM_NAME'];      $mail->AddReplyTo($replyEmail, $replyName);      $mail->Subject    = $subject;      $mail->MsgHTML($body);      $mail->AddAddress($to, $name);      if(is_file($attachment)){                   // 添加附件          $mail->AddAttachment($attachment);      }      return $mail->Send()


回複討論(解決方案)

第7行處加入
ob_start();

第23行後加入
$s = ob_get_flush();
file_put_contents($filename.".xls", $s);
$attachment = $filename.".xls";
執行郵件發送

第7行處加入
ob_start();

第23行後加入
$s = ob_get_flush();
file_put_contents($filename.".xls", $s);
$attachment = $filename.".xls";
執行郵件發送


好的好的 我也試了ob_get_contents 半天還是沒思路 謝謝老大指點 我試試看

第7行處加入
ob_start();

第23行後加入
$s = ob_get_flush();
file_put_contents($filename.".xls", $s);
$attachment = $filename.".xls";
執行郵件發送


加了ob_start() excel打不開了,怎麼能做到不彈出另存excel的輸出呢,就附件只送到$s = ob_get_flush();這裡。 不再瀏覽器介面上任何彈出

肯定是你哪裡出錯了,認真檢查一下
你實際輸出的是文字檔,用記事本就可開啟

ob 函數的功能、用法,手冊中都有

肯定是你哪裡出錯了,認真檢查一下
你實際輸出的是文字檔,用記事本就可開啟

ob 函數的功能、用法,手冊中都有


嗯嗯 看了用法 能做到禁止輸出嗎? 寫完附件清除。。 不彈出另存

看了還問?
ob_get_clean

可能要寫到磁碟上 ,在發送這個磁碟檔案。。

汗!那樣匯出的還沒有路徑,你如何作為附件發送呢??
你這不是天方夜譚嗎?

提個思路,你可參考下:
先把excel儲存在伺服器上,然後獲得該excel的路徑,然後作為附件進行email發送,如果你不需要這個檔案了,然後再執行刪除操作就OK了

提個思路,你可參考下:
先把excel儲存在伺服器上,然後獲得該excel的路徑,然後作為附件進行email發送,如果你不需要這個檔案了,然後再執行刪除操作就OK了


en 就是不想組建檔案 直接擷取內容傳送檔案 然後刪除臨時檔案。。 已經按 @xuzuning 大哥的方法實現 謝謝你
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.