LetMeLearn123
LetMeLearn123

Reputation: 61

sending email in laravel difficulties

I am trying to send an mail in laravel. Not sure if im doing it correctly but I based everything on tutorials, and trying to keep it as simple as possible. What am i doing wrong? It currently gives me an error that: "Argument 2 passed to Illuminate\Mail\Mailer::send() must be of the type array, string " If i leave an empty $data it says its undefined. Im not sure how to do all these things. Any help please?

 $data = "helloooo";

 Mail::send('emails.welcome', $data, function($message) { 
 $message->to('[email protected]', 'me')->subject('Welcome!'); });

Upvotes: 0

Views: 15334

Answers (2)

Mani dev
Mani dev

Reputation: 9

$data = "helloooo"; Instead using $data make it array. Like this $data['message'] = "helloooo";

As the argument passed to Illuminate\Mail\Mailer::send() must be of the type array, so we passed it in array format.

and in email view: emails.welcome use $message to show your variable value.

Upvotes: 0

Wader
Wader

Reputation: 9883

The clue is in your error. "Argument 2 passed to Illuminate\Mail\Mailer::send() must be of the type array, string given"

The array you need to pass to the Mail::send() function is exactly the same as the usual way a view is rendered.

For example you might do this to render a view.

$data['foo'] = 'bar';

return View::make('my.view', $data);

In your view you then have a variable of $foo available. The same applies to sending an email. Laravel still needs to render your view for the email. To solve your problem above...

$data = ['foo' => 'bar'];

Mail::send('emails.welcome', $data, function($message)
{
    $message->to('[email protected]', 'Jon Doe')->subject('Welcome!');
});

If you don't have/need any data to be passed to your view, just use an empty array.

$data = []; // Empty array

Mail::send('emails.welcome', $data, function($message)
{
    $message->to('[email protected]', 'Jon Doe')->subject('Welcome!');
});

Upvotes: 7

Related Questions