📧

[Laravel]メールアドレス確認の通知メールの文面をカスタマイズしたい

2025/02/07に公開

わからなくて調べた結果、解法を二つ見つけたので共有。

解法1: 公式ドキュメントのまま書く

https://readouble.com/laravel/11.x/ja/verification.html

一番スマートな開放だと思われます。最後から2番目の「カスタマイズ」のところに書いてある通りに書く方法です。app/Providers/AppserviceProvider.phpのbootに以下のように追加。

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

/**
 * Bootstrap any application services.
 */
public function boot(): void
{
    // …

    VerifyEmail::toMailUsing(function (object $notifiable, string $url) {
        return (new MailMessage)
            ->subject('Verify Email Address')
            ->line('Click the button below to verify your email address.')
            ->action('Verify Email Address', $url);
    });
}

BladeやTextファイルなどを指定するには、、、

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

/**
 * Bootstrap any application services.
 */
public function boot(): void
{
    // …

    VerifyEmail::toMailUsing(function (object $notifiable, string $url) {
        return (new MailMessage)
            ->subject('Verify Email Address')
            ->view('[テンプレートの名前]', ['url' => $url])
            ->text('[テンプレートの名前(text)]', ['url' => $url]);
    });
}

たったこれだけで自分でコーディングした文章を仮登録メールとして送信できます。

解法2: 通知クラスのVerifyEmailを継承した通知クラス"CustomVerifyEmail"を作成

二つやることがあります。

  1. 通知クラス"CustomVerifyEmail"を作成
  2. Userクラスに"CustomVerifyEmail"で送信するよう指定

1. 通知クラス"CustomVerifyEmail"を作成

名前はCustomVerifyEmailとしましたがなんでもよいかと。

まずは通知クラスを作成。

php artisan make:notification CustomVerifyEmail

Notifications/にクラスができたらtoMailメソッド内でMailMessageからメソッドをニョキニョキ伸ばし、どんなメールにするか設定していきます。


<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Support\Facades\Lang;


class CustomVerifyEmail extends VerifyEmail
{
    use Queueable;

    /**
     * Create a new notification instance.
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the notification's delivery channels.
     *
     * @return array<int, string>
     */
    public function via($notifiable): array
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     * 
     * @param $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable): MailMessage
    {
        $verificationUrl = $this->verificationUrl($notifiable);

        // どんなメールを送るか。解法1と同じ
        return (new MailMessage)
            ->subject('Verify Email Address')
            ->view('email.verify-email', ['url' => $verificationUrl])
            ->text('email.verify-email-text', ['url' => $verificationUrl]);
    }

    /**
     * Get the array representation of the notification.
     *
     * @return array<string, mixed>
     */
    public function toArray(object $notifiable): array
    {
        return [
            //
        ];
    }
}

2. Userクラスに"CustomVerifyEmail"で送信するよう指定

1で作成したCustomVerifyEmailからメールを送るように、sendEmailVerificationNotificationメソッドに上書きします。

class User extends Authenticatable implements MustVerifyEmail
    (中略)
    /**
     * メールアドレス確認通知を送信
     *
     * @return void
     */
    public function sendEmailVerificationNotification()
    {
        $this->notify(new CustomVerifyEmail()); // ここでCustomVerifyEmailを使う
    }

解法1の方がスマートです。

Discussion