Categories
Laravel

Laravel Mail Callback Scope

Sending email with Laravel uses a callback closure to set the recipient, subject & other headers. You may need that callback to access variables from the scope of the calling code.
Like this:

[code language=”php”]
$to = ‘CaptainMorgan@example.com’;

Mail::send(‘myEmailView’, [‘data’=>value], function($message) use ($to) {
$message->to($to)->subject(‘Hey, do stuff’);
});
[/code]

Ensure a space after use statement.
Via StackOverflow.

Leave a Reply

Your email address will not be published. Required fields are marked *