1
votes

I am trying to use notification and mailgun service for the first-time in Laravel. I am using Laravel 5.4

I see Laravel 5.2+ comes with a new feature called Notification out of the box that handles password reset etc. quite well.

So I am planning to send email to users for following:

1) When User Register's for the first time

2) When user requests for a password reset

3) When User reports a problem

4) When User Send's a friend request

5) Finally, when user accepts a friend request.

I am little bit confused and lost track on how to proceed this. MailGun is all set and I can send test email to users at this point using Postman without bringing Notification in the picture. I have also setup Notification service and have 2 things in place:

I dont understand how Notification and Mail work together? Or Should I stick to anyone?

I am currently getting an error when I enter an email in Password Reset Form.

Error

ReflectionException in Container.php line 681:
Class App\Http\Controllers\ResetPasswordController does not exist

Form

<form id="msform"  role="form" method="POST" action="{{ url('/api/reset') }}">
                        {{ csrf_field() }}

                        <fieldset>
                        <h2 class="fs-title">Enter Password Reset Details</h2>
                        <h3 class="fs-subtitle">Easy as That !</h3>

                        <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">

                            <div class="col-md-6">
                                <input id="email" type="email" class="form-control" name="email" placeholder="Email Address" value="{{ old('email') }}" required>

                                @if ($errors->has('email'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('email') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group">
                            <input type="submit" name="submit" class="submit action-button" value="Reset"/>
                        </div>
                     </fieldset>
    </form>

Routes

Route::post('api/reset', 'ResetPasswordController@send');

ResetPassword Controller

public function send(Request $request)
{
    $user = Auth::user()->email;
    $user->notify(new WelcomeUser());

    Mail::send('emails.passwordreset', function ($message)
    {
        $message->from('[email protected]', 'Admin - John Doe');
        $message->to('[email protected]');
        $message->subject('Password Reset');
    });

    return response()->json(['message' => 'Request completed']);      
}

User model

use Illuminate\Notifications\Notifiable;

public function routeNotificationForMail()
{
    return $this->email;
}

Notification

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class WelcomeUser extends Notification
{
    use Queueable;


    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {

    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line("Welcome User!")
                    ->action('Let\'s Login', route('auth.login'))
                    ->line("Let's get going!");
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}
1

1 Answers

0
votes

Sure, you can use Mail Notifications and Mail together. In Notifications you just do not need to create a view blade files. All mail notifications use the same view template. Also you may want to replace some of your mails with mail notifications.

As for your specific implementation:

Assuming you are using Laravel auth scaffold controllers, add Auth\ to your route:

Route::post('api/reset', 'Auth\ResetPasswordController@send');

Also PasswordResetController cannot access Auth::user(), so in your send() function replace the first line with something like:

$user = User::where('email', $request->email)->first();

Then in your WelcomeUser notification remove auth. from your route:

->action('Let\'s Login', route('login'))

Also add [] as second parameter in Mail::send() function.