簡體   English   中英

Laravel:從視圖中抓取html並將其作為郵件附件發送

[英]Laravel: grab html from the view and send it as a mail attachment

我有一個刀片模板,可以生成我需要的內容:

invoice.blade.php

//nothing interesting here, just html

我有一個通過鏈接訪問此視圖的路由:

web.php

Route::get('/invoice/{id}', function ($id) {return view('invoice.invoice')->with('id', $id);});

通過laravel郵件程序發送html文件(由此路由生成)的更好方法是什么? 我試着這樣做:

$file = file_get_contents(url('invoice/1'));    
Mail::send([], [], function ($message) use ($file) {
       ...
       $message->attach($file, [
          'as' => 'file-name',
          'mime' => 'text/html',
       ]);
    });

Mailer工作正常,但是得到一個關於file_get_contents超出最大執行時間的錯誤(我知道它可以通過allow_url_fopen PHP設置修復),我也知道curl方法 ,但它現在也不能通過服務器設置進行測試。 所以我的問題是:我們有另一種方法來實現這個嗎? 哪種方式更好(更快)?

解決方法由view()->render()和mailer ->attachData()函數解決。

$file = view('invoice.invoice_inline', ['id' => $id])->render();
Mail::send([], [], function ($message) use ($file) {
   ...
   $message->attachData($file, 'filename.html', ['mime' => 'text/html']);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM