環境
Laravel 5.5
背景
仕事で、とあるアクションを行った時に複数の宛先にメールを送る方法がわからずに四苦八苦してしまった。
問題点
app/Mail/sendmail.php
public function build()
{
return $this->from('XXXXfromXXXX@gmail.com')
->subject('テスト送信完了')
->to(????)
->view('emails.sendmail');
}
初めは直接->to()の中に記述していたが、エラーでうまくいかず...
見るべき場所
途中Maiableを継承している場所を確認すればいいことに気づいた。
app/Mail/sendmail.php
use Illuminate\Mail\Mailable;
class OrderShipped extends Mailable
{
illuminateなのでvendor以下のMailableを確認する。
vendor\laravel\framework\src\Illuminate\Mail\Mailable.php
/**
* The "to" recipients of the message.
*
* @var array
*/
public $to = [];
変数$toは配列で初期化されていることを発見。
(中の処理を詳しく見たい方はvendor以下のMailableクラスを参考にしてください。)
問題解決
配列を作成して、引数として渡す方法で解決した。
app/Mail/sendmail.php
public function build()
{
$to = array('XXXto1XXX@gmail.com','XXXto2XXX@yahoo.co.jp');
return $this->from('XXXXfromXXXX@gmail.com')
->subject('テスト送信完了')
->to($to)
->view('emails.sendmail');
}
気づき
継承しているクラスを確認するのが大事。