如何在laravel中更改重置密码邮件主题?

rseugnpd  于 2023-03-19  发布在  其他
关注(0)|答案(9)|浏览(280)

我是Laravel的初学者。目前我正在学习这个框架。我目前的Laravel版本是5. 3。
我正在使用php artisan make:auth搭建我的auth所有都运行良好。我还在我的.env文件中配置了gmail smtp,在config directgory中配置了mail.php。所有都运行良好。但是我看到默认情况下忘记密码邮件主题是Reset Password。我想更改一下。
我看到了一些博客。我发现了一些博客。我已经实现了在我的网站。但同样的输出来。
我跟踪了这些链接-
https://laracasts.com/discuss/channels/general-discussion/laravel-5-password-reset-link-subject
https://laracasts.com/discuss/channels/general-discussion/reset-password-email-subject
https://laracasts.com/discuss/channels/laravel/how-to-override-message-in-sendresetlinkemail-in-forgotpasswordcontroller

uurv41yg

uurv41yg1#

您可以更改密码重置邮件的主题,但这需要一些额外的工作。首先,您需要创建自己的ResetPassword通知实现。
app\Notifications目录中创建一个新的通知类,我们将其命名为ResetPassword.php

<?php

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;

class ResetPassword extends Notification
{
    public $token;

    public function __construct($token)
    {
        $this->token = $token;
    }

    public function via($notifiable)
    {
        return ['mail'];
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->subject('Your Reset Password Subject Here')
            ->line('You are receiving this email because we received a password reset request for your account.')
            ->action('Reset Password', url('password/reset', $this->token))
            ->line('If you did not request a password reset, no further action is required.');
    }
}

您还可以使用artisan命令生成通知模板:

php artisan make:notification ResetPassword

或者你可以简单地复制粘贴上面的代码,你可能会注意到这个通知类和默认的Illuminate\Auth\Notifications\ResetPassword非常相似,你实际上可以从默认的ResetPassword类扩展它。
唯一的区别是,您添加了一个新的方法调用来定义电子邮件的主题:

return (new MailMessage)
        ->subject('Your Reset Password Subject Here')

您可以阅读更多关于邮件通知在这里.
其次,在你的app\User.php文件上,你需要覆盖Illuminate\Auth\Passwords\CanResetPassword trait定义的默认sendPasswordResetNotification()方法,现在你应该使用你自己的ResetPassword实现:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Notifications\ResetPassword as ResetPasswordNotification;

class User extends Authenticatable
{
    use Notifiable;

    ...

    public function sendPasswordResetNotification($token)
    {
        // Your your own implementation.
        $this->notify(new ResetPasswordNotification($token));
    }
}

现在您的重置密码电子邮件主题应该更新!

希望这能有所帮助!

fhg3lkii

fhg3lkii2#

您可以轻松修改用于向用户发送密码重置链接的通知类。要开始,请重写用户模型上的sendPasswordResetNotification方法。在此方法中,您可以使用选择的任何通知类发送通知。密码重置$token是此方法接收的第一个参数,请参阅自定义文档

/**
 * Send the password reset notification.
 *
 * @param  string  $token
 * @return void
 */
public function sendPasswordResetNotification($token)
{
    $this->notify(new ResetPasswordNotification($token));
}

希望这有帮助!

qhhrdooz

qhhrdooz3#

Laravel 5.7中,默认实现类似于:

return (new MailMessage)
            ->subject(Lang::getFromJson('Reset Password Notification'))
            ->line(Lang::getFromJson('You are receiving this email because we received a password reset request for your account.'))
            ->action(Lang::getFromJson('Reset Password'), url(config('app.url').route('password.reset', $this->token, false)))
            ->line(Lang::getFromJson('This password reset link will expire in :count minutes.', ['count' => config('auth.passwords.users.expire')]))
            ->line(Lang::getFromJson('If you did not request a password reset, no further action is required.'));

你所要做的就是将你的localeconfig/app.php修改为ro,然后在你的resources/lang中,创建一个类似于ro.json的文件:

{
  "Reset Password Notification": "Viața Medicală CMS :: Resetare parolă",
  "Hello!": "Salut,",
  "You are receiving this email because we received a password reset request for your account.": "Primești acest email deoarece am primit o solicitare de resetare a parolei pentru contul tău.",
  "Reset Password": "Reseteză parola",
  "This password reset link will expire in :count minutes.": "Acest link va expira în :count de minute.",
  "If you did not request a password reset, no further action is required.": "Dacă nu ai solicitat resetarea parolei, nu este necesară nicio altă acțiune.",
  "Regards": "Toate cele bune",
  "Oh no": "O, nu",
  "Whoops!": "Hopa!",
  "If you’re having trouble clicking the \":actionText\" button, copy and paste the URL below\ninto your web browser: [:actionURL](:actionURL)": "Dacă nu reușești să dai click pe butonul de \":actionText\", dă copy-paste la URL-ul de mai jos în browser:\n [:actionURL](:actionURL)"
}

它将翻译主题(第一个键)和邮件正文。

更新Laravel 6。*

这也可用于VerifyEmail.php通知。

kx1ctssn

kx1ctssn4#

拉腊维尔8

在验证服务提供程序. php中
添加这些代码。

ResetPassword::toMailUsing(function ($notifiable, $url) {
        return (new MailMessage)
            ->subject(Lang::get('Reset Password Notification'))
            ->line(Lang::get('You are receiving this email because we received a password reset request for your account.'))
            ->action(Lang::get('Reset Password'), $url)
            ->line(Lang::get('This password reset link will expire in :count minutes.', ['count' => config('auth.passwords.' . config('auth.defaults.passwords') . '.expire')]))
            ->line(Lang::get('If you did not request a password reset, no further action is required.'));
    });
4xrmg8kj

4xrmg8kj5#

对于询问如何更新Hello、Regards和subcopy文本的每个人:
php artisan vendor:publish(选项11)
然后在视图/供应商/通知/电子邮件. blade.php中
在这个文件中会有像Hello这样的文本,你可以通过改变来改变它:例如:第9行# @lang('Hallo!, Hei!, Bonjour!, Guten Tag!, Geia!')

jutyujz0

jutyujz06#

您可以创建一个自定义函数,该函数将创建如下所示的重置密码标记。

$user = User::where('email', 'example@name.com' )->first();
 $password_broker = app(PasswordBroker::class); //so we can have dependency injection
 $token = $password_broker->createToken($user); //create reset password token
 $password_broker->emailResetLink($user, $token, function (Message $message) {
         $message->subject('Custom Email title');
 });//send email.
xxe27gdn

xxe27gdn7#

关于此答案的注解:https://stackoverflow.com/a/40574428/9784378
您可以复制供应商文件函数并将其粘贴到您在notification文件夹中创建的Resetpassword.php文件中。

ep6jt1vc

ep6jt1vc8#

如果您不想创建新的通知类,这适用于Laravel 9。
将其添加到App\Providers\AuthServiceProvider.php的 Boot ()方法中

ResetPassword::toMailUsing(function (object $notifiable, string $token) {
    
    $url = url(route('password.reset', [
        'token' => $token,
        'email' => $notifiable->getEmailForPasswordReset(),
    ], false));

    return (new MailMessage)
        ->subject(Lang::get('Reset Password Notification'))
        ->line(Lang::get('You are receiving this email because we received a password reset request for your account.'))
        ->action(Lang::get('Reset Password'), $url)
        ->line(Lang::get('This password reset link will expire in :count minutes.', ['count' => config('auth.passwords.'.config('auth.defaults.passwords').'.expire')]))
        ->line(Lang::get('If you did not request a password reset, no further action is required.'))
});
jxct1oxe

jxct1oxe9#

只需添加以下行:

  • 〉主题(“新主题c”)
    在文件Illuminate\Auth\Notifications\ResetPassword的toMail方法中,如下所示:
public function toMail($notifiable)
{
    return (new MailMessage)
        ->subject('New Subjetc')
        ->line('You are receiving this email because we received a password reset request for your account.')
        ->action('Restaurar Contraseña', url(config('app.url').route('password.reset', $this->token, false)))
        ->line('If you did not request a password reset, no further action is required.');
}

相关问题