Sending email with Laravel 4.2 using Mailgun

I’m somewhat new to using Laravel 4 and Mailgun. I’ve looked over both documentations and managed to test a few emails successfully through a simple route as shown below:

Route::get('send_test_email', function() {
    Mail::send('emails.registro', ['key' => 'value'], function($message) {
        $message->subject('Welcome to the amazing experience');
        $message->from(env('CONTACT_MAIL'), env('CONTACT_NAME'));
        $message->to('[email protected]');
    });
});

I accessed myapp/send_test_email in the browser and received an email.

However, I’m looking to send an email during user registration instead. I’ve set up a new route like this:

Route::get('mail', ['uses' => 'MailController@send', 'as' => 'send']);

Here’s the MailController I’m working with:

<?php

class MailController extends BaseController {

    public function index() {
        return View::make('signup');
    }

    public function send() {
        Mail::send('emails.registro', $data, function($message) use
           {
           $message->subject('Welcome to the amazing experience');
           $message->from(env('CONTACT_MAIL'), env('CONTACT_NAME'));
           $message->to($user->email, $user->firstname);
           });
    }
}

I also added a form in the signup page like this:

{{ Form::open(['route' => 'send', 'method' => 'get']) }}

<div class="form-group">
    {{ Form::label('username', 'Username', ['class' => 'sr-only']) }}
    {{ Form::text('username', null, ['placeholder' => 'Username', 'required', 'minlength' => 6, 'class' => 'form-control']) }}
    @foreach($errors->get('username', '<span class=error>:message</span>') as $message)
        {{$message}}
    @endforeach
</div>
<div class="form-group">
    {{ Form::label('password', 'Password', ['class' => 'sr-only']) }}
    {{ Form::password('password', ['placeholder' => 'Password', 'required', 'minlength' => 8, 'class' => 'form-control']) }}
    @foreach($errors->get('password', '<span class=error>:message</span>') as $message)
        {{$message}}
    @endforeach
</div>
<div class="form-group">
    {{ Form::label('password_confirm', 'Confirm Password', ['class' => 'sr-only']) }}
    {{ Form::password('password_confirmation', ['placeholder' => 'Confirm Password', 'required', 'minlength' => 8, 'class' => 'form-control']) }}
    @foreach($errors->get('password_confirmation', '<span class=error>:message</span>') as $message)
        {{$message}}
    @endforeach
</div>
<div class="form-group">
    {{ Form::label('email', 'Email', ['class' => 'sr-only']) }}
    {{ Form::email('email', null, ['placeholder' => 'Email', 'required', 'class' => 'form-control']) }}
    @foreach($errors->get('email', '<span class=error>:message</span>') as $message)
        {{$message}}
    @endforeach
</div>
<div class="form-group">
    {{ Form::label('firstname', 'First Name', ['class' => 'sr-only']) }}
    {{ Form::text('firstname', null, ['placeholder' => 'First Name', 'required', 'class' => 'form-control']) }}
</div>
<div class="form-group">
    {{ Form::label('lastname', 'Last Name', ['class' => 'sr-only']) }}
    {{ Form::text('lastname', null, ['placeholder' => 'Last Name', 'required', 'class' => 'form-control']) }}
</div>

<div class="form-group">
    {{ Form::submit('Register', ['class' => 'btn btn-lg btn-block btn-kinbu']) }}
</div>
{{ Form::close() }}

But I’m encountering a Parse error: syntax error, unexpected 'Mail' (T_STRING) in the controller. Can anyone help me figure out what might be wrong?

ur missing a closing parenthesis after use. also, $data and $user arent defined anywhere. grab the form input with Input::get('email'), then pass those variables into the use clause like use ($email, $name). and switch your method to POST since ur submitting user data.

Your controller has a syntax error in the use statement. You wrote use without any variables, which breaks the parser. The issue is you’re trying to reference $data and $user variables that don’t exist in your function scope.

Either drop the use clause completely or define the variables you actually need. For a registration form, grab the form input first, then pass it to your Mail closure:

public function send() {
    $userData = Input::all();
    
    Mail::send('emails.registro', $userData, function($message) use ($userData) {
        $message->subject('Welcome to the amazing experience');
        $message->from(env('CONTACT_MAIL'), env('CONTACT_NAME'));
        $message->to($userData['email'], $userData['firstname']);
    });
}

This captures your form data and makes it available to both the email template and the closure.

The syntax error happens because your closure’s use statement is wrong. You’re trying to access $data and $user variables that don’t exist in your send function’s scope. Fix this by grabbing the input data with Input::get() for each form field, then use those values in your email logic:

public function send() {
    $email = Input::get('email');
    $firstname = Input::get('firstname');
    $data = ['firstname' => $firstname];
    
    Mail::send('emails.registro', $data, function($message) use ($email, $firstname) {
        $message->subject('Welcome to the amazing experience');
        $message->from(env('CONTACT_MAIL'), env('CONTACT_NAME'));
        $message->to($email, $firstname);
    });
}

Also, change your form method to POST instead of GET since you’re submitting data.