Support the ongoing development of Laravel.io →
posted 10 years ago
Mail

I'm getting a "Undefined variable: data" error whenever this function runs. The $data variable is definitely defined in the method calling this function so I'm not entirely sure why I'm getting this error. Any ideas?

public function email($data)
{
	Mail::queue(array('text' => 'emails.plaintext'), $data, function($message)
	{
		$message->from('my@email.co.za', 'My Name');
		
		$message->to($data['email'])->subject('Email Subject');
	});
}
Last updated 3 years ago.
0

I've tried adding $data to the function set as the 3rd parameter which hasn't helped

Mail::queue(array('text' => 'emails.plaintext'), $data, function($message, $data)
{
        $message->from('my@email.co.za', 'My Name');

        $message->to($data['email'])->subject('Email Subject');
});

I've also tried defining a new variable for the email which hasn't worked either

$email = $data['email'];

Mail::queue(array('text' => 'emails.plaintext'), $data, function($message)
{
        $message->from('my@email.co.za', 'My Name');

        $message->to($email)->subject('Email Subject');
});
Last updated 10 years ago.
0

Try with function($message) use ($data), that should do it.

0

You, Sir, are a rockstar. That did the trick. Thanks.

0

Sign in to participate in this thread!

PHPverse

Your banner here too?

VenomRush venomrush Joined 27 May 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.