Mohsin Ali
Mohsin Ali

Reputation: 500

laravel default email styling is not changing

I'm following this guide to change the default email stlying. I tried to change the stylesheet inside resources/views/mail/html/themes/default.css but it doesn't work. I'm not creating any custom notification for auth emails.

I tried to change the content of the blade file located in the following directory, it is showing updated content, but I don't know from where it is applying to style the email.

resources/views/vendor/notifications/email.blade.php

Upvotes: 0

Views: 2296

Answers (2)

Joseph Ajibodu
Joseph Ajibodu

Reputation: 1656

Laravel does not actually use any email template, instead it builds the email directly using MailMessage like this

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.'));

So to override this, in any of your Service Providers, I will go with AuthServiceProvider, add the following:

// use Illuminate\Auth\Notifications\ResetPassword;
// ...


ResetPassword::toMailUsing(function ($notifiable, $token) {
            $email = $notifiable->getEmailForPasswordReset();

            
            $emailResetUrl = url(route('password.reset', [
                    'token' => $token,
                    'email' => $email,
                ], false));
            
            // this is where you generate your own email
            return (new MailMessage)
                ->subject(Lang::get('Reset Password Notification'))
                ->view('email.auth.reset-password', [
                    'url' => $emailResetUrl
                ]);
        });

Or you learn more from this article, https://medium.com/@josephajibodu/how-to-customize-laravel-fortify-authentication-email-templates-21b6a315e279

Upvotes: 0

lordisp
lordisp

Reputation: 730

Customizing Notifications with markdown

create a copy of the default.css in resources/views/vendor/mail/html/themes. Something like custom.css

If you don't have vendor directory, consider to publish the laravel-mail tag:

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

In your Notification change the css with the theme method:

public function toMail($notifiable)
{
    return (new MailMessage)
        ->theme('custom')
        ->subject('Demo Subject')
        ->markdown('mail.invoice.paid', ['url' => $url]);
}

Customizing auth mails

While generating the auth using make:auth, it will generate the required views in the resources/view/auth folder.

The files will have the corresponding layouts tag

<x-guest-layout>

  {{-- some content --}}

</x-guest-layout>
...

You can customize the guest-layout from the resources/views/layouts/guest.blade.php or create a new custom.blade.php file and change the tags in your mail templates:

<x-custom-layout>

  {{-- some content --}}

</x-custom-layout>

Upvotes: 2

Related Questions