LoginSignup
2
0

More than 3 years have passed since last update.

Laravel 7 バッチ処理でusersテーブルのメールアドレスにビューファイルをベースとしたメールを送る

Last updated at Posted at 2020-08-12

目的

  • ビューファイルを用いたメールをバッチファイルから送信する方法をまとめる

実施環境

  • ハードウェア環境(下記の二つの環境で確認)
項目 情報
OS macOS Catalina(10.15.3)
ハードウェア MacBook Pro (16-inch ,2019)
プロセッサ 2.6 GHz 6コアIntel Core i7
メモリ 16 GB 2667 MHz DDR4
グラフィックス AMD Radeon Pro 5300M 4 GB Intel UHD Graphics 630 1536 MB
  • ソフトウェア環境
項目 情報 備考
PHP バージョン 7.4.3 Homwbrewを用いて導入
Laravel バージョン 7.0.8 commposerを用いて導入
MySQLバージョン 8.0.19 for osx10.13 on x86_64 Homwbrewを用いて導入

前提情報

概要

  1. バッチファイルの作成
  2. メール用のビューファイル設定
  3. 確認

概要

  1. バッチファイルの作成

    1. アプリ名ディレクトリで下記コマンドを実行してバッチファイル(コマンドファイル)を作成する。ファイル名を「SendMailCommand.php」とする。

      $ php artisan make:command SendMailCommand
      
    2. アプリ名ディレクトリで下記コマンドを実行して先に作成したコマンドファイルを開く。

      $ vi app/Console/Commands/SendMailCommand.php
      
    3. 開いたコマンドファイルを下記の様に修正する。(開いたファイルで作成するコマンドでメールの送信処理を行う)

      アプリ名ディレクトリ/app/Console/Commands/SendMailCommand.php
      <?php
      
      namespace App\Console\Commands;
      
      use Illuminate\Console\Command;
      //下記を追加する
      //usersテーブル用のモデルファイルを紐づける
      use App\User;
      //メール送信用ファサードを紐づける
      use Illuminate\Support\Facades\Mail;
      use App\Mail\SendMail;
      //上記までを追加する
      
      class SendMailCommand extends Command
      {
          /**
           * The name and signature of the console command.
           *
           * @var string
           */
          //下記を修正する
          protected $signature = 'app:send_mail_users';
      
          /**
           * The console command description.
           *
           * @var string
           */
          //下記を修正する
          protected $description = 'usersテーブルのemail全てにメールを送信する';
      
          /**
           * Create a new command instance.
           *
           * @return void
           */
          public function __construct()
          {
              parent::__construct();
          }
      
          /**
           * Execute the console command.
           *
           * @return mixed
           */
          public function handle()
          {
              //下記を追加・修正する
              $users_infos = User::all();
      
              foreach ($users_infos as $users_info) {
                  echo $users_info['email']."\n";
                  Mail::to($users_info['email'])->send(new SendMail());
              }
              //上記までを追加・修正する
          }
      }
      
  2. メール用のビューファイル設定

    1. アプリ名ディレクトリで下記コマンドを実行してビューファイルをメールで使用するためのクラス(Mailable)ファイルを用意する。

      $ php artisan make:mail SendMail
      
    2. アプリ名ディレクトリで下記コマンドを実行して先に作成したメール送信専用のクラスファイルを開く

      $ vi app/Mail/SendMail.php
      
    3. 開いたクラスファイルを下記の様に修正する。

      アプリ名ディレクトリ/app/Mail/SendMail.php
      <?php
      
      namespace App\Mail;
      
      use Illuminate\Bus\Queueable;
      use Illuminate\Contracts\Queue\ShouldQueue;
      use Illuminate\Mail\Mailable;
      use Illuminate\Queue\SerializesModels;
      
      class SendMail extends Mailable
      {
          use Queueable, SerializesModels;
      
          /**
           * Create a new message instance.
           *
           * @return void
           */
          public function __construct()
          {
          }
      
          /**
           * Build the message.
           *
           * @return $this
           */
          public function build()
          {
              //下記を追記
              return $this->view('mails.test');
          }
      }
      
    4. アプリ名ディレクトリで下記コマンドを実行してメールとして表示したいビューファイルを格納するディレクトリを作成する。

      $ mkdir resources/views/mails
      
    5. アプリ名で下記コマンドを実行してメールとして表示したいビューファイル作成する。

      $ vi resources/views/mails/test.blade.php
      
    6. 開いたメールとして表示したいビューファイルを下記の様に編集する。

      アプリ名ディレクトリ/resources/views/mails/test.blade.php
      これはテストメールです
      
  3. 確認

    1. アプリ名ディレクトリで下記コマンドを実行してコマンドが追加されている事を確認する。

      $ php artisan list
      
    2. 先のコマンドの出力の中にapp:send_mail_usersがある事を確認する。

    3. アプリ名ディレクトリで下記コマンドを実行してバッチ処理が正常に動作するか確認する。(実際にメールが送信されるため注意する!!!)

      $ php artisan app:send_mail_users
      
    4. usersテーブルに登録されているメールアドレスの受信ボックスを確認し、下記の様なメールが届いていれば実装完了である。

      Test_Mail_-_shun_okawa_gmail_com_-_Gmail-2.png

2
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
2
0