我試圖在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 [
//
];
}
}