4ndro1d's avatar

Send files via email from register form

This is how I can send multiple files via email directly from a form without the need of uploading the file:

$input = Input::all();

   $data = ['level' => 'register',
   'introLines' => ['Ein neuer Benutzer hat sich registriert und wartet auf die Freischaltung', 'Im Anhang befinden sich vom Benutzer übermittelte Dokumente.'],
   'actionText' => 'Benutzer verwalten',
   'actionUrl' => Config::get('constants.bywood_url') . '/admin/user',
   'outroLines' => ['Klicken Sie hier um die Benutzer zu verwalten']];


   Mail::send('vendor.notifications.email', $data, function($message) use ($input)
   {
    $message->to(Config::get('constants.bywood_mail'));
    $message->subject('Ein neuer Benutzer hat sich registriert');
    $message->from(Config::get('constants.bywood_mail'));

    if(Input::file('files')){
      foreach(Input::file('files') as $file) {
        $message->attach( $file->getRealPath(), array(
          'as' =>  $file->getClientOriginalName(), 
          'mime' =>  $file->getMimeType())
        );
      }
    }
  });

The thing is that I need to do this while registering a user. As I am using the build in authentication mechanism the RegisterController provides a create() method and somehow I cannot access the Input::File variable (it is null).

Any idea on how to solve this?

0 likes
9 replies
PovilasKorop's avatar

Inside of mail send function you cannot use Input:: facade, it's not available there - for that you wrote use $input, so play with $input variable there.

4ndro1d's avatar

I can and it is working in another Controller. The problem is that that I can't access the input facade correctrly at all in the protected function create(array $data) available in the RegisterController

PovilasKorop's avatar

@4ndro1d ok sorry, misunderstood your problem. Let's see now - that Controller function is called from RegistersUsers trait:

event(new Registered($user = $this->create($request->all())));

So create() function gets all the $request inside of it, you just need to use it. So in your case, $data is your Input::all() thing.

4ndro1d's avatar

That's what I thought as well, but it looks like I can't access the $data parameter within the Mail::send('vendor.notifications.email', $maildata, function($message) use ($input)

PovilasKorop's avatar

Oh right, then change that to ... function ($message) use ($data)

4ndro1d's avatar
      foreach($data['files'] as $file) {
        $message->attach( $file->getRealPath(), array(
          'as' =>  $file->getClientOriginalName(), 
          'mime' =>  $file->getMimeType())
        );
      }

I only get Strings within the $data object and cannot access methods like getRalPath() or getMimeType()

PovilasKorop's avatar

Ok I give up helping on every step here, ultimately you need to use $request->files() and pass it to RegisterController somehow.

4ndro1d's avatar

At least a good last advice, thanks ;)

Please or to participate in this conversation.