【问题标题】:Laravel how to pass the name of the user when I sent the forgot password functionLaravel发送忘记密码功能时如何传递用户名
【发布时间】:2018-02-06 09:20:51
【问题描述】:

当用户向他/她的电子邮件发送忘记密码重置链接时,我想让我的邮件更加详细。这是收到重置密码链接时的图片示例。

我想在这里添加一些细节,Hello 应该是 Hello! (此处为用户名)

这是我在 SendsPasswordResetEmails.php

中添加的代码
public function sendResetLinkEmail(Request $request)
    {
        $this->validateEmail($request);

        // We will send the password reset link to this user. Once we have attempted
        // to send the link, we will examine the response then see the message we
        // need to show to the user. Finally, we'll send out a proper response.
        $response = $this->broker()->sendResetLink(
            $request->only('email')
        );

        $applicant_name = Applicant::where('email', $request->email)->get()->value('name');

        return $response == Password::RESET_LINK_SENT
                    ? $this->sendResetLinkResponse($response)
                    : $this->sendResetLinkFailedResponse($request, $response);
    }

它应该将数据传递给 app\Notifications\ApplicantResetPasswordNotification.php

public function toMail($notifiable)
{
    return (new MailMessage)
                ->from('vcc3dummy@gmail.com', 'CCV3')
                ->greeting('Hello! Applicant Name') // Applicant name pass here
                ->line('You are receiving this email because we received a password request for your account.')
                ->action('Click here to Reset Password', route('applicant.reset', $this->token))
                ->line('If you did not reset your password, no further action is required.');
}

寻求有关如何传递数据或如何查询数据的帮助。 如果有人可以帮助我,将不胜感激 提前致谢。

【问题讨论】:

  • 您可以在调用时将其传递给ApplicantResetPasswordNotification.php。你在哪里调用这个文件??

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


【解决方案1】:

这是在 laravel 中发送邮件的另一种方式 - Put that data you want to use/show in email template.

 $data = [
              'email' => $email,
              'remember_token' => $remember_token,
              'name' => $applicant_name
         ];

Mail::send('emails/forgotmail', $data, function ($message) use ($data) {
      $message->from('youremail@gmail.com');
      $message->to( $data['email'] )->subject('Forgot Password Link');
                    });

email/forgotmail”在您必须创建的“resources/views/email/forgotmail.blade.php”中。这样您就可以将您的电子邮件模板放在这里并在其中使用 $data 。

【讨论】:

  • 我正在使用 auth 生成的忘记密码。所以我会把它放在我的 SendsPasswordResetEmails.php 中?
  • 是的,你把那个认证的模板放进去。
  • 对不起.. 我的错,SendsPasswordResetEmails.php 是您的控制器吗?在控制器“SendsPasswordResetEmails.php”中添加邮件发送功能,并在视图中创建“emails/forgotmail”来放置您的电子邮件模板。
  • 没关系 :) 谢谢。顺便说一句,在我忘记密码的情况下,电子邮件只是被输入,所以我不会得到名字,因为我必须为此创建一个函数或使用查询来获取申请人的姓名。
  • 是的,您必须使用申请人的电子邮件查询才能获得申请人的姓名。这样您就可以将其以数组的形式传递给视图模板使用。
【解决方案2】:

在您的ApplicationResetPasswordNotification.php 中,您可以使用$notifiable 变量,如下所示:

public function toMail($notifiable)
{
    return (new MailMessage)
                ->from('vcc3dummy@gmail.com', 'CCV3')
                ->greeting('Hello!' . $notifiable->name) // Applicant name 
                ...
}

如果对你有用,请标记为答案!

【讨论】:

  • 就这样吧,没有什么可以传入构造函数,但是你可以dd($notifiable) 来打印对象。感谢您的回答:)
猜你喜欢
  • 2017-06-15
  • 1970-01-01
  • 2018-03-12
  • 2018-06-07
  • 2013-12-09
  • 2011-10-19
  • 2011-09-28
  • 1970-01-01
  • 2017-07-06
相关资源
最近更新 更多