【问题标题】:Send email notification using MailGun, Notification - Laravel 5.4使用 MailGun、Notification - Laravel 5.4 发送电子邮件通知
【发布时间】:2017-07-02 04:14:54
【问题描述】:

我第一次尝试在 Laravel 中使用通知和 mailgun 服务。我正在使用 Laravel 5.4

我看到 Laravel 5.2+ 带有一个新功能,即开箱即用的通知,可以很好地处理密码重置等。

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

1) 用户首次注册时

2) 当用户要求重设密码时

3) 当用户报告问题时

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

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

我有点困惑,不知道如何进行此操作。 MailGun 已准备就绪,此时我可以使用 Postman 向用户发送测试电子邮件,而无需在图片中添加通知。我还设置了通知服务并准备了两件事:

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

我目前在密码重置表单中输入电子邮件时遇到错误。

错误

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', 'ResetPasswordController@send');

重置密码控制器

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

    Mail::send('emails.passwordreset', function ($message)
    {
        $message->from('admin@abc.com', 'Admin - John Doe');
        $message->to('johndoe@gmail.com');
        $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 [
            //
        ];
    }
}

【问题讨论】:

    标签: php email laravel-5 notifications laravel-5.4


    【解决方案1】:

    当然,您可以同时使用邮件通知和邮件。在通知中,您只是不需要创建视图刀片文件。所有邮件通知都使用相同的视图模板。此外,您可能想用邮件通知替换您的一些邮件。

    至于你的具体实现:

    假设您使用的是 Laravel auth 脚手架控制器,请将 Auth\ 添加到您的路由中:

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

    PasswordResetController 也无法访问Auth::user(),因此在您的 send() 函数中将第一行替换为:

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

    然后在您的WelcomeUser 通知中从您的路线中删除auth.

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

    还在 Mail::send() 函数中添加 [] 作为第二个参数。

    【讨论】:

      猜你喜欢
      • 2017-10-12
      • 1970-01-01
      • 2014-04-14
      • 2018-02-01
      • 1970-01-01
      • 2019-10-18
      • 1970-01-01
      • 2018-04-20
      • 2021-01-01
      相关资源
      最近更新 更多