In Laravel Framework 5.4.18 I just ran php artisan make:auth
When I request to reset my password, I get an email that says
(...)
You are receiving this email because we received a password reset
request for your account
(...)
Where is the file where it is specified to say that? I want to change it completely.
Notice that here is how to change (only) the general appearance of any notification, and that here is how to change (in addition) the body of the notification.
Thank you very much.
Your User model uses the Illuminate\Auth\Passwords\CanResetPassword trait. This trait has the following function:
public function sendPasswordResetNotification($token)
{
// use Illuminate\Auth\Notifications\ResetPassword as ResetPasswordNotification
$this->notify(new ResetPasswordNotification($token));
}
When a password reset is requested, this method is called and uses the ResetPassword notification to send out the email.
If you would like to modify your reset password email, you can create a new custom Notification and define the sendPasswordResetNotification method on your User model to send your custom Notification. Defining the method directly on the User will take precedence over the method included by the trait.
Create a notification that extends the built in one:
use Illuminate\Auth\Notifications\ResetPassword;
class YourCustomResetPasswordNotification extends ResetPassword
{
/**
* Build the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('This is your custom text above the action button.')
->action('Reset Password', route('password.reset', $this->token))
->line('This is your custom text below the action button.');
}
}
Define the method on your User to use your custom notification:
class User extends Authenticatable
{
public function sendPasswordResetNotification($token)
{
$this->notify(new YourCustomResetPasswordNotification($token));
}
}
First open a terminal and go to the root of the application and run the following command:
php artisan vendor:publish
You will see some files copied, you can find the email template files in
Root_Of_App/resources/views/vendor
You can edit the Email templates for notifications there.
Related
I am doing a password reset and sending email with Notification.
I created ResetPasswordNotification. I added the sendPasswordResetNotification method inside the User.php model.It works successfully. But since my User model is working in another common project, where can I write the sendPasswordResetNotification method outside of the User.php model.
my User.php model
/**
* Send the password reset notification.
*
* #param string $token
* #return void
*/
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPasswordNotification($token));
}
In short, how can I use a method other than the user.php model to exclude the sendPasswordResetNotification method in the canResetPassword trait?
In the project where I use Notification, I opened the user model and extended the common user model and added the sendPasswordResetNotification method there. I produced a solution in this way.
I wanted to make a program that able to store a data and every the data that i send to the Database must contain the Email from user used to login to my Web. In here i'm using Myth Auth library as authenticator and login security. But i can't find the method to call the current Email so i can fetch it and push to my Database. i'm using MySql as my DB.
You have to create an Entity extending Mith's one.
So create app/Entities/User.php
Then... to use:
echo user()->getEmail();
See Documentation:
https://github.com/lonnieezell/myth-auth/blob/develop/docs/extending.md
<?php namespace App\Entities;
use Myth\Auth\Entities\User as MythUser;
class User extends MythUser
{
/**
* Returns Email
*
* #return string
*/
public function getEmail()
{
return trim(trim($this->attributes['email']));
}
}
add the following method to myth-auth\src\Entities\User.php
public function getEmail()
{
return trim(trim($this->attributes['email']));
}
then, anywhere in your app, you can call it with user()->getUsername()
You can do the same for username or any of the other user attributes.
A!
Now that we have our new entity, we need to update our UserModel to return it instead of the default Myth:Auth version:
class UserModel extends MythModel
{
protected $returnType = 'App\Entities\User';
...
I have two different types of theme/css for my emails that are sent from my app. A default one for system emails (reset password etc) and one for consumer emails (emails as a result of actions form users within the app).
In my consumer emails, in the toMail() method of the mailable/notification I execute the mailable like so:
public function toMail($notifiable)
{
return (new MailMessage)
->theme($this->theme)
->from($this->booking->school->school_email, $this->booking->school->name)
->subject($this->getSubject())
->attachData($this->booking->payments()->first()->toPdf()->output(), 'Invoice.pdf')
->markdown('mail.notifications.bookings.paid_booking', $this->toArray());
}
Notice that I call ->theme(...) on the mailable. This works perfectly fine and the correct theme is set in the template that is received in the mailbox.
When I try to use Laravel's Mailable Preview within a route:
Route::get('/mail/resetpass', function () {
return (new App\Notifications\ResetPasswordNotification('token'))->toMail(\App\User::find(2));
});
Route::get('/mail/reserved', function () {
$booking = \App\Domains\Customers\Models\Booking::find(1);
return (new \App\Domains\Customers\Notifications\ReservedBookingConfirmation($booking))->toMail($booking->customer);
});
The "default" theme, as defined in my config files is the one that is used, and my call to ->theme(...) is ignored.
Is there a solution for this? Changing the config value, isn't a feasible option as I actually want to use this functionality to allow my users to view their emails in the browser. I'm unsure what else to try.
The issue lies in the render() method of the Illuminate\Notifications\Messages\MailMessage class.
This is fixed in Laravel 8 and so this solution only applies if your project is using laravel 7 or below
it does not call the theme. I extended the class and have overridden the render method. Adding the call to ->theme(...) like so.
<?php
namespace App\Mail;
use Illuminate\Container\Container;
use Illuminate\Mail\Markdown;
use Illuminate\Notifications\Messages\MailMessage as Original;
class MailMessage extends Original
{
/**
* Render the mail notification message into an HTML string.
*
* #return string
*/
public function render()
{
if (isset($this->view)) {
return Container::getInstance()->make('mailer')->render(
$this->view, $this->data()
);
}
return Container::getInstance()
->make(Markdown::class)
->theme($this->theme ?? config('mail.markdown.theme')) //add this line here
->render($this->markdown, $this->data());
}
}
I send out user emails and afterwards an admin report. I want to change the theme of the admin notification.
To do this I defined a custom css template in the vendor/mail/themes directory.
I tried to follow this example although it is for Mailables:
https://laravel-news.com/email-themes
class AdminReport extends Notification
{
use Queueable;
protected $theme = 'adminemail';
But this doesn't change anything the theme.
I also tried to change the theme before the notification is sent and it didn't work:
config([ "mail.markdown.theme" => "adminemail" ]);
Changing the theme does work though when I set the config before I send out the first user notification.
Does anyone know the right way to do this?
As of Laravel v5.3.7 Mailables can also be passed to Notifications. So create a Mailable for your email and then pass the mailable to the toMail() method:
class AdminReport extends Mailable
{
protected $theme = 'my-theme';
...
}
-
class AdminReport extends Notification
{
...
public function toMail($notifiable)
{
return (new App\Mailables\AdminReport)->to($notifiable->email);
}
}
I work on a website project with the laravel framework and I want when I click on the button send a notification either send to the user's email
$invite = Invite::create([
'name' => $request->get('name'),
'email' => $request->get('email'),
'token' => str_random(60),
]);
$invite->notify(new UserInvite());
tnx to help me
What you are using is mail notification, Here is the answer but you can refer to notification section of laravel docs for more information:
https://laravel.com/docs/5.4/notifications
first generate the notification using your terminal in project folder:
php artisan make:notification UserInvite
Then in generated file specify your driver to be 'Mail'. byr default it is. Also laravel has a nice sample code there. And it is better you inject your $invite to the notification so you can use it there. here is a quick sample code. You can find the notification generated under 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
}
}
now you can call your notification:
$invite->notify(new UserInvite());
since you are notifying on invite, your notifiable is the same invite. As a result in your notification you can use $notification->token to retrieve the token of invite object.
Please let me know if I can be of any help.
regards.