phplaravelemaillaravel-5

Laravel sending email call_user_func() error


I am trying to send email in laravel for which I am using the mail::send function.

This is my code:

$data = [
          'title'=>'Some Title',
          'content'=>'Content',
          'email'=> 'email',
          'password'=>'password',
          'remarks'=>'remarks'
      ];

      Mail::send('admin.mails.activate', $data, ['user'=>$user], function ($message) use ($user) {
          $message->to($user->email, $user->name)->subject('Account Activation Email')->from('support@webmail.com');
      });

I am trying to pass the $data variable to the view file and $user variable to the callback function so that I could use user's email to send an email. But it is giving me this error:

    call_user_func() expects parameter 1 to be a valid callback, array must have exactly two members 

Solution

  • Mail::send() accepts 3 arguments (view, data, callback) but you've given it 4:

    Mail::send('admin.mails.activate', $data, ['user'=>$user], function ($message) use ($user) {
        $message->to($user->email, $user->name)->subject('Account Activation Email')->from('support@webmail.com');
    });
    

    I assume you meant to merge the ['user' => $user] array in with $data:

    Mail::send('admin.mails.activate', array_merge($data, ['user' => $user]), function ($message) use ($user) {
        $message->to($user->email, $user->name)->subject('Account Activation Email')->from('support@webmail.com');
    });
    

    or alternatively:

    $data = [
        'title'=>'Some Title',
        'content'=>'Content',
        'email'=> 'email',
        'password'=>'password',
        'remarks'=>'remarks',
        'user' => $user
    ];
    
    Mail::send('admin.mails.activate', $data, function ($message) use ($user) {
        $message->to($user->email, $user->name)->subject('Account Activation Email')->from('support@webmail.com');
    });