自定义Laravel默认验证电子邮件(更改标题)



我正在尝试更改和修改Laravel中的默认验证电子邮件,当你可以更改默认电子邮件的内容时,我已经找到了该文件,但在文件中,它只有主题和行,我找不到要更改的电子邮件标题,所以我在哪里可以找到标题行并更改它?

标题我的意思是:

";你好"单词

中文件的代码

Vendor/Laravel/Framework/src/illuminate/Auth/Notifications/VerifyEmail.php
protected function buildMailMessage($url)
{
return (new MailMessage)
->subject(Lang::get('Verify Email Address'))
->line(Lang::get('Please click the button below to verify your email address.'))
->action(Lang::get('Verify Email Address'), $url)
->line(Lang::get('If you did not create an account, no further action is required.'));
}

自定义Laravel通知电子邮件模板(页眉和页脚(最初,Laravel将使用隐藏在框架核心的组件,您可以通过进行导出

php artisan vendor:publish --tag=laravel-mail

它将在资源/视图/供应商文件夹中创建一个邮件和降价文件夹。在里面你会发现组件,如布局或标题等

正在创建通知你想做的是创建一个通知、事件或邮件类,以便在发生什么事情时发出电子邮件。我决定带着通知去。当创建任何通知时(你可以阅读更多关于如何通过artisan创建通知的信息(,你会得到这样的类:

<?php
namespace AppNotifications;
use IlluminateBusQueueable;
use IlluminateNotificationsNotification;
use IlluminateContractsQueueShouldQueue;
use IlluminateNotificationsMessagesMailMessage;
class UserRegistered extends Notification {
use Queueable;
public $user;
public function __construct($user) {
$this->user = $user;
}

public function via($notifiable) {
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return IlluminateNotificationsMessagesMailMessage
*/
public function toMail($notifiable) {
return (new MailMessage)
->from('info@sometimes-it-wont-work.com', 'Admin')
->subject('Welcome to the the Portal')
->markdown('mail.welcome.index', ['user' => $this->user]);
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable) {
return [
//
];
}
}

在这里,注意toMail方法以及类的构造函数,因为我们将向它传递一个对象->markdown('some.blade.php'(;下一步是将此通知推送至工作状态。在RegisterController中的某个位置,您可能想要调用它(不讨论如何执行它,同步或排队…(。不要忘记在顶部包含通知的名称空间。

$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'lastname' => $data['lastname'],
'password' => bcrypt($data['password']),
]);
$user->notify(new UserRegistered($user));

为什么我要这么深入?好吧,因为我也想向你展示如何将你的数据传递到电子邮件模板中。

接下来,您可以转到resources/views/mail/welcome/index.blade.php(它可以是您想要的任何文件夹和文件名(并粘贴以下内容:

@component('mail::layout')
{{-- Header --}}
@slot('header')
@component('mail::header', ['url' => config('app.url')])
Header Title
@endcomponent
@endslot

{{--正文--}}这是我们的主要消息{{$user}}

{{-- Subcopy --}}
@isset($subcopy)
@slot('subcopy')
@component('mail::subcopy')
{{ $subcopy }}
@endcomponent
@endslot
@endisset
{{-- Footer --}}
@slot('footer')
@component('mail::footer')
© {{ date('Y') }} {{ config('app.name') }}. Super FOOTER!
@endcomponent
@endslot
@endcomponent

您现在可以轻松地将任何图像添加到页眉或更改页脚内的链接等。希望这能有所帮助。

就像官方Laravel文档中提到的那样,您可以通过向AppProvidersAuthServiceProviderboot方法添加代码来实现这一点。

use IlluminateAuthNotificationsVerifyEmail;
use IlluminateNotificationsMessagesMailMessage;
public function boot()
{
// ...
VerifyEmail::toMailUsing(function ($notifiable, $url) {
return (new MailMessage)
->subject('Verify Email Address')
->line('Click the button below to verify your email address.')
->action('Verify Email Address', $url);
});
}

问题的两个答案都为您发布电子邮件模板的方式提供了绝对正确的解决方案,因为您永远不应该在"供应商";文件夹然而,我相信你的问题主要是关于如何修改";你好"字符串,上面没有回答。根据https://laravel.com/api/8.x/Illuminate/Notifications/Messages/MailMessage.html你应该使用";问候语";方法换句话说,你的代码应该是这样的:

return (new MailMessage)
->greeting(Lang::get('Hi there!'))
->subject(Lang::get('Verify Email Address'))
->line(Lang::get('Please click the button below to verify your email address.'))
->action(Lang::get('Verify Email Address'), $url)
->line(Lang::get('If you did not create an account, no further action is required.'));

我最终在用户模型中使用了Mail facade。。

public function sendPasswordResetNotification($token){
// $this->notify(new MyCustomResetPasswordNotification($token)); <--- remove this, use Mail instead like below
$data = [
$this->email
];
Mail::send('email.reset-password', [
'fullname'      => $this->fullname,
'reset_url'     => route('user.password.reset', ['token' => $token, 'email' => $this->email]),
], function($message) use($data){
$message->subject('Reset Password Request');
$message->to($data[0]);
});
}

相关内容

最新更新