Changing the default "subject" field for the verification email in laravel 5.7

Published 3 weeks ago by RuinSain

I am trying to change the default subject in the verification email that come with laravel 5.7. How and where can i change it? I have searched all over the place by myself, and online, but because it is brand new, i could not find an answer. Can you help me please? Thanks!

Best Answer (As Selected By RuinSain)
Snapey

the file needs to be he.json and you need to change the app.config locale setting so that laravel knows which file to check

The file needs to be in json so wrap the string in { }

{
    "Verify Email Address": "My hebrew translation",
    "Please click the button below to verify your email address.":"Another translation"
}


romulo27

You have send e-mail verification?

romulo27

It should be there see the migration file of 5.7: https://github.com/laravel/laravel/blob/v5.7.0/database/migrations/2014_1012000000_create_users_table.php

RuinSain

That has nothing to do with my question. I have made the built-in auth scaffolding, and have succefuly sent emails. The subjects of those wmails is by default "Verify Email address". I want to change it to a diffrent language.

Vilfago

I guess this is not in resources/views/auth/verify.blade.php or in resources/lang folder ?

RuinSain

@Vilfago No, of cours not. My guess it deep iinside some method in some controller, or something like that, but i just cant find it. maybe need to add a function on the verify controller. dont know yet.

D9705996
D9705996
3 weeks ago (119,850 XP)

You need to add a translation string to your language specific json file e.g resources/lang/en.js ( you may need to create this file) with

{
  "Verify Email Address": "My Special Subject"
}

This key is also used for the action button text so you might want to look at the source to see what will change. I don't recommend changing this file as it's part of the core framework.

D9705996
D9705996
3 weeks ago (119,850 XP)

Alternatively if you need more control override the sendEmailVerificationNotification function from the MustVerifyEmail trait on your user model by adding you own version of this function in app\User.php

    public function sendEmailVerificationNotification()
    {
        $this->notify(new Notifications\VerifyEmail); // Replace this with your custom notification class
    }

Details on creating notifications are in the official docs

Snapey
Snapey
3 weeks ago (1,036,605 XP)

Here is the function that sends the email

    public function toMail($notifiable)
    {
        if (static::$toMailCallback) {
            return call_user_func(static::$toMailCallback, $notifiable);
        }

        return (new MailMessage)
            ->subject(Lang::getFromJson('Verify Email Address'))
            ->line(Lang::getFromJson('Please click the button below to verify your email address.'))
            ->action(
                Lang::getFromJson('Verify Email Address'),
                $this->verificationUrl($notifiable)
            )
            ->line(Lang::getFromJson('If you did not create an account, no further action is required.'));
    }

You will see that all the strings used are wrapped in Lang which means you can create a translation for the string with no code changes.

see https://laravel.com/docs/5.7/localization#using-translation-strings-as-keys

RuinSain

But how can i use it for the email? Do i need to make a new notification?

Snapey
Snapey
3 weeks ago (1,036,605 XP)

No, just create translations.

The Lang class will see if there is a translation for 'Verify Email Address' in the current locale and if not, just return the english original.

You don't just change the string, you specify the locale you want to use in the app config file and then create a file in the right folder for the language containing the english string and what you want it to be replaced by

What language is it? Then more specific instructions can be provided. You don't need to recode anything.

Cronix
Cronix
3 weeks ago (783,370 XP)

No, did you read the link in Snapeys post? You just need to create the translation file For english, it would be

resources/lang/en.json

{
    "Verify Email Address": "This is the subject I want to use."
}

since the key is 'Verify Email Address'

->subject(Lang::getFromJson('Verify Email Address'))

if you're using a different language like spanish, then you'd create a es.json file instead of en.json.

RuinSain

I asked this question on SO here: https://stackoverflow.com/questions/52408042/changing-the-default-subject-field-for-the-verification-email-in-laravel-5-7

and got an answer that is working great, but your solution just seems so easy i have to try it. @Snapey The language is hebrew.

So, if i got you right, i need to create a locale lang folder called he (for hebrew, or just hebrew), and place this in it:

"Verify Email Address": "My hebrew translation"

And thats it?

Snapey
Snapey
3 weeks ago (1,036,605 XP)

the file needs to be he.json and you need to change the app.config locale setting so that laravel knows which file to check

The file needs to be in json so wrap the string in { }

{
    "Verify Email Address": "My hebrew translation",
    "Please click the button below to verify your email address.":"Another translation"
}


RuinSain

Thanks to you both! @Snapey @Cronix

Please sign in or create an account to participate in this conversation.