Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TecThor's avatar
Level 13

Verification email only if role is "student" - manually verify if role is "teacher"

Hi there πŸ™‚

I have the roles "student" and "teacher" in my app and only want to send a verification email if a new user selected the role "student". If the new user selected "teacher" I would like the user to be verified by an admin and send a different email after the registration.

Has anyone an idea how I could implement that?

So far I defined a custom verification email in my AppServiceProvider like this:

public function boot()
    {
        ...

        // Custom email for verification
        VerifyEmail::toMailUsing(function ($notifiable, $verifyUrl) {
            $mail = new MailMessage;
            $mail->subject('Welcome to PriMa! - Please verify your email address');
            $mail->markdown('emails.verify-email', ['url' => $verifyUrl]);
            return $mail;
        });

       ...
    }

Works fine for all users but I have the feeling, that I am wrong in the boot function within the AppServiceProvider to accomplish my goal.

Thank you for helping 😊

0 likes
2 replies
jdc1898's avatar
jdc1898
Best Answer
Level 21

You could create two separate mail classes and call them from your controller when the user is created. Build your logic in the controller.

TecThor's avatar
Level 13

Thank you jdc1898!

That was the right way to go and works like a charm.

In order to also help anyone who might run into the same problem (and probably also is as new to mails as I am) here is how I solved it:

I deleted the code from the AppServiceProvider and simply added the following at the end of the create function in my RegisterController:

if ($data['user_role'] == 3){

    // Send info-mail to teachers
    VerifyEmail::toMailUsing(function ($notifiable, $verifyUrl) {
        $mail = new MailMessage;
        $mail->subject('Your registration as teacher');
        $mail->markdown('emails.register-teacher');
        return $mail;
    });
   

} else {
    
    // Send verification to students
    VerifyEmail::toMailUsing(function ($notifiable, $verifyUrl) {
        $mail = new MailMessage;
        $mail->subject('Welcome to PriMa! - Please verify your email adress');
        $mail->markdown('emails.verify-email', ['url' => $verifyUrl]);
        return $mail;
    });


}

$this->redirectTo = '/email/verify';
return $user;

Best regards and happy coding

Please or to participate in this conversation.