Help us understand the problem. What is going on with this article?

【Laravel】メール送信失敗時のログを残す

Laravel で複数のメールアドレスへの送信処理を書いた時、メール送信が失敗したアドレスの一覧を取得したかったが、ドキュメントには載っていなかったのでメモとして残しておく。

環境

Laravel 6.X

やり方

Mail::failures() で送信失敗メールアドレスが配列で取得できる。(参考
例えば、Mailable クラスの設定後、送信処理をコントローラに書く場合、下記のような感じ。

MailSendController.php
namespace App\Http\Controllers;

use App\Mail\SampleMailableClass;
use Illuminate\Support\Facades\Mail;
use Illuminate\Http\Request;
use Log;

class MailSendController extends Controller
{
    public function send()
    {
        $emails = ['hoge@example.com', 'huga@example.com']
        Mail::to($emails)->send(new SampleMailableClass());
        if (count(Mail::failures()) > 0) {
            Log::channel('slack')->warning(Mail::failures());
        };
    }
}

Slack へのログ投稿の方法は こちらの記事 に詳しく書かれていました。

参考

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away