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. Commented Mar 18, 2018 at 23:30
  • 1
    Markdown used for the mail content, not for attachments. 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? Commented Mar 19, 2018 at 8:59
  • I need HTML file, not PDF. :) 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']); }); 
Sign up to request clarification or add additional context in comments.

2 Comments

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.
This is really awesome. It works fine and faster than generating pdf

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.