0

I use laravel 5.6 and I succesfully send view as email.

I use this code :

Mail::to($user->email)->send(new Welcome($user));

My only problem is for password reset. I know I can customize a bit the template but how to override the default email template and send my own view ?

I try to write my own ResetPasswordNotification :

<?php

namespace App\Notifications;

use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Notifications\Messages\MailMessage;

class ResetPasswordNotification extends ResetPassword
{
/**
 * Build the mail representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \Illuminate\Notifications\Messages\MailMessage
 */
public function toMail($notifiable)
{
    if (static::$toMailCallback) {
        return call_user_func(static::$toMailCallback, $notifiable, $this->token);
    }

    return (new MailMessage)
        ->line('Vous recevez cet email car une demande de modification du mot de passe pour votre compte a été initialisée.')
        ->action('Réinitialiser le mot de passe', url(config('app.url').route('password.reset', $this->token, false)))
        ->line('Si vous n\'êtes pas à l\'origine de cette demande, merci de contacter l\'équipe du site.');
}
}

But I can only translate the email. What I want is to send my own view according to my own template.

Is it possible ?

Thank for your help.

Furya
  • 299
  • 2
  • 6
  • 15

1 Answers1

0

You can define your own view with MailMessage

Example:

return (new MailMessage)->view(
    'your.email.blade', [
        'data' => $data
    ]
);

See https://laravel.com/docs/5.6/notifications#mail-notifications for more info.

Edit:

Since you are using the password reset notification, you'll need to find the user first. I believe the $notifiable object in the toMail should be an instance of the CanResetPassword trait, so you'll have to look for the user with the email:

public function toMail($notifiable) {
{
    $email = $notifiable->getEmailForPasswordReset();
    $user = User::where('email', '=', $email)->first();

    return (new MailMessage)->view(
        'your.email.blade', [
            'user' => $user,
        ]
    );
}
JPark
  • 779
  • 4
  • 6
  • Thank it work ! Is it possible to get user information ? – Furya Aug 29 '18 at 15:35
  • Ok I see but where do I add $user->notify ? I only copy toMail from ResetPasswordNotification – Furya Aug 29 '18 at 15:58
  • I've got this notify in my User controller, but it didn't know $user. – Furya Aug 29 '18 at 16:03
  • 1
    I've found an easy way, $notifiable contain user data – Furya Aug 29 '18 at 16:10
  • Yeah sorry, just realized it was for the password reset notification, which doesn't call the notification specifically from the user model. – JPark Aug 29 '18 at 16:14
  • Do you know how to prevent redirect after success ? – Furya Aug 29 '18 at 16:26
  • You'll have to override the `reset` function in your `ResetPasswordController`. The function is located inside the `ResetsPasswords` trait in your controller. See https://stackoverflow.com/questions/11939166/how-to-override-trait-function-and-call-it-from-the-overridden-function for info on overriding trait functions. – JPark Aug 29 '18 at 16:43
  • I add sendResetResponse on my controller and erase everything except return $response, but I still be redirect. – Furya Aug 29 '18 at 17:04
  • Create a new issue with what you have so far and I can take a look at it – JPark Aug 29 '18 at 18:10
  • There you go if you have some time : https://stackoverflow.com/q/52086041/10139835 that k for your help for this case ! – Furya Aug 29 '18 at 21:03