1

I have a blade template that generates the content that I need:

invoice.blade.php

//nothing interesting here, just html

I have a route to access this view by a link:

web.php

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

What is the better way to send the html file (which is generated by this route) by laravel mailer? I tried to do it in this way:

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

Mailer works fine, but get an error about maximum execution time exceeded for file_get_contents (I know it can be fixed by allow_url_fopen PHP setting), also I know about curl method, but it also not available for test for me right now by server settings. So my question is: do we have another way to implement this? Which way is better (faster)?

4
  • Use markdown mails instead. Good luck.
    – Kyslik
    Commented Mar 18, 2018 at 23:30
  • 1
    Markdown used for the mail content, not for attachments.
    – Alex
    Commented Mar 19, 2018 at 8:48
  • It was too late and I tried to be too clever. Good, that you found out how to do it. Wouldn't it be easier to generate PDF instead?
    – Kyslik
    Commented Mar 19, 2018 at 8:59
  • I need HTML file, not PDF. :)
    – Alex
    Commented Mar 19, 2018 at 9:00

1 Answer 1

4

Solved by view()->render() and mailer ->attachData() functions.

$file = view('invoice.invoice_inline', ['id' => $id])->render();
Mail::send([], [], function ($message) use ($file) {
   ...
   $message->attachData($file, 'filename.html', ['mime' => 'text/html']);
});
2
  • 1
    I definitely owe you a beer, my friend. What I am working on is a bit different, but close enough that this helped a ton.
    – cfnerd
    Commented Sep 12, 2018 at 23:49
  • 1
    This is really awesome. It works fine and faster than generating pdf Commented Jul 7, 2020 at 21:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.