laravelmailer

Laravel attach with variable?


I encountered an interesting problem and I would like to ask for your help in this regard. I want to attach an invoice in the email, but for some reason I can't do it.

code:

        Mail::send('emails.nygta', ['number' => $result->getDocumentNumber(), 'link' => 'https://'.$_SERVER['HTTP_HOST'].'/pdf/'.$result->getDocumentNumber().'.pdf'], function($m) use ($email) {
            $m->from('support@task.net', 'task.net');
            $m->to($email, $email)->subject(trans('welcome.nygtsubject'));
            $m->attach(base_path('szamlazzhu/pdf/'.$result->getDocumentNumber().'.pdf'), ['as' => 'NYUGTA.pdf', 'mime' => 'application/pdf']);
        });

I checked the route and it points to the right place.

if I added a direct path, for example:

base_path('szamlazzhu/pdf/INVOICE-2022-10.pdf')

Then it sends the message, perfectly.

getDocumentNumber() -> returns good value.

In the direct path, I enter a non-existent index, and it is only created when creating the invoice, so I am trying to attach an existing pdf file.

Sorry, bad english!


Solution

  • The 3rd argument into Mail::send is an anonymous function, in which you bring $email into scope, but not $result, so within the function $result is undefined.

    If you have error logs enabled it should have entry in there about this. Change your function to this and it should work.

    function($m) use ($email, $result)