2017-02-14 279 views
1

我试图在Laravel首次使用通知和mailgun服务。我正在使用Laravel 5.4使用MailGun,通知 - Laravel发送电子邮件通知5.4

我看到Laravel 5.2+带有一个新的功能,叫做Notification,可以很好地处理密码重置等。

所以我打算向用户发送电子邮件为以下几点:

1)当用户注册的首次

2)当输入密码的用户请求重置

3)当用户报告问题

4)当用户发送的好友请求

5)最后,当用户接受好友请求。

我有点困惑和失去了如何进行这项工作。 MailGun已经全部设置好了,我可以使用邮递员在这一点上向用户发送测试邮件,而无需在图片中添加通知。我也安装了通知服务,并有2件事情:

我不明白如何通知和邮件一起工作?还是我应该坚持任何人?

我在密码重置表单中输入电子邮件时,当前出现错误。

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

表格
<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> 

路由
Route::post('api/reset', '[email protected]'); 

ResetPassword控制器

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']);  
} 

用户模型

use Illuminate\Notifications\Notifiable; 

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

通知

<?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 [ 
      // 
     ]; 
    } 
} 

回答

0

当然,你可以用邮件通知和邮件一起。在通知中,您不需要创建视图刀片文件。所有邮件通知使用相同的视图模板。你也可能想用邮件通知来替换你的一些邮件。

至于你具体实现:

假设你正在使用Laravel权威性支架控制器,添加Auth\到您的路线:在你发送

Route::post('api/reset', 'Auth\[email protected]'); 

也PasswordResetController无法访问Auth::user(),所以()函数替换第一行类似于:

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

然后在您的WelcomeUser否tification从路径删除auth.

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

在邮件中还添加[]作为第二个参数::发送()函数。