简体   繁体   English

Laravel为来宾使用默认电子邮件确认

[英]Laravel use default email confirmation for guests

I have a question about my code for confirmation email guests. 我对确认电子邮件邀请对象的代码有疑问。 I have a comment system. 我有一个评论系统。 When guest read a post, he can write a comment. 访客阅读帖子时,他可以发表评论。 When he add a comment, guest need confirm email (on comment form, I have field for email). 当他添加评论时,客人需要确认电子邮件(在评论表上,我有电子邮件字段)。 How I can correctly do email confirmation? 如何正确进行电子邮件确认? How In Laravel, when user register. 用户注册时如何在Laravel中使用。

Now I have table "guests" and a model Guest, with columns: name , email , email_token and email_verified_at (how in users table). 现在,我有了表“ guests”和一个模型来宾,其中包含列: nameemailemail_tokenemail_verified_at (在users表中的方式)。

In model guest I have function: 在模型来宾中,我具有功能:

public function sendEmailNotification($token)
{
    $this->notify((new ReviewEmailNotification($token)));
}

When comment is created, I call observed function created and send email: 创建评论后,我将调用created观察函数并发送电子邮件:

public function created(Review $review)
{
    Auth::guest() ? $review->sendEmailNotification($review->email_token) : '';
}

My notification: 我的通知:

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Facades\Lang;

class ReviewEmailNotification extends Notification
{
    use Queueable;
public $token;

/**
 * Create a new notification instance.
 *
 * @return void
 */
public function __construct($token)
{
    $this->token = $token;
}

/**
 * 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)
        ->subject(Lang::getFromJson('Verify comment'))
        ->line(Lang::getFromJson('Please confirm your Email:'))
        ->action(Lang::getFromJson('Confirm E-Mail'), url(config('app.url') . route('revemail', $this->token)))
        ->line(Lang::getFromJson('...'));
}

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

My function for confirm email: 我的确认电子邮件功能:

public function activateEmail($token = null)
{
    $guest = Guest::where('email_token', $token)->first();

    if ($review) {
        $guest->email_verified_at = now();
        $guest->save();
    }

    return redirect()->route('home');
}

And when email is verified I create record on guests table. 验证电子邮件后,我在来宾表上创建记录。

This solution is working. 此解决方案正在工作。 But how I can go away from email_token ? 但是如何摆脱email_token In users table I have only email_verified_at without column token. 在用户表中,我只有没有列标记的email_verified_at And I want do token with expire date, how in laravel. 我想用到期日做令牌,在laravel中怎么做。 Can I use default laravel email confirmation for guests? 我可以为访客使用默认的laravel电子邮件确认吗?

Simple modify your sendEmailNotification currently which is accepting $token change it to email and in your function where you are using token change it to email like this: 只需简单地修改当前接受$ token的sendEmailNotification即可将其更改为电子邮件,并在使用令牌的函数中将其更改为电子邮件,如下所示:

open this function ReviewEmailNotification 打开此功能ReviewEmailNotification

modify like this Review::where('email',$email)

want more help then post the complete code of ReviewEmailNotification 需要更多帮助,然后发布ReviewEmailNotification的完整代码

finally i understand your query, you have to change the code.... Dont use the user table for that because in every post/article will publish new comment/review so guest table will be good and add post_id, user_id for email confirmation , so your new function look like this 终于我了解了您的查询,您必须更改代码。...不要为此使用用户表,因为在每个帖子/文章中都会发布新的评论/评论,因此来宾表会很好,并添加post_id,user_id以进行电子邮件确认,所以你的新功能看起来像这样

public function created(Review $review)
{
    Auth::guest() ? $review->sendEmailNotification($review-> post_id,$review->user_id,) : '';
}



public function activateEmail($token = null)
{
    $guest = Guest::where('post_id', $post_id)->where('user_id', $user_id)->first();

    if ($review) {
        $guest->email_verified_at = now();
        $guest->save();
    }

    return redirect()->route('home');
}



public function sendEmailNotification($user_id,$post_id)
{
    $this->notify((new ReviewEmailNotification($post_id,$user_id )));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM