【问题标题】:Send notification to email向电子邮件发送通知
【发布时间】:2017-05-21 22:28:53
【问题描述】:

我在一个使用 laravel 框架的网站项目上工作,我希望在单击按钮时发送通知或者发送到用户的电子邮件

  $invite = Invite::create([
    'name' => $request->get('name'),
    'email' => $request->get('email'),
    'token' => str_random(60),
]);

$invite->notify(new UserInvite());

tnx 来帮助我

【问题讨论】:

标签: php laravel wamp laravel-mail


【解决方案1】:

您使用的是邮件通知,这是答案,但您可以参考 laravel 文档的通知部分了解更多信息:

https://laravel.com/docs/5.4/notifications

首先在项目文件夹中使用终端生成通知:

php artisan make:notification UserInvite

然后在生成的文件中指定您的驱动程序为'Mail'。默认情况下它是。 laravel 也有一个很好的示例代码。最好将 $invite 注入到通知中,以便在那里使用它。这是一个快速示例代码。您可以在 App\Notifications 下找到生成的通知。

<?php

namespace App\Notifications;

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

class UserInvite extends Notification implements ShouldQueue
{
use Queueable;


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

public function via($notifiable)
{
    return ['mail']; // Here you specify your driver, your case is Mail
}

public function toMail($notifiable)
{
    return (new MailMessage)
        ->greeting('Your greeting comes here')
        ->line('The introduction to the notification.') //here is your lines in email
        ->action('Notification Action', url('/')) // here is your button
        ->line("You can use {$notifiable->token}"); // another line and you can add many lines
}
}

现在您可以拨打您的通知:

$invite->notify(new UserInvite());

由于您是在邀请时通知,因此您的通知是同一个邀请。因此,在您的通知中,您可以使用$notification-&gt;token 检索invite object 的令牌。

如果我能提供任何帮助,请告诉我。 问候。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多