sendmail/logs

v1.0.1 2024-07-21 17:43 UTC

This package is auto-updated.

Last update: 2024-09-21 18:20:37 UTC


README

记录发送邮件成功或错误操作

描述

处理发送邮件成功或错误并重试 版本 v1.1.0

安装

composer require sendmail/logs

在 config/app.php 中声明服务

'providers' => ServiceProvider::defaultProviders()->merge([
        /*
         * Package Service Providers...
         */

        /*
         * Application Service Providers...
         */
        ...
        Email\Logs\EmailLogServiceProvider::class
        ...
    ])->toArray(),

发布配置,迁移

php artisan vendor:publish --tag="email_log_migration"

示例

<?php

namespace App\Jobs;

use App\Mail\SendMailTest;
use Email\Logs\EmailLogEvent;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;

class JobSendEmailTest implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $data;
    /**
     * Create a new job instance.
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        try {
            $mailable = new SendMailTest($this->data);
            Mail::to("codethue94@gmail.com")->send($mailable);
            event(new EmailLogEvent("codethue94@gmail.com", $this->data['title'] ?? "", $this->data['body'] ?? "", 'success'));
        } catch (\Exception $e) {
            event(new EmailLogEvent("codethue94@gmail.com", $this->data['title'] ?? "", $this->data['body'] ?? "", 'failure', $e->getMessage()));
        }
    }
}