oneduo/laravel-mail-scheduler

批量发送电子邮件的简单方法

v1.0.0 2024-07-04 15:19 UTC

This package is auto-updated.

Last update: 2024-09-04 15:38:43 UTC


README

Status License PHP Latest Version on Packagist Downloads Run tests

此包提供批量发送电子邮件的能力。创建ScheduledEmails后,您可以使用自动调度功能或自己注册控制台内核中的命令来发送电子邮件。

目录

入门

先决条件

此包需要以下内容

  • PHP 8.1 或更高版本
  • Laravel 8.0 或更高版本

安装

要开始,您需要安装以下依赖项

composer require oneduo/laravel-mail-scheduler

就这样,您可以出发了!

配置

您可以通过运行以下命令来发布包的配置

php artisan vendor:publish --tag="mail-scheduler-config"

注意 您可以在配置文件部分中找到有关配置选项的详细信息。

用法

此包提供了一个流畅的门面来创建计划电子邮件

<?php

use App\Mail\OrderShipped;
use Oneduo\MailScheduler\Support\Facades\ScheduledEmail;

$instance = ScheduledEmail::mailable(new OrderShipped)
    ->to(['john@doe.com'])
    ->save();

加密

出于安全原因,您可能希望加密可邮寄的内容以保护敏感数据。您可以使用encrypted方法

<?php

use App\Mail\OrderShipped;
use Oneduo\MailScheduler\Support\Facades\ScheduledEmail;

$instance = ScheduledEmail::mailable(new OrderShipped)
    ->to(['john@doe.com'])
    ->encrypted() // will encrypt the mailable in database
    ->save();

配置邮件发送器

您可能希望为计划电子邮件使用特定的邮件发送器。

<?php

use App\Mail\OrderShipped;
use Oneduo\MailScheduler\Support\Facades\ScheduledEmail;

$instance = ScheduledEmail::mailable(new OrderShipped)
    ->to(['john@doe.com'])
    ->mailer('my_mailer') // mailer defined in config/mail.php
    ->save();

将电子邮件链接到源模型

您可能希望使用morphTo关系将ScheduledEmail实例链接到您的某个模型之一。它可以是用户或产品。由您决定。

<?php

use App\Mail\OrderShipped;
use App\Models\Product;
use Oneduo\MailScheduler\Support\Facades\ScheduledEmail;

$product = Product::query()->first();

$instance = ScheduledEmail::mailable(new OrderShipped($product))
    ->to(['john@doe.com'])
    ->encrypted() // will encrypt the mailable in database
    ->source($product) // 
    ->save();
<?php

namespace App\Models\Product;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Oneduo\MailScheduler\Models\ScheduledEmail;

class Product extends Model
{
    public function emails(): MorphMany
    {
        return $this->morphMany(ScheduledEmail::class, 'source');    
    }
}

发送电子邮件

auto_schedule为true时,此包可以为您注册命令。您可以使用schedule_cron配置CRON表达式。

如果您想对调度器有更多控制,您可以禁用auto_schedule并自己注册命令

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('mail-scheduler:send')
            ->everyMinute()
            ->between('08:00', '18:00');
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}

错误处理

如果在发送电子邮件时发生异常,异常消息和堆栈跟踪将被保存到模型中。命令将重新发送带有错误状态的电子邮件,直到达到max_attempts

配置文件

作者

请参阅贡献者列表,了解参与此项目的贡献者。

变更日志

请参阅变更日志,了解最近的变化。

安全

有关如何报告安全漏洞的详细信息,请参阅我们的安全策略

贡献

有关详细信息,请参阅CONTRIBUTING

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅许可证文件