mercredi 9 mars 2016

Laravel 5 -> pass second argument to Mail::send method

I want to modify the create method in my AuthController. A mail with a verification link should be send to the users email after he send the registration form. It's almost done, but I have a problem, passing the users details into the function.

Here is what I have right now:

Mail::send('mails.registrierung', $data, function($message){
    $message->from('registrierung@junperbo.de', 'Registrierung');
    $message->to('email@yxz.de');
});

But it's clear, that a hardcoded email is not working. So I want to pass the created user to the function. I tried to global $user within the function (no success). I also tried to pass a second argument like:

Mail::send('mails.registrierung', $data, function($message, $user) {
    $message->from('registrierung@junperbo.de', 'Registrierung');
    $message->to($user->email);
});

The result is this error message (for the line Mail::send(....)):

Missing argument 2 for App\Http\Controllers\Auth\AuthController::App\Http\Controllers\Auth{closure}()

My whole create method:

protected function create(array $data)
{
    $user = User::create([
        'benutzername' => $data['benutzername'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'bestaetigt' => 0,
        'verfikations_schluessel' => str_random(60)
    ]);

    $data['user'] = $user;

    Mail::send('mails.registrierung', $data, function($message, $user){
        $message->from('registrierung@junperbo.de', 'Registrierung');
        $message->to($user->email);
    });

    return $user;
}

Any idea? Thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire