1
0

【Laravel】Mailableでメール件名にもBladeファイルを使いたい

Last updated at Posted at 2021-08-05

この記事は移行しました!最新の内容はこちらをご覧ください😀

なぜやりたいか

  • 件名と本文を同じところに定義したい!
    • エンジニア以外でもパッとわかるように。
  • 件名にも変数使いたいとき
    • 本文と同じようにbladeで処理を統一したい

いままでは?

こんな感じでディレクトリ分かれがち。

  • resources/lang/ja/hoge.php
  • resources/views/emails/hoge.blade.php
return $this
    ->subject(__('hoge.subject'))
    ->text('emails.hoge')
    ->with(['data' => $data]);
}

件名もBlade使う

フォルダを切って、件名と本文のファイルを用意。

  • resources/views/emails/hoge/subject.blade.php
  • resources/views/emails/hoge/body.blade.php

subject() 内で view() を使う。

return $this
    ->subject(view('emails.hoge.subject'))
    ->text('emails.hoge.body')
    ->with(['data' => $data]);
}

IDEが改行含みがち

IDE(VSCodeとか)がファイル保存時に改行保管してくれちゃったりする。

image.png

メール件名では改行取り除きたいので除去してあげる。

return $this
    ->subject(str_replace(PHP_EOL, '', view('emails.hoge.subject')))
    ->text('emails.hoge.body')
    ->with(['data' => $data]);
}

件名でも変数使いたい場合はこちら

return $this
    ->subject(str_replace(PHP_EOL, '', view('emails.hoge.subject')->with(['data' => $data])))
    ->text('emails.hoge.body')
    ->with(['data' => $data]);
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0